aboutsummaryrefslogtreecommitdiff
path: root/builtin/async_event.lua
diff options
context:
space:
mode:
authorsapier <Sapier at GMX dot net>2013-11-26 18:15:31 +0100
committersapier <Sapier at GMX dot net>2013-11-29 22:09:14 +0100
commit2e66aca35722e7fee786027d545fe371786fc01f (patch)
treef6d3ec721d23680bb493bd66054379b9327a1c7a /builtin/async_event.lua
parentb08d7558de53325d184b3ddf0476cb84fc08d0ad (diff)
downloadminetest-2e66aca35722e7fee786027d545fe371786fc01f.tar.gz
minetest-2e66aca35722e7fee786027d545fe371786fc01f.tar.bz2
minetest-2e66aca35722e7fee786027d545fe371786fc01f.zip
Fix modstore/favourites hang by adding asynchronous lua job support
Diffstat (limited to 'builtin/async_event.lua')
-rw-r--r--builtin/async_event.lua59
1 files changed, 59 insertions, 0 deletions
diff --git a/builtin/async_event.lua b/builtin/async_event.lua
new file mode 100644
index 000000000..f4c7d2449
--- /dev/null
+++ b/builtin/async_event.lua
@@ -0,0 +1,59 @@
+local tbl = engine or minetest
+
+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)
+end
+
+function tbl.handle_async(fct, parameters, callback)
+
+ --serialize fct
+ local serialized_fct = marshal.encode(fct)
+
+ assert(marshal.decode(serialized_fct) ~= nil)
+
+ --serialize parameters
+ local serialized_params = marshal.encode(parameters)
+
+ if serialized_fct == nil or
+ serialized_params == nil or
+ serialized_fct:len() == 0 or
+ serialized_params:len() == 0 then
+ return false
+ end
+
+ local jobid = tbl.do_async_callback( serialized_fct,
+ serialized_fct:len(),
+ serialized_params,
+ serialized_params:len())
+
+ tbl.async_jobs[jobid] = callback
+
+ return true
+end