Skip to content

Commit 08ec1c3

Browse files
committedJan 26, 2025
Enable Thread::reset() for all Lua versions
1 parent 1f79b56 commit 08ec1c3

File tree

4 files changed

+66
-61
lines changed

4 files changed

+66
-61
lines changed
 

Diff for: ‎src/state.rs

-3
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ pub struct LuaOptions {
9898

9999
/// Max size of thread (coroutine) object pool used to execute asynchronous functions.
100100
///
101-
/// It works on Lua 5.4 and Luau, where [`lua_resetthread`] function
102-
/// is available and allows to reuse old coroutines after resetting their state.
103-
///
104101
/// Default: **0** (disabled)
105102
///
106103
/// [`lua_resetthread`]: https://www.lua.org/manual/5.4/manual.html#lua_resetthread

Diff for: ‎src/state/raw.rs

+33-14
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ impl RawLua {
505505
/// Wraps a Lua function into a new or recycled thread (coroutine).
506506
#[cfg(feature = "async")]
507507
pub(crate) unsafe fn create_recycled_thread(&self, func: &Function) -> Result<Thread> {
508-
#[cfg(any(feature = "lua54", feature = "luau"))]
509508
if let Some(index) = (*self.extra.get()).thread_pool.pop() {
510509
let thread_state = ffi::lua_tothread(self.ref_thread(), index);
511510
ffi::lua_xpush(self.ref_thread(), thread_state, func.0.index);
@@ -525,27 +524,47 @@ impl RawLua {
525524

526525
/// Resets thread (coroutine) and returns it to the pool for later use.
527526
#[cfg(feature = "async")]
528-
#[cfg(any(feature = "lua54", feature = "luau"))]
529-
pub(crate) unsafe fn recycle_thread(&self, thread: &mut Thread) -> bool {
527+
pub(crate) unsafe fn recycle_thread(&self, thread: &mut Thread) {
528+
let thread_state = thread.1;
530529
let extra = &mut *self.extra.get();
531-
if extra.thread_pool.len() < extra.thread_pool.capacity() {
532-
let thread_state = ffi::lua_tothread(extra.ref_thread, thread.0.index);
533-
#[cfg(all(feature = "lua54", not(feature = "vendored")))]
534-
let status = ffi::lua_resetthread(thread_state);
535-
#[cfg(all(feature = "lua54", feature = "vendored"))]
536-
let status = ffi::lua_closethread(thread_state, self.state());
530+
if extra.thread_pool.len() == extra.thread_pool.capacity() {
537531
#[cfg(feature = "lua54")]
538-
if status != ffi::LUA_OK {
539-
// Error object is on top, drop it
532+
if ffi::lua_status(thread_state) != ffi::LUA_OK {
533+
// Close all to-be-closed variables without returning thread to the pool
534+
#[cfg(not(feature = "vendored"))]
535+
ffi::lua_resetthread(thread_state);
536+
#[cfg(feature = "vendored")]
537+
ffi::lua_closethread(thread_state, self.state());
538+
}
539+
return;
540+
}
541+
542+
let mut reset_ok = false;
543+
if ffi::lua_status(thread_state) == ffi::LUA_OK {
544+
if ffi::lua_gettop(thread_state) > 0 {
540545
ffi::lua_settop(thread_state, 0);
541546
}
542-
#[cfg(feature = "luau")]
547+
reset_ok = true;
548+
}
549+
550+
#[cfg(feature = "lua54")]
551+
if !reset_ok {
552+
#[cfg(not(feature = "vendored"))]
553+
let status = ffi::lua_resetthread(thread_state);
554+
#[cfg(feature = "vendored")]
555+
let status = ffi::lua_closethread(thread_state, self.state());
556+
reset_ok = status == ffi::LUA_OK;
557+
}
558+
#[cfg(feature = "luau")]
559+
if !reset_ok {
543560
ffi::lua_resetthread(thread_state);
561+
reset_ok = true;
562+
}
563+
564+
if reset_ok {
544565
extra.thread_pool.push(thread.0.index);
545566
thread.0.drop = false; // Prevent thread from being garbage collected
546-
return true;
547567
}
548-
false
549568
}
550569

551570
/// Pushes a value that implements `IntoLua` onto the Lua stack.

Diff for: ‎src/thread.rs

+30-37
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use std::fmt;
22
use std::os::raw::{c_int, c_void};
33

44
use crate::error::{Error, Result};
5-
#[allow(unused)]
6-
use crate::state::Lua;
5+
use crate::function::Function;
76
use crate::state::RawLua;
87
use crate::traits::{FromLuaMulti, IntoLuaMulti};
98
use crate::types::{LuaType, ValueRef};
@@ -232,7 +231,7 @@ impl Thread {
232231
#[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))]
233232
pub fn set_hook<F>(&self, triggers: HookTriggers, callback: F)
234233
where
235-
F: Fn(&Lua, Debug) -> Result<crate::VmState> + MaybeSend + 'static,
234+
F: Fn(&crate::Lua, Debug) -> Result<crate::VmState> + MaybeSend + 'static,
236235
{
237236
let lua = self.0.lua.lock();
238237
unsafe {
@@ -249,32 +248,37 @@ impl Thread {
249248
/// In Luau: resets to the initial state of a newly created Lua thread.
250249
/// Lua threads in arbitrary states (like yielded or errored) can be reset properly.
251250
///
252-
/// Sets a Lua function for the thread afterwards.
251+
/// Other Lua versions can reset only new or finished threads.
253252
///
254-
/// Requires `feature = "lua54"` OR `feature = "luau"`.
253+
/// Sets a Lua function for the thread afterwards.
255254
///
256255
/// [Lua 5.4]: https://www.lua.org/manual/5.4/manual.html#lua_closethread
257-
#[cfg(any(feature = "lua54", feature = "luau"))]
258-
#[cfg_attr(docsrs, doc(cfg(any(feature = "lua54", feature = "luau"))))]
259-
pub fn reset(&self, func: crate::function::Function) -> Result<()> {
256+
pub fn reset(&self, func: Function) -> Result<()> {
260257
let lua = self.0.lua.lock();
261-
if matches!(self.status_inner(&lua), ThreadStatusInner::Running) {
262-
return Err(Error::runtime("cannot reset a running thread"));
258+
let thread_state = self.state();
259+
match self.status_inner(&lua) {
260+
ThreadStatusInner::Running => return Err(Error::runtime("cannot reset a running thread")),
261+
// Any Lua can reuse new or finished thread
262+
ThreadStatusInner::New(_) => unsafe { ffi::lua_settop(thread_state, 0) },
263+
ThreadStatusInner::Finished => {}
264+
#[cfg(not(any(feature = "lua54", feature = "luau")))]
265+
_ => return Err(Error::runtime("cannot reset non-finished thread")),
266+
#[cfg(any(feature = "lua54", feature = "luau"))]
267+
_ => unsafe {
268+
#[cfg(all(feature = "lua54", not(feature = "vendored")))]
269+
let status = ffi::lua_resetthread(thread_state);
270+
#[cfg(all(feature = "lua54", feature = "vendored"))]
271+
let status = ffi::lua_closethread(thread_state, lua.state());
272+
#[cfg(feature = "lua54")]
273+
if status != ffi::LUA_OK {
274+
return Err(pop_error(thread_state, status));
275+
}
276+
#[cfg(feature = "luau")]
277+
ffi::lua_resetthread(thread_state);
278+
},
263279
}
264280

265-
let thread_state = self.state();
266281
unsafe {
267-
#[cfg(all(feature = "lua54", not(feature = "vendored")))]
268-
let status = ffi::lua_resetthread(thread_state);
269-
#[cfg(all(feature = "lua54", feature = "vendored"))]
270-
let status = ffi::lua_closethread(thread_state, lua.state());
271-
#[cfg(feature = "lua54")]
272-
if status != ffi::LUA_OK {
273-
return Err(pop_error(thread_state, status));
274-
}
275-
#[cfg(feature = "luau")]
276-
ffi::lua_resetthread(thread_state);
277-
278282
// Push function to the top of the thread stack
279283
ffi::lua_xpush(lua.ref_thread(), thread_state, func.0.index);
280284

@@ -445,30 +449,19 @@ impl LuaType for Thread {
445449

446450
#[cfg(feature = "async")]
447451
impl<R> AsyncThread<R> {
448-
#[inline]
452+
#[inline(always)]
449453
pub(crate) fn set_recyclable(&mut self, recyclable: bool) {
450454
self.recycle = recyclable;
451455
}
452456
}
453457

454458
#[cfg(feature = "async")]
455-
#[cfg(any(feature = "lua54", feature = "luau"))]
456459
impl<R> Drop for AsyncThread<R> {
457460
fn drop(&mut self) {
458461
if self.recycle {
459462
if let Some(lua) = self.thread.0.lua.try_lock() {
460-
unsafe {
461-
// For Lua 5.4 this also closes all pending to-be-closed variables
462-
if !lua.recycle_thread(&mut self.thread) {
463-
#[cfg(feature = "lua54")]
464-
if matches!(self.thread.status_inner(&lua), ThreadStatusInner::Error) {
465-
#[cfg(not(feature = "vendored"))]
466-
ffi::lua_resetthread(self.thread.state());
467-
#[cfg(feature = "vendored")]
468-
ffi::lua_closethread(self.thread.state(), lua.state());
469-
}
470-
}
471-
}
463+
// For Lua 5.4 this also closes all pending to-be-closed variables
464+
unsafe { lua.recycle_thread(&mut self.thread) };
472465
}
473466
}
474467
}
@@ -549,7 +542,7 @@ impl<R: FromLuaMulti> Future for AsyncThread<R> {
549542
#[cfg(feature = "async")]
550543
#[inline(always)]
551544
unsafe fn is_poll_pending(state: *mut ffi::lua_State) -> bool {
552-
ffi::lua_tolightuserdata(state, -1) == Lua::poll_pending().0
545+
ffi::lua_tolightuserdata(state, -1) == crate::Lua::poll_pending().0
553546
}
554547

555548
#[cfg(feature = "async")]

Diff for: ‎tests/thread.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ fn test_thread() -> Result<()> {
107107
}
108108

109109
#[test]
110-
#[cfg(any(feature = "lua54", feature = "luau"))]
111110
fn test_thread_reset() -> Result<()> {
112111
use mlua::{AnyUserData, UserData};
113112
use std::sync::Arc;
@@ -120,7 +119,8 @@ fn test_thread_reset() -> Result<()> {
120119
let arc = Arc::new(());
121120

122121
let func: Function = lua.load(r#"function(ud) coroutine.yield(ud) end"#).eval()?;
123-
let thread = lua.create_thread(func.clone())?;
122+
let thread = lua.create_thread(lua.load("return 0").into_function()?)?; // Dummy function first
123+
assert!(thread.reset(func.clone()).is_ok());
124124

125125
for _ in 0..2 {
126126
assert_eq!(thread.status(), ThreadStatus::Resumable);
@@ -145,11 +145,7 @@ fn test_thread_reset() -> Result<()> {
145145
assert!(thread.reset(func.clone()).is_err());
146146
// Reset behavior has changed in Lua v5.4.4
147147
// It's became possible to force reset thread by popping error object
148-
assert!(matches!(
149-
thread.status(),
150-
ThreadStatus::Finished | ThreadStatus::Error
151-
));
152-
// Would pass in 5.4.4
148+
assert!(matches!(thread.status(), ThreadStatus::Finished));
153149
assert!(thread.reset(func.clone()).is_ok());
154150
assert_eq!(thread.status(), ThreadStatus::Resumable);
155151
}

0 commit comments

Comments
 (0)