Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fix modstore/favourites hang by adding asynchronous lua job support
- Loading branch information
sapier
authored and
sapier
committed
Nov 29, 2013
1 parent
b08d755
commit 2e66aca
Showing
27 changed files
with
2,271 additions
and
559 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
engine.log("info","Initializing Asynchronous environment") | ||
|
||
dofile(SCRIPTDIR .. DIR_DELIM .. "misc_helpers.lua") | ||
|
||
function engine.job_processor(serialized_function, serialized_data) | ||
|
||
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) | ||
else | ||
engine.log("error","ASYNC WORKER: unable to deserialize function") | ||
end | ||
|
||
return retval,retval:len() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.