Fix task.defer timing

This commit is contained in:
Filip Tibell 2023-01-24 12:51:12 -05:00
parent 7d0338ef3c
commit 4609092ec4
No known key found for this signature in database
2 changed files with 13 additions and 7 deletions

View file

@ -38,8 +38,13 @@ pub async fn run_registered_task<T>(
// Run the new task separately from the current one using the executor // Run the new task separately from the current one using the executor
let sender = sender.clone(); let sender = sender.clone();
let task = exec.spawn(async move { let task = exec.spawn(async move {
// HACK: For deferred tasks we yield a bunch of times to try and ensure
// we run our task at the very end of the async queue, this can fail if
// the user creates a bunch of interleaved deferred and normal tasks
if mode == TaskRunMode::Deferred { if mode == TaskRunMode::Deferred {
yield_now().await; for _ in 0..64 {
yield_now().await;
}
} }
sender sender
.send(match to_run.await { .send(match to_run.await {

View file

@ -25,18 +25,19 @@ task.wait(0.2)
assert(flag2, "Defer should work with yielding (2)") assert(flag2, "Defer should work with yielding (2)")
-- Deferred functions should run after other spawned threads -- Deferred functions should run after other spawned threads
local flag3: boolean = false local flag3: number = 1
task.defer(function() task.defer(function()
if flag3 == true then if flag3 == 2 then
flag3 = false flag3 = 3
end end
end) end)
task.spawn(function() task.spawn(function()
if flag3 == false then if flag3 == 1 then
flag3 = true flag3 = 2
end end
end) end)
assert(not flag2, "Defer should run after spawned threads") task.wait()
assert(flag3 == 3, "Defer should run after spawned threads")
-- Varargs should get passed correctly -- Varargs should get passed correctly