mirror of
https://github.com/lune-org/mlua-luau-scheduler.git
synced 2025-04-10 21:40:55 +01:00
Get rid of poll pending hack, more threads in example file
This commit is contained in:
parent
1e09e6238e
commit
bab29a5cbf
3 changed files with 7 additions and 43 deletions
|
@ -2,7 +2,7 @@
|
||||||
--!nolint UnknownGlobal
|
--!nolint UnknownGlobal
|
||||||
|
|
||||||
local NUM_BATCHES = 10
|
local NUM_BATCHES = 10
|
||||||
local NUM_THREADS = 10_000
|
local NUM_THREADS = 100_000
|
||||||
|
|
||||||
print(`Spawning {NUM_BATCHES * NUM_THREADS} threads split into {NUM_BATCHES} batches\n`)
|
print(`Spawning {NUM_BATCHES * NUM_THREADS} threads split into {NUM_BATCHES} batches\n`)
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,8 @@ use smol::prelude::*;
|
||||||
use smol::{block_on, Executor, LocalExecutor};
|
use smol::{block_on, Executor, LocalExecutor};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
error_callback::ThreadErrorCallback,
|
error_callback::ThreadErrorCallback, queue::ThreadQueue, traits::IntoLuaThread,
|
||||||
queue::ThreadQueue,
|
util::LuaThreadOrFunction,
|
||||||
traits::IntoLuaThread,
|
|
||||||
util::{is_poll_pending, LuaThreadOrFunction},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Runtime<'lua> {
|
pub struct Runtime<'lua> {
|
||||||
|
@ -112,7 +110,10 @@ impl<'lua> Runtime<'lua> {
|
||||||
// and only if we get the pending value back we can spawn to async executor
|
// and only if we get the pending value back we can spawn to async executor
|
||||||
match thread.resume::<_, LuaValue>(args.clone()) {
|
match thread.resume::<_, LuaValue>(args.clone()) {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
if is_poll_pending(&v) {
|
if v.as_light_userdata()
|
||||||
|
.map(|l| l == Lua::poll_pending())
|
||||||
|
.unwrap_or_default()
|
||||||
|
{
|
||||||
spawn_queue.push(lua, &thread, args)?;
|
spawn_queue.push(lua, &thread, args)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
37
lib/util.rs
37
lib/util.rs
|
@ -1,42 +1,5 @@
|
||||||
use std::cell::OnceCell;
|
|
||||||
|
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
use crate::IntoLuaThread;
|
|
||||||
|
|
||||||
thread_local! {
|
|
||||||
static POLL_PENDING: OnceCell<LuaLightUserData> = OnceCell::new();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_poll_pending(lua: &Lua) -> LuaResult<LuaLightUserData> {
|
|
||||||
let yielder_fn = lua.create_async_function(|_, ()| async move {
|
|
||||||
smol::future::yield_now().await;
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
|
|
||||||
yielder_fn
|
|
||||||
.into_lua_thread(lua)?
|
|
||||||
.resume::<_, LuaLightUserData>(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn is_poll_pending(value: &LuaValue) -> bool {
|
|
||||||
// TODO: Replace with Lua::poll_pending() when it's available
|
|
||||||
|
|
||||||
let pp = POLL_PENDING.with(|cell| {
|
|
||||||
*cell.get_or_init(|| {
|
|
||||||
let lua = Lua::new().into_static();
|
|
||||||
let pending = get_poll_pending(lua).unwrap();
|
|
||||||
// SAFETY: We only use the Lua state for the lifetime of this function,
|
|
||||||
// and the "poll pending" light userdata / pointer is completely static.
|
|
||||||
drop(unsafe { Lua::from_static(lua) });
|
|
||||||
pending
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
matches!(value, LuaValue::LightUserData(u) if u == &pp)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Wrapper struct to accept either a Lua thread or a Lua function as function argument.
|
Wrapper struct to accept either a Lua thread or a Lua function as function argument.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue