Comparison

util/timer.lua @ 11264:2cdcf55c6dd5

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Fri, 08 Jan 2021 23:56:27 +0100
parent 10981:e6c1e92cc7a7
parent 11263:1274deeab39a
child 12975:d10957394a3c
comparison
equal deleted inserted replaced
11260:08b397c21805 11264:2cdcf55c6dd5
13 local type = type; 13 local type = type;
14 local debug_traceback = debug.traceback; 14 local debug_traceback = debug.traceback;
15 local tostring = tostring; 15 local tostring = tostring;
16 local xpcall = require "util.xpcall".xpcall; 16 local xpcall = require "util.xpcall".xpcall;
17 local math_max = math.max; 17 local math_max = math.max;
18 local pairs = pairs;
18 19
19 if server.timer then 20 if server.timer then
20 -- The selected net.server implements this API, so defer to that 21 -- The selected net.server implements this API, so defer to that
21 return server.timer; 22 return server.timer;
22 end 23 end
32 local params = {}; 33 local params = {};
33 local next_time = nil; 34 local next_time = nil;
34 local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end 35 local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end
35 local function _on_timer(now) 36 local function _on_timer(now)
36 local peek; 37 local peek;
38 local readd;
37 while true do 39 while true do
38 peek = h:peek(); 40 peek = h:peek();
39 if peek == nil or peek > now then break; end 41 if peek == nil or peek > now then break; end
40 local _, callback, id = h:pop(); 42 local _, callback, id = h:pop();
41 local param = params[id]; 43 local param = params[id];
42 params[id] = nil; 44 params[id] = nil;
43 --item(now, id, _param); 45 --item(now, id, _param);
44 local success, err = xpcall(callback, _traceback_handler, now, id, param); 46 local success, err = xpcall(callback, _traceback_handler, now, id, param);
45 if success and type(err) == "number" then 47 if success and type(err) == "number" then
46 h:insert(callback, err + now, id); -- re-add 48 if readd then
49 readd[id] = { callback, err + now };
50 else
51 readd = { [id] = { callback, err + now } };
52 end
47 params[id] = param; 53 params[id] = param;
48 end 54 end
55 end
56
57 if readd then
58 for id,timer in pairs(readd) do
59 h:insert(timer[1], timer[2], id);
60 end
61 peek = h:peek();
49 end 62 end
50 63
51 if peek ~= nil and _active_timers > 1 and peek == next_time then 64 if peek ~= nil and _active_timers > 1 and peek == next_time then
52 -- Another instance of _on_timer already set next_time to the same value, 65 -- Another instance of _on_timer already set next_time to the same value,
53 -- so it should be safe to not renew this timer event 66 -- so it should be safe to not renew this timer event