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::{
|
use crate::{
|
||||||
error_callback::ThreadErrorCallback,
|
error_callback::ThreadErrorCallback,
|
||||||
queue::{DeferredThreadQueue, SpawnedThreadQueue},
|
queue::{DeferredThreadQueue, SpawnedThreadQueue},
|
||||||
result_map::ThreadResultMap,
|
threads::{ThreadId, ThreadMap},
|
||||||
thread_id::ThreadId,
|
|
||||||
traits::LuaSchedulerExt,
|
traits::LuaSchedulerExt,
|
||||||
util::{is_poll_pending, LuaThreadOrFunction},
|
util::{is_poll_pending, LuaThreadOrFunction},
|
||||||
};
|
};
|
||||||
|
@ -102,13 +101,13 @@ impl Functions {
|
||||||
.app_data_ref::<ThreadErrorCallback>()
|
.app_data_ref::<ThreadErrorCallback>()
|
||||||
.expect(ERR_METADATA_NOT_ATTACHED)
|
.expect(ERR_METADATA_NOT_ATTACHED)
|
||||||
.clone();
|
.clone();
|
||||||
let result_map = lua
|
let thread_map = lua
|
||||||
.app_data_ref::<ThreadResultMap>()
|
.app_data_ref::<ThreadMap>()
|
||||||
.expect(ERR_METADATA_NOT_ATTACHED)
|
.expect(ERR_METADATA_NOT_ATTACHED)
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
let resume_queue = defer_queue.clone();
|
let resume_queue = defer_queue.clone();
|
||||||
let resume_map = result_map.clone();
|
let resume_map = thread_map.clone();
|
||||||
let resume =
|
let resume =
|
||||||
lua.create_function(move |lua, (thread, args): (LuaThread, LuaMultiValue)| {
|
lua.create_function(move |lua, (thread, args): (LuaThread, LuaMultiValue)| {
|
||||||
let _span = tracing::trace_span!("Scheduler::fn_resume").entered();
|
let _span = tracing::trace_span!("Scheduler::fn_resume").entered();
|
||||||
|
@ -158,7 +157,7 @@ impl Functions {
|
||||||
.set_environment(wrap_env)
|
.set_environment(wrap_env)
|
||||||
.into_function()?;
|
.into_function()?;
|
||||||
|
|
||||||
let spawn_map = result_map.clone();
|
let spawn_map = thread_map.clone();
|
||||||
let spawn = lua.create_function(
|
let spawn = lua.create_function(
|
||||||
move |lua, (tof, args): (LuaThreadOrFunction, LuaMultiValue)| {
|
move |lua, (tof, args): (LuaThreadOrFunction, LuaMultiValue)| {
|
||||||
let _span = tracing::trace_span!("Scheduler::fn_spawn").entered();
|
let _span = tracing::trace_span!("Scheduler::fn_spawn").entered();
|
||||||
|
|
|
@ -4,16 +4,14 @@ mod error_callback;
|
||||||
mod exit;
|
mod exit;
|
||||||
mod functions;
|
mod functions;
|
||||||
mod queue;
|
mod queue;
|
||||||
mod result_event;
|
|
||||||
mod result_map;
|
|
||||||
mod scheduler;
|
mod scheduler;
|
||||||
mod status;
|
mod status;
|
||||||
mod thread_id;
|
mod threads;
|
||||||
mod traits;
|
mod traits;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
pub use functions::Functions;
|
pub use functions::Functions;
|
||||||
pub use scheduler::Scheduler;
|
pub use scheduler::Scheduler;
|
||||||
pub use status::Status;
|
pub use status::Status;
|
||||||
pub use thread_id::ThreadId;
|
pub use threads::ThreadId;
|
||||||
pub use traits::{IntoLuaThread, LuaSchedulerExt, LuaSpawnExt};
|
pub use traits::{IntoLuaThread, LuaSchedulerExt, LuaSpawnExt};
|
||||||
|
|
|
@ -17,9 +17,8 @@ use crate::{
|
||||||
error_callback::ThreadErrorCallback,
|
error_callback::ThreadErrorCallback,
|
||||||
exit::Exit,
|
exit::Exit,
|
||||||
queue::{DeferredThreadQueue, FuturesQueue, SpawnedThreadQueue},
|
queue::{DeferredThreadQueue, FuturesQueue, SpawnedThreadQueue},
|
||||||
result_map::ThreadResultMap,
|
|
||||||
status::Status,
|
status::Status,
|
||||||
thread_id::ThreadId,
|
threads::{ThreadId, ThreadMap},
|
||||||
traits::IntoLuaThread,
|
traits::IntoLuaThread,
|
||||||
util::run_until_yield,
|
util::run_until_yield,
|
||||||
};
|
};
|
||||||
|
@ -48,7 +47,7 @@ pub struct Scheduler {
|
||||||
queue_spawn: SpawnedThreadQueue,
|
queue_spawn: SpawnedThreadQueue,
|
||||||
queue_defer: DeferredThreadQueue,
|
queue_defer: DeferredThreadQueue,
|
||||||
error_callback: ThreadErrorCallback,
|
error_callback: ThreadErrorCallback,
|
||||||
result_map: ThreadResultMap,
|
thread_map: ThreadMap,
|
||||||
status: Rc<Cell<Status>>,
|
status: Rc<Cell<Status>>,
|
||||||
exit: Exit,
|
exit: Exit,
|
||||||
}
|
}
|
||||||
|
@ -68,7 +67,7 @@ impl Scheduler {
|
||||||
let queue_spawn = SpawnedThreadQueue::new();
|
let queue_spawn = SpawnedThreadQueue::new();
|
||||||
let queue_defer = DeferredThreadQueue::new();
|
let queue_defer = DeferredThreadQueue::new();
|
||||||
let error_callback = ThreadErrorCallback::default();
|
let error_callback = ThreadErrorCallback::default();
|
||||||
let result_map = ThreadResultMap::new();
|
let result_map = ThreadMap::new();
|
||||||
let exit = Exit::new();
|
let exit = Exit::new();
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
|
@ -84,7 +83,7 @@ impl Scheduler {
|
||||||
"{ERR_METADATA_ALREADY_ATTACHED}"
|
"{ERR_METADATA_ALREADY_ATTACHED}"
|
||||||
);
|
);
|
||||||
assert!(
|
assert!(
|
||||||
lua.app_data_ref::<ThreadResultMap>().is_none(),
|
lua.app_data_ref::<ThreadMap>().is_none(),
|
||||||
"{ERR_METADATA_ALREADY_ATTACHED}"
|
"{ERR_METADATA_ALREADY_ATTACHED}"
|
||||||
);
|
);
|
||||||
assert!(
|
assert!(
|
||||||
|
@ -105,7 +104,7 @@ impl Scheduler {
|
||||||
queue_spawn,
|
queue_spawn,
|
||||||
queue_defer,
|
queue_defer,
|
||||||
error_callback,
|
error_callback,
|
||||||
result_map,
|
thread_map: result_map,
|
||||||
status,
|
status,
|
||||||
exit,
|
exit,
|
||||||
}
|
}
|
||||||
|
@ -201,7 +200,7 @@ impl Scheduler {
|
||||||
args: impl IntoLuaMulti,
|
args: impl IntoLuaMulti,
|
||||||
) -> LuaResult<ThreadId> {
|
) -> LuaResult<ThreadId> {
|
||||||
let id = self.queue_spawn.push_item(&self.lua, thread, args)?;
|
let id = self.queue_spawn.push_item(&self.lua, thread, args)?;
|
||||||
self.result_map.track(id);
|
self.thread_map.track(id);
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,7 +227,7 @@ impl Scheduler {
|
||||||
args: impl IntoLuaMulti,
|
args: impl IntoLuaMulti,
|
||||||
) -> LuaResult<ThreadId> {
|
) -> LuaResult<ThreadId> {
|
||||||
let id = self.queue_defer.push_item(&self.lua, thread, args)?;
|
let id = self.queue_defer.push_item(&self.lua, thread, args)?;
|
||||||
self.result_map.track(id);
|
self.thread_map.track(id);
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,7 +247,7 @@ impl Scheduler {
|
||||||
*/
|
*/
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get_thread_result(&self, id: ThreadId) -> Option<LuaResult<LuaMultiValue>> {
|
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.
|
This will return instantly if the thread has already completed.
|
||||||
*/
|
*/
|
||||||
pub async fn wait_for_thread(&self, id: ThreadId) {
|
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.
|
when there are new Lua threads to enqueue and potentially more work to be done.
|
||||||
*/
|
*/
|
||||||
let fut = async {
|
let fut = async {
|
||||||
let result_map = self.result_map.clone();
|
let result_map = self.thread_map.clone();
|
||||||
let process_thread = |thread: LuaThread, args| {
|
let process_thread = |thread: LuaThread, args| {
|
||||||
// NOTE: Thread may have been cancelled from Lua
|
// NOTE: Thread may have been cancelled from Lua
|
||||||
// before we got here, so we need to check it again
|
// 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::<SpawnedThreadQueue>();
|
||||||
self.lua.remove_app_data::<DeferredThreadQueue>();
|
self.lua.remove_app_data::<DeferredThreadQueue>();
|
||||||
self.lua.remove_app_data::<ThreadErrorCallback>();
|
self.lua.remove_app_data::<ThreadErrorCallback>();
|
||||||
self.lua.remove_app_data::<ThreadResultMap>();
|
self.lua.remove_app_data::<ThreadMap>();
|
||||||
self.lua.remove_app_data::<Exit>();
|
self.lua.remove_app_data::<Exit>();
|
||||||
} else {
|
} else {
|
||||||
// In any other case we panic if metadata was removed incorrectly
|
// In any other case we panic if metadata was removed incorrectly
|
||||||
|
@ -472,7 +471,7 @@ impl Drop for Scheduler {
|
||||||
.remove_app_data::<ThreadErrorCallback>()
|
.remove_app_data::<ThreadErrorCallback>()
|
||||||
.expect(ERR_METADATA_REMOVED);
|
.expect(ERR_METADATA_REMOVED);
|
||||||
self.lua
|
self.lua
|
||||||
.remove_app_data::<ThreadResultMap>()
|
.remove_app_data::<ThreadMap>()
|
||||||
.expect(ERR_METADATA_REMOVED);
|
.expect(ERR_METADATA_REMOVED);
|
||||||
self.lua
|
self.lua
|
||||||
.remove_app_data::<Exit>()
|
.remove_app_data::<Exit>()
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::{cell::RefCell, rc::Rc};
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::{result_event::OnceEvent, thread_id::ThreadId};
|
use super::{event::OnceEvent, id::ThreadId};
|
||||||
|
|
||||||
struct ThreadEvent {
|
struct ThreadEvent {
|
||||||
result: Option<LuaResult<LuaMultiValue>>,
|
result: Option<LuaResult<LuaMultiValue>>,
|
||||||
|
@ -22,11 +22,11 @@ impl ThreadEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct ThreadResultMap {
|
pub(crate) struct ThreadMap {
|
||||||
inner: Rc<RefCell<FxHashMap<ThreadId, ThreadEvent>>>,
|
inner: Rc<RefCell<FxHashMap<ThreadId, ThreadEvent>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ThreadResultMap {
|
impl ThreadMap {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let inner = Rc::new(RefCell::new(FxHashMap::default()));
|
let inner = Rc::new(RefCell::new(FxHashMap::default()));
|
||||||
Self { inner }
|
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::{
|
use crate::{
|
||||||
exit::Exit,
|
exit::Exit,
|
||||||
queue::{DeferredThreadQueue, FuturesQueue, SpawnedThreadQueue},
|
queue::{DeferredThreadQueue, FuturesQueue, SpawnedThreadQueue},
|
||||||
result_map::ThreadResultMap,
|
|
||||||
scheduler::Scheduler,
|
scheduler::Scheduler,
|
||||||
thread_id::ThreadId,
|
threads::{ThreadId, ThreadMap},
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -314,21 +313,21 @@ impl LuaSchedulerExt for Lua {
|
||||||
|
|
||||||
fn track_thread(&self, id: ThreadId) {
|
fn track_thread(&self, id: ThreadId) {
|
||||||
let map = self
|
let map = self
|
||||||
.app_data_ref::<ThreadResultMap>()
|
.app_data_ref::<ThreadMap>()
|
||||||
.expect("lua threads can only be tracked from within an active scheduler");
|
.expect("lua threads can only be tracked from within an active scheduler");
|
||||||
map.track(id);
|
map.track(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_thread_result(&self, id: ThreadId) -> Option<LuaResult<LuaMultiValue>> {
|
fn get_thread_result(&self, id: ThreadId) -> Option<LuaResult<LuaMultiValue>> {
|
||||||
let map = self
|
let map = self
|
||||||
.app_data_ref::<ThreadResultMap>()
|
.app_data_ref::<ThreadMap>()
|
||||||
.expect("lua threads results can only be retrieved from within an active scheduler");
|
.expect("lua threads results can only be retrieved from within an active scheduler");
|
||||||
map.remove(id)
|
map.remove(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wait_for_thread(&self, id: ThreadId) -> impl Future<Output = ()> {
|
fn wait_for_thread(&self, id: ThreadId) -> impl Future<Output = ()> {
|
||||||
let map = self
|
let map = self
|
||||||
.app_data_ref::<ThreadResultMap>()
|
.app_data_ref::<ThreadMap>()
|
||||||
.expect("lua threads results can only be retrieved from within an active scheduler");
|
.expect("lua threads results can only be retrieved from within an active scheduler");
|
||||||
async move { map.listen(id).await }
|
async move { map.listen(id).await }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue