Remove dependency on marshal and many other async changes
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
This commit is contained in:
parent
87e7000acc
commit
cc78050568
|
@ -1,19 +1,21 @@
|
|||
engine.log("info","Initializing Asynchronous environment")
|
||||
engine.log("info", "Initializing Asynchronous environment")
|
||||
local tbl = engine or minetest
|
||||
|
||||
minetest = tbl
|
||||
dofile(SCRIPTDIR .. DIR_DELIM .. "serialize.lua")
|
||||
dofile(SCRIPTDIR .. DIR_DELIM .. "misc_helpers.lua")
|
||||
|
||||
function engine.job_processor(serialized_function, serialized_data)
|
||||
function tbl.job_processor(serialized_func, serialized_param)
|
||||
local func = loadstring(serialized_func)
|
||||
local param = tbl.deserialize(serialized_param)
|
||||
local retval = nil
|
||||
|
||||
local fct = marshal.decode(serialized_function)
|
||||
local params = marshal.decode(serialized_data)
|
||||
local retval = marshal.encode(nil)
|
||||
|
||||
if fct ~= nil and type(fct) == "function" then
|
||||
local result = fct(params)
|
||||
retval = marshal.encode(result)
|
||||
if type(func) == "function" then
|
||||
retval = tbl.serialize(func(param))
|
||||
else
|
||||
engine.log("error","ASYNC WORKER: unable to deserialize function")
|
||||
tbl.log("error", "ASYNC WORKER: Unable to deserialize function")
|
||||
end
|
||||
|
||||
return retval,retval:len()
|
||||
return retval or tbl.serialize(nil)
|
||||
end
|
||||
|
||||
|
|
|
@ -1,59 +1,45 @@
|
|||
local tbl = engine or minetest
|
||||
|
||||
local SCRIPTDIR = SCRIPTDIR or tbl.get_scriptdir()
|
||||
minetest = tbl
|
||||
dofile(SCRIPTDIR .. DIR_DELIM .. "serialize.lua")
|
||||
|
||||
tbl.async_jobs = {}
|
||||
|
||||
if engine ~= nil then
|
||||
function tbl.async_event_handler(jobid, serialized_retval)
|
||||
local retval = nil
|
||||
if serialized_retval ~= "ERROR" then
|
||||
retval= marshal.decode(serialized_retval)
|
||||
else
|
||||
tbl.log("error","Error fetching async result")
|
||||
end
|
||||
|
||||
assert(type(tbl.async_jobs[jobid]) == "function")
|
||||
tbl.async_jobs[jobid](retval)
|
||||
tbl.async_jobs[jobid] = nil
|
||||
end
|
||||
else
|
||||
|
||||
minetest.register_globalstep(
|
||||
function(dtime)
|
||||
local list = tbl.get_finished_jobs()
|
||||
|
||||
for i=1,#list,1 do
|
||||
local retval = marshal.decode(list[i].retval)
|
||||
|
||||
assert(type(tbl.async_jobs[jobid]) == "function")
|
||||
tbl.async_jobs[list[i].jobid](retval)
|
||||
tbl.async_jobs[list[i].jobid] = nil
|
||||
end
|
||||
end)
|
||||
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
|
||||
|
||||
function tbl.handle_async(fct, parameters, callback)
|
||||
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
|
||||
|
||||
--serialize fct
|
||||
local serialized_fct = marshal.encode(fct)
|
||||
function tbl.handle_async(func, parameter, callback)
|
||||
-- Serialize function
|
||||
local serialized_func = string.dump(func)
|
||||
|
||||
assert(marshal.decode(serialized_fct) ~= nil)
|
||||
assert(serialized_func ~= nil)
|
||||
|
||||
--serialize parameters
|
||||
local serialized_params = marshal.encode(parameters)
|
||||
-- Serialize parameters
|
||||
local serialized_param = tbl.serialize(parameter)
|
||||
|
||||
if serialized_fct == nil or
|
||||
serialized_params == nil or
|
||||
serialized_fct:len() == 0 or
|
||||
serialized_params:len() == 0 then
|
||||
if serialized_param == nil then
|
||||
return false
|
||||
end
|
||||
|
||||
local jobid = tbl.do_async_callback( serialized_fct,
|
||||
serialized_fct:len(),
|
||||
serialized_params,
|
||||
serialized_params:len())
|
||||
local jobid = tbl.do_async_callback(serialized_func, serialized_param)
|
||||
|
||||
tbl.async_jobs[jobid] = callback
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue