Mercurial > prosody-hg
comparison util/timer.lua @ 9562:acf74ad0b795
Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 26 Oct 2018 19:32:00 +0100 |
| parents | 2e6f7ab97794 |
| children | e6c1e92cc7a7 1274deeab39a |
comparison
equal
deleted
inserted
replaced
| 9561:cfc7b2f7251e | 9562:acf74ad0b795 |
|---|---|
| 11 local server = require "net.server"; | 11 local server = require "net.server"; |
| 12 local get_time = require "util.time".now | 12 local get_time = require "util.time".now |
| 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 = xpcall; | 16 local xpcall = require "util.xpcall".xpcall; |
| 17 local math_max = math.max; | 17 local math_max = math.max; |
| 18 | 18 |
| 19 local _ENV = nil; | 19 local _ENV = nil; |
| 20 -- luacheck: std none | 20 -- luacheck: std none |
| 21 | 21 |
| 24 local _server_timer; | 24 local _server_timer; |
| 25 local _active_timers = 0; | 25 local _active_timers = 0; |
| 26 local h = indexedbheap.create(); | 26 local h = indexedbheap.create(); |
| 27 local params = {}; | 27 local params = {}; |
| 28 local next_time = nil; | 28 local next_time = nil; |
| 29 local _id, _callback, _now, _param; | |
| 30 local function _call() return _callback(_now, _id, _param); end | |
| 31 local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end | 29 local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end |
| 32 local function _on_timer(now) | 30 local function _on_timer(now) |
| 33 local peek; | 31 local peek; |
| 34 while true do | 32 while true do |
| 35 peek = h:peek(); | 33 peek = h:peek(); |
| 36 if peek == nil or peek > now then break; end | 34 if peek == nil or peek > now then break; end |
| 37 local _; | 35 local _, callback, id = h:pop(); |
| 38 _, _callback, _id = h:pop(); | 36 local param = params[id]; |
| 39 _now = now; | 37 params[id] = nil; |
| 40 _param = params[_id]; | 38 --item(now, id, _param); |
| 41 params[_id] = nil; | 39 local success, err = xpcall(callback, _traceback_handler, now, id, param); |
| 42 --item(now, id, _param); -- FIXME pcall | |
| 43 local success, err = xpcall(_call, _traceback_handler); | |
| 44 if success and type(err) == "number" then | 40 if success and type(err) == "number" then |
| 45 h:insert(_callback, err + now, _id); -- re-add | 41 h:insert(callback, err + now, id); -- re-add |
| 46 params[_id] = _param; | 42 params[id] = param; |
| 47 end | 43 end |
| 48 end | 44 end |
| 49 | 45 |
| 50 if peek ~= nil and _active_timers > 1 and peek == next_time then | 46 if peek ~= nil and _active_timers > 1 and peek == next_time then |
| 51 -- Another instance of _on_timer already set next_time to the same value, | 47 -- Another instance of _on_timer already set next_time to the same value, |
