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 _server_timer;
23 local _active_timers = 0;
24 local h = indexedbheap.create();
25 local params = {};
26 local next_time = nil;
27 local _id, _callback, _now, _param;
28 local function _call() return _callback(_now, _id, _param); end
29 local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end
30 local function _on_timer(now)
31         local peek;
32         while true do
33                 peek = h:peek();
34                 if peek == nil or peek > now then break; end
35                 local _;
36                 _, _callback, _id = h:pop();
37                 _now = now;
38                 _param = params[_id];
39                 params[_id] = nil;
40                 --item(now, id, _param); -- FIXME pcall
41                 local success, err = xpcall(_call, _traceback_handler);
42                 if success and type(err) == "number" then
43                         h:insert(_callback, err + now, _id); -- re-add
44                         params[_id] = _param;
45                 end
46         end
47
48         if peek ~= nil and _active_timers > 1 and peek == next_time then
49                 -- Another instance of _on_timer already set next_time to the same value,
50                 -- so it should be safe to not renew this timer event
51                 peek = nil;
52         else
53                 next_time = peek;
54         end
55
56         if peek then
57                 -- peek is the time of the next event
58                 return peek - now;
59         end
60         _active_timers = _active_timers - 1;
61 end
62 local function add_task(delay, callback, param)
63         local current_time = get_time();
64         local event_time = current_time + delay;
65
66         local id = h:insert(callback, event_time);
67         params[id] = param;
68         if next_time == nil or event_time < next_time then
69                 next_time = event_time;
70                 if _server_timer then
71                         _server_timer:close();
72                         _server_timer = nil;
73                 else
74                         _active_timers = _active_timers + 1;
75                 end
76                 _server_timer = _add_task(next_time - current_time, _on_timer);
77         end
78         return id;
79 end
80 local function stop(id)
81         params[id] = nil;
82         local result, item, result_sync = h:remove(id);
83         local peek = h:peek();
84         if peek ~= next_time and _server_timer then
85                 next_time = peek;
86                 _server_timer:close();
87                 if next_time ~= nil then
88                         _server_timer = _add_task(next_time - get_time(), _on_timer);
89                 end
90         end
91         return result, item, result_sync;
92 end
93 local function reschedule(id, delay)
94         local current_time = get_time();
95         local event_time = current_time + delay;
96         h:reprioritize(id, delay);
97         if next_time == nil or event_time < next_time then
98                 next_time = event_time;
99                 _add_task(next_time - current_time, _on_timer);
100         end
101         return id;
102 end
103
104 return {
105         add_task = add_task;
106         stop = stop;
107         reschedule = reschedule;
108 };
109