Migrate lune-std-task to use async-io instead of tokio for better scheduler integration + remove temp test file

This commit is contained in:
Filip Tibell 2025-04-23 22:17:21 +02:00
parent 524a5c0777
commit 54115430f5
No known key found for this signature in database
4 changed files with 22 additions and 51 deletions

11
Cargo.lock generated
View file

@ -214,9 +214,9 @@ dependencies = [
[[package]]
name = "async-io"
version = "2.3.4"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "444b0228950ee6501b3568d3c93bf1176a1fdbc3b758dcd9475046d30f4dc7e8"
checksum = "43a2b323ccce0a1d90b449fd71f2a06ca7faa7c54c2751f06c9bd851fc061059"
dependencies = [
"async-lock",
"cfg-if 1.0.0",
@ -990,9 +990,9 @@ checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]]
name = "futures-lite"
version = "2.3.0"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5"
checksum = "f5edaec856126859abb19ed65f39e90fea3a9574b9707f13539acf4abf7eb532"
dependencies = [
"fastrand",
"futures-core",
@ -1743,10 +1743,11 @@ dependencies = [
name = "lune-std-task"
version = "0.1.2"
dependencies = [
"async-io",
"futures-lite",
"lune-utils",
"mlua",
"mlua-luau-scheduler",
"tokio",
]
[[package]]

View file

@ -16,6 +16,7 @@ workspace = true
mlua = { version = "0.10.3", features = ["luau"] }
mlua-luau-scheduler = { version = "0.0.2", path = "../mlua-luau-scheduler" }
tokio = { version = "1", default-features = false, features = ["time"] }
async-io = "2.4"
futures-lite = "2.6"
lune-utils = { version = "0.1.3", path = "../lune-utils" }

View file

@ -1,15 +1,13 @@
#![allow(clippy::cargo_common_metadata)]
use std::time::Duration;
use std::time::{Duration, Instant};
use async_io::Timer;
use futures_lite::future::yield_now;
use mlua::prelude::*;
use mlua_luau_scheduler::Functions;
use tokio::{
task::yield_now,
time::{sleep, Instant},
};
use lune_utils::TableBuilder;
/**
@ -60,11 +58,17 @@ async fn wait(lua: Lua, secs: Option<f64>) -> LuaResult<f64> {
}
async fn wait_inner(_: Lua, secs: Option<f64>) -> LuaResult<f64> {
// One millisecond is a reasonable minimum sleep duration,
// anything lower than this runs the risk of completing the
// the below timer instantly, without giving control to the OS ...
let duration = Duration::from_secs_f64(secs.unwrap_or_default());
let duration = duration.max(Duration::from_millis(1));
// ... however, we should still _guarantee_ that whatever
// coroutine that calls this sleep function always yields,
// even if the timer is able to complete without doing so
yield_now().await;
// We may then sleep as normal
let before = Instant::now();
sleep(duration).await;
let after = Instant::now();
let after = Timer::after(duration).await;
Ok((after - before).as_secs_f64())
}

View file

@ -1,35 +0,0 @@
local task = require("@lune/task")
local started = os.clock()
local amount = 400000
local batches = 5
local per_batch = amount / batches
for current = 1, batches do
local thread = coroutine.running()
print(`Batch {current} / {batches}`)
for i = 1, per_batch do
--print("Spawning thread #" .. i)
task.spawn(function()
print("[BEFORE] Thread number", i)
task.wait(0.1)
--_TEST_ASYNC_WORK(0.1)
print("[AFTER] Thread number", i)
if i == per_batch then
print("Last thread in batch #" .. current)
assert(
coroutine.status(thread) == "suspended",
`Thread {i} has status {coroutine.status(thread)}`
)
task.spawn(thread)
end
end)
end
coroutine.yield()
end
local took = os.clock() - started
print(`Spawned {amount} sleeping threads in {took}s`)