From c35eaa7899eaa160d6976a04f129a2c1daa15832 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 30 Apr 2025 13:39:35 +0200 Subject: [PATCH] Organize thread-related files a bit better --- crates/mlua-luau-scheduler/src/functions.rs | 11 ++++---- crates/mlua-luau-scheduler/src/lib.rs | 6 ++--- crates/mlua-luau-scheduler/src/scheduler.rs | 25 +++++++++---------- .../src/{result_event.rs => threads/event.rs} | 0 .../src/{thread_id.rs => threads/id.rs} | 0 .../src/{result_map.rs => threads/map.rs} | 6 ++--- crates/mlua-luau-scheduler/src/threads/mod.rs | 6 +++++ crates/mlua-luau-scheduler/src/traits.rs | 9 +++---- 8 files changed, 32 insertions(+), 31 deletions(-) rename crates/mlua-luau-scheduler/src/{result_event.rs => threads/event.rs} (100%) rename crates/mlua-luau-scheduler/src/{thread_id.rs => threads/id.rs} (100%) rename crates/mlua-luau-scheduler/src/{result_map.rs => threads/map.rs} (93%) create mode 100644 crates/mlua-luau-scheduler/src/threads/mod.rs diff --git a/crates/mlua-luau-scheduler/src/functions.rs b/crates/mlua-luau-scheduler/src/functions.rs index cb26a23..1461b6a 100644 --- a/crates/mlua-luau-scheduler/src/functions.rs +++ b/crates/mlua-luau-scheduler/src/functions.rs @@ -5,8 +5,7 @@ use mlua::prelude::*; use crate::{ error_callback::ThreadErrorCallback, queue::{DeferredThreadQueue, SpawnedThreadQueue}, - result_map::ThreadResultMap, - thread_id::ThreadId, + threads::{ThreadId, ThreadMap}, traits::LuaSchedulerExt, util::{is_poll_pending, LuaThreadOrFunction}, }; @@ -102,13 +101,13 @@ impl Functions { .app_data_ref::() .expect(ERR_METADATA_NOT_ATTACHED) .clone(); - let result_map = lua - .app_data_ref::() + let thread_map = lua + .app_data_ref::() .expect(ERR_METADATA_NOT_ATTACHED) .clone(); let resume_queue = defer_queue.clone(); - let resume_map = result_map.clone(); + let resume_map = thread_map.clone(); let resume = lua.create_function(move |lua, (thread, args): (LuaThread, LuaMultiValue)| { let _span = tracing::trace_span!("Scheduler::fn_resume").entered(); @@ -158,7 +157,7 @@ impl Functions { .set_environment(wrap_env) .into_function()?; - let spawn_map = result_map.clone(); + let spawn_map = thread_map.clone(); let spawn = lua.create_function( move |lua, (tof, args): (LuaThreadOrFunction, LuaMultiValue)| { let _span = tracing::trace_span!("Scheduler::fn_spawn").entered(); diff --git a/crates/mlua-luau-scheduler/src/lib.rs b/crates/mlua-luau-scheduler/src/lib.rs index 8c8b201..68a7beb 100644 --- a/crates/mlua-luau-scheduler/src/lib.rs +++ b/crates/mlua-luau-scheduler/src/lib.rs @@ -4,16 +4,14 @@ mod error_callback; mod exit; mod functions; mod queue; -mod result_event; -mod result_map; mod scheduler; mod status; -mod thread_id; +mod threads; mod traits; mod util; pub use functions::Functions; pub use scheduler::Scheduler; pub use status::Status; -pub use thread_id::ThreadId; +pub use threads::ThreadId; pub use traits::{IntoLuaThread, LuaSchedulerExt, LuaSpawnExt}; diff --git a/crates/mlua-luau-scheduler/src/scheduler.rs b/crates/mlua-luau-scheduler/src/scheduler.rs index 7eeaa7d..00bd2fd 100644 --- a/crates/mlua-luau-scheduler/src/scheduler.rs +++ b/crates/mlua-luau-scheduler/src/scheduler.rs @@ -17,9 +17,8 @@ use crate::{ error_callback::ThreadErrorCallback, exit::Exit, queue::{DeferredThreadQueue, FuturesQueue, SpawnedThreadQueue}, - result_map::ThreadResultMap, status::Status, - thread_id::ThreadId, + threads::{ThreadId, ThreadMap}, traits::IntoLuaThread, util::run_until_yield, }; @@ -48,7 +47,7 @@ pub struct Scheduler { queue_spawn: SpawnedThreadQueue, queue_defer: DeferredThreadQueue, error_callback: ThreadErrorCallback, - result_map: ThreadResultMap, + thread_map: ThreadMap, status: Rc>, exit: Exit, } @@ -68,7 +67,7 @@ impl Scheduler { let queue_spawn = SpawnedThreadQueue::new(); let queue_defer = DeferredThreadQueue::new(); let error_callback = ThreadErrorCallback::default(); - let result_map = ThreadResultMap::new(); + let result_map = ThreadMap::new(); let exit = Exit::new(); assert!( @@ -84,7 +83,7 @@ impl Scheduler { "{ERR_METADATA_ALREADY_ATTACHED}" ); assert!( - lua.app_data_ref::().is_none(), + lua.app_data_ref::().is_none(), "{ERR_METADATA_ALREADY_ATTACHED}" ); assert!( @@ -105,7 +104,7 @@ impl Scheduler { queue_spawn, queue_defer, error_callback, - result_map, + thread_map: result_map, status, exit, } @@ -201,7 +200,7 @@ impl Scheduler { args: impl IntoLuaMulti, ) -> LuaResult { let id = self.queue_spawn.push_item(&self.lua, thread, args)?; - self.result_map.track(id); + self.thread_map.track(id); Ok(id) } @@ -228,7 +227,7 @@ impl Scheduler { args: impl IntoLuaMulti, ) -> LuaResult { let id = self.queue_defer.push_item(&self.lua, thread, args)?; - self.result_map.track(id); + self.thread_map.track(id); Ok(id) } @@ -248,7 +247,7 @@ impl Scheduler { */ #[must_use] pub fn get_thread_result(&self, id: ThreadId) -> Option> { - self.result_map.remove(id) + self.thread_map.remove(id) } /** @@ -257,7 +256,7 @@ impl Scheduler { This will return instantly if the thread has already completed. */ pub async fn wait_for_thread(&self, id: ThreadId) { - self.result_map.listen(id).await; + self.thread_map.listen(id).await; } /** @@ -320,7 +319,7 @@ impl Scheduler { when there are new Lua threads to enqueue and potentially more work to be done. */ let fut = async { - let result_map = self.result_map.clone(); + let result_map = self.thread_map.clone(); let process_thread = |thread: LuaThread, args| { // NOTE: Thread may have been cancelled from Lua // before we got here, so we need to check it again @@ -458,7 +457,7 @@ impl Drop for Scheduler { self.lua.remove_app_data::(); self.lua.remove_app_data::(); self.lua.remove_app_data::(); - self.lua.remove_app_data::(); + self.lua.remove_app_data::(); self.lua.remove_app_data::(); } else { // In any other case we panic if metadata was removed incorrectly @@ -472,7 +471,7 @@ impl Drop for Scheduler { .remove_app_data::() .expect(ERR_METADATA_REMOVED); self.lua - .remove_app_data::() + .remove_app_data::() .expect(ERR_METADATA_REMOVED); self.lua .remove_app_data::() diff --git a/crates/mlua-luau-scheduler/src/result_event.rs b/crates/mlua-luau-scheduler/src/threads/event.rs similarity index 100% rename from crates/mlua-luau-scheduler/src/result_event.rs rename to crates/mlua-luau-scheduler/src/threads/event.rs diff --git a/crates/mlua-luau-scheduler/src/thread_id.rs b/crates/mlua-luau-scheduler/src/threads/id.rs similarity index 100% rename from crates/mlua-luau-scheduler/src/thread_id.rs rename to crates/mlua-luau-scheduler/src/threads/id.rs diff --git a/crates/mlua-luau-scheduler/src/result_map.rs b/crates/mlua-luau-scheduler/src/threads/map.rs similarity index 93% rename from crates/mlua-luau-scheduler/src/result_map.rs rename to crates/mlua-luau-scheduler/src/threads/map.rs index 456c0c6..a3c6794 100644 --- a/crates/mlua-luau-scheduler/src/result_map.rs +++ b/crates/mlua-luau-scheduler/src/threads/map.rs @@ -5,7 +5,7 @@ use std::{cell::RefCell, rc::Rc}; use mlua::prelude::*; use rustc_hash::FxHashMap; -use crate::{result_event::OnceEvent, thread_id::ThreadId}; +use super::{event::OnceEvent, id::ThreadId}; struct ThreadEvent { result: Option>, @@ -22,11 +22,11 @@ impl ThreadEvent { } #[derive(Clone)] -pub(crate) struct ThreadResultMap { +pub(crate) struct ThreadMap { inner: Rc>>, } -impl ThreadResultMap { +impl ThreadMap { pub fn new() -> Self { let inner = Rc::new(RefCell::new(FxHashMap::default())); Self { inner } diff --git a/crates/mlua-luau-scheduler/src/threads/mod.rs b/crates/mlua-luau-scheduler/src/threads/mod.rs new file mode 100644 index 0000000..4ec6006 --- /dev/null +++ b/crates/mlua-luau-scheduler/src/threads/mod.rs @@ -0,0 +1,6 @@ +mod event; +mod id; +mod map; + +pub use id::ThreadId; +pub(crate) use map::ThreadMap; diff --git a/crates/mlua-luau-scheduler/src/traits.rs b/crates/mlua-luau-scheduler/src/traits.rs index 27a3733..90122b9 100644 --- a/crates/mlua-luau-scheduler/src/traits.rs +++ b/crates/mlua-luau-scheduler/src/traits.rs @@ -12,9 +12,8 @@ use tracing::trace; use crate::{ exit::Exit, queue::{DeferredThreadQueue, FuturesQueue, SpawnedThreadQueue}, - result_map::ThreadResultMap, scheduler::Scheduler, - thread_id::ThreadId, + threads::{ThreadId, ThreadMap}, }; /** @@ -314,21 +313,21 @@ impl LuaSchedulerExt for Lua { fn track_thread(&self, id: ThreadId) { let map = self - .app_data_ref::() + .app_data_ref::() .expect("lua threads can only be tracked from within an active scheduler"); map.track(id); } fn get_thread_result(&self, id: ThreadId) -> Option> { let map = self - .app_data_ref::() + .app_data_ref::() .expect("lua threads results can only be retrieved from within an active scheduler"); map.remove(id) } fn wait_for_thread(&self, id: ThreadId) -> impl Future { let map = self - .app_data_ref::() + .app_data_ref::() .expect("lua threads results can only be retrieved from within an active scheduler"); async move { map.listen(id).await } }