1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-05 18:41:05 +00:00

Add API to cancel async jobs (#14602)

* Implement API to cancel async jobs

Co-authored-by: sfan5 <sfan5@live.de>

* update AsyncJob:cancel documentation from review

* Use IPC to unblock async

* review

* review async unblocking

* review

* Apply suggestions from code review

Co-authored-by: sfan5 <sfan5@live.de>

* minor licensing

---------

Co-authored-by: y5nw <y5nw@protonmail.com>
Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
y5nw 2025-08-26 12:40:31 +02:00 committed by GitHub
parent 7cbe62fe7b
commit f390137d6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 229 additions and 78 deletions

View file

@ -207,3 +207,32 @@ local function test_vector_preserve(cb)
end, {vec})
end
unittests.register("test_async_vector", test_vector_preserve, {async=true})
local function test_async_job_replacement(cb)
core.ipc_set("unittests:end_blocking", nil)
local capacity = core.get_async_threading_capacity()
for _ = 1, capacity do
core.handle_async(function()
core.ipc_poll("unittests:end_blocking", 1000)
end, function() end)
end
local job = core.handle_async(function()
end, function()
return cb("Canceled async job ran")
end)
if not job:cancel() then
return cb("AsyncJob:cancel sanity check failed")
end
core.ipc_set("unittests:end_blocking", true)
-- Try to cancel a job that is already run.
job = core.handle_async(function(x)
return x
end, function(ret)
if job:cancel() then
return cb("AsyncJob:cancel canceled a completed job")
end
cb()
end, 1)
end
unittests.register("test_async_job_replacement", test_async_job_replacement, {async=true})