Merge 0.10->trunk
[prosody.git] / util / timer.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local indexedbheap = require "util.indexedbheap";
10 local log = require "util.logger".init("timer");
11 local server = require "net.server";
12 local get_time = require "socket".gettime;
13 local type = type;
14 local debug_traceback = debug.traceback;
15 local tostring = tostring;
16 local xpcall = xpcall;
17
18 local _ENV = nil;
19
20 local _add_task = server.add_task;
21
22 local h = indexedbheap.create();
23 local params = {};
24 local next_time = nil;
25 local _id, _callback, _now, _param;
26 local function _call() return _callback(_now, _id, _param); end
27 local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end
28 local function _on_timer(now)
29         local peek;
30         while true do
31                 peek = h:peek();
32                 if peek == nil or peek > now then break; end
33                 local _;
34                 _, _callback, _id = h:pop();
35                 _now = now;
36                 _param = params[_id];
37                 params[_id] = nil;
38                 --item(now, id, _param); -- FIXME pcall
39                 local success, err = xpcall(_call, _traceback_handler);
40                 if success and type(err) == "number" then
41                         h:insert(_callback, err + now, _id); -- re-add
42                         params[_id] = _param;
43                 end
44         end
45         next_time = peek;
46         if peek ~= nil then
47                 return peek - now;
48         end
49 end
50 local function add_task(delay, callback, param)
51         local current_time = get_time();
52         local event_time = current_time + delay;
53
54         local id = h:insert(callback, event_time);
55         params[id] = param;
56         if next_time == nil or event_time < next_time then
57                 next_time = event_time;
58                 _add_task(next_time - current_time, _on_timer);
59         end
60         return id;
61 end
62 local function stop(id)
63         params[id] = nil;
64         return h:remove(id);
65 end
66 local function reschedule(id, delay)
67         local current_time = get_time();
68         local event_time = current_time + delay;
69         h:reprioritize(id, delay);
70         if next_time == nil or event_time < next_time then
71                 next_time = event_time;
72                 _add_task(next_time - current_time, _on_timer);
73         end
74         return id;
75 end
76
77 return {
78         add_task = add_task;
79         stop = stop;
80         reschedule = reschedule;
81 };
82