mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
Organize thread-related files a bit better
This commit is contained in:
parent
b57fa6fad3
commit
c35eaa7899
8 changed files with 32 additions and 31 deletions
|
@ -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::<ThreadErrorCallback>()
|
||||
.expect(ERR_METADATA_NOT_ATTACHED)
|
||||
.clone();
|
||||
let result_map = lua
|
||||
.app_data_ref::<ThreadResultMap>()
|
||||
let thread_map = lua
|
||||
.app_data_ref::<ThreadMap>()
|
||||
.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();
|
||||
|
|
|
@ -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};
|
||||
|
|
|
@ -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<Cell<Status>>,
|
||||
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::<ThreadResultMap>().is_none(),
|
||||
lua.app_data_ref::<ThreadMap>().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<ThreadId> {
|
||||
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<ThreadId> {
|
||||
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<LuaResult<LuaMultiValue>> {
|
||||
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::<SpawnedThreadQueue>();
|
||||
self.lua.remove_app_data::<DeferredThreadQueue>();
|
||||
self.lua.remove_app_data::<ThreadErrorCallback>();
|
||||
self.lua.remove_app_data::<ThreadResultMap>();
|
||||
self.lua.remove_app_data::<ThreadMap>();
|
||||
self.lua.remove_app_data::<Exit>();
|
||||
} else {
|
||||
// In any other case we panic if metadata was removed incorrectly
|
||||
|
@ -472,7 +471,7 @@ impl Drop for Scheduler {
|
|||
.remove_app_data::<ThreadErrorCallback>()
|
||||
.expect(ERR_METADATA_REMOVED);
|
||||
self.lua
|
||||
.remove_app_data::<ThreadResultMap>()
|
||||
.remove_app_data::<ThreadMap>()
|
||||
.expect(ERR_METADATA_REMOVED);
|
||||
self.lua
|
||||
.remove_app_data::<Exit>()
|
||||
|
|
|
@ -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<LuaResult<LuaMultiValue>>,
|
||||
|
@ -22,11 +22,11 @@ impl ThreadEvent {
|
|||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ThreadResultMap {
|
||||
pub(crate) struct ThreadMap {
|
||||
inner: Rc<RefCell<FxHashMap<ThreadId, ThreadEvent>>>,
|
||||
}
|
||||
|
||||
impl ThreadResultMap {
|
||||
impl ThreadMap {
|
||||
pub fn new() -> Self {
|
||||
let inner = Rc::new(RefCell::new(FxHashMap::default()));
|
||||
Self { inner }
|
6
crates/mlua-luau-scheduler/src/threads/mod.rs
Normal file
6
crates/mlua-luau-scheduler/src/threads/mod.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
mod event;
|
||||
mod id;
|
||||
mod map;
|
||||
|
||||
pub use id::ThreadId;
|
||||
pub(crate) use map::ThreadMap;
|
|
@ -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::<ThreadResultMap>()
|
||||
.app_data_ref::<ThreadMap>()
|
||||
.expect("lua threads can only be tracked from within an active scheduler");
|
||||
map.track(id);
|
||||
}
|
||||
|
||||
fn get_thread_result(&self, id: ThreadId) -> Option<LuaResult<LuaMultiValue>> {
|
||||
let map = self
|
||||
.app_data_ref::<ThreadResultMap>()
|
||||
.app_data_ref::<ThreadMap>()
|
||||
.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<Output = ()> {
|
||||
let map = self
|
||||
.app_data_ref::<ThreadResultMap>()
|
||||
.app_data_ref::<ThreadMap>()
|
||||
.expect("lua threads results can only be retrieved from within an active scheduler");
|
||||
async move { map.listen(id).await }
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue