diff options
author | sfan5 <sfan5@live.de> | 2022-05-02 20:55:04 +0200 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2022-05-02 20:56:06 +0200 |
commit | e7659883cc6fca343785da2a1af3890ae273abbf (patch) | |
tree | b8d2e3bdbe10ed0e99074207113e24ccca3fb5df /builtin/game | |
parent | 663c9364289dae45aeb86a87cba826f577d84a9c (diff) | |
download | minetest-e7659883cc6fca343785da2a1af3890ae273abbf.tar.gz minetest-e7659883cc6fca343785da2a1af3890ae273abbf.tar.bz2 minetest-e7659883cc6fca343785da2a1af3890ae273abbf.zip |
Async environment for mods to do concurrent tasks (#11131)
Diffstat (limited to 'builtin/game')
-rw-r--r-- | builtin/game/async.lua | 22 | ||||
-rw-r--r-- | builtin/game/init.lua | 1 | ||||
-rw-r--r-- | builtin/game/misc.lua | 29 |
3 files changed, 52 insertions, 0 deletions
diff --git a/builtin/game/async.lua b/builtin/game/async.lua new file mode 100644 index 000000000..469f179d7 --- /dev/null +++ b/builtin/game/async.lua @@ -0,0 +1,22 @@ + +core.async_jobs = {} + +function core.async_event_handler(jobid, retval) + local callback = core.async_jobs[jobid] + assert(type(callback) == "function") + callback(unpack(retval, 1, retval.n)) + core.async_jobs[jobid] = nil +end + +function core.handle_async(func, callback, ...) + assert(type(func) == "function" and type(callback) == "function", + "Invalid minetest.handle_async invocation") + local args = {n = select("#", ...), ...} + local mod_origin = core.get_last_run_mod() + + local jobid = core.do_async_callback(func, args, mod_origin) + core.async_jobs[jobid] = callback + + return true +end + diff --git a/builtin/game/init.lua b/builtin/game/init.lua index c5f08113b..68d6a10f8 100644 --- a/builtin/game/init.lua +++ b/builtin/game/init.lua @@ -34,5 +34,6 @@ dofile(gamepath .. "voxelarea.lua") dofile(gamepath .. "forceloading.lua") dofile(gamepath .. "statbars.lua") dofile(gamepath .. "knockback.lua") +dofile(gamepath .. "async.lua") profiler = nil diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index 18d5a7310..9f5e3312b 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -235,3 +235,32 @@ end -- Used for callback handling with dynamic_add_media core.dynamic_media_callbacks = {} + + +-- Transfer of certain globals into async environment +-- see builtin/async/game.lua for the other side + +local function copy_filtering(t, seen) + if type(t) == "userdata" or type(t) == "function" then + return true -- don't use nil so presence can still be detected + elseif type(t) ~= "table" then + return t + end + local n = {} + seen = seen or {} + seen[t] = n + for k, v in pairs(t) do + local k_ = seen[k] or copy_filtering(k, seen) + local v_ = seen[v] or copy_filtering(v, seen) + n[k_] = v_ + end + return n +end + +function core.get_globals_to_transfer() + local all = { + registered_items = copy_filtering(core.registered_items), + registered_aliases = core.registered_aliases, + } + return core.serialize(all) +end |