mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01:00
6ab3b4c838
This makes a number of changes: * Remove the dependency on marshal by using string.dump and loadstring. * Use lua_tolstring rather than having Lua functions pass string lengths to C++. * Move lua_api/l_async_events.* to cpp_api/s_async.*, where it belongs. * Make AsyncWorkerThread a child of ScriptApiBase, this removes some duplicate functionality. * Don't wait for async threads to shut down. (Is this safe? Might result in corruption if the thread is writing to a file.) * Pop more unused items from the stack * Code style fixes * Other misc changes
46 lines
1015 B
Lua
46 lines
1015 B
Lua
local tbl = engine or minetest
|
|
|
|
local SCRIPTDIR = SCRIPTDIR or tbl.get_scriptdir()
|
|
minetest = tbl
|
|
dofile(SCRIPTDIR .. DIR_DELIM .. "serialize.lua")
|
|
|
|
tbl.async_jobs = {}
|
|
|
|
local function handle_job(jobid, serialized_retval)
|
|
local retval = tbl.deserialize(serialized_retval)
|
|
assert(type(tbl.async_jobs[jobid]) == "function")
|
|
tbl.async_jobs[jobid](retval)
|
|
tbl.async_jobs[jobid] = nil
|
|
end
|
|
|
|
if engine ~= nil then
|
|
tbl.async_event_handler = handle_job
|
|
else
|
|
minetest.register_globalstep(function(dtime)
|
|
for i, job in ipairs(tbl.get_finished_jobs()) do
|
|
handle_job(job.jobid, job.retval)
|
|
end
|
|
end)
|
|
end
|
|
|
|
function tbl.handle_async(func, parameter, callback)
|
|
-- Serialize function
|
|
local serialized_func = string.dump(func)
|
|
|
|
assert(serialized_func ~= nil)
|
|
|
|
-- Serialize parameters
|
|
local serialized_param = tbl.serialize(parameter)
|
|
|
|
if serialized_param == nil then
|
|
return false
|
|
end
|
|
|
|
local jobid = tbl.do_async_callback(serialized_func, serialized_param)
|
|
|
|
tbl.async_jobs[jobid] = callback
|
|
|
|
return true
|
|
end
|
|
|