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 math_min = math.min
13 local math_huge = math.huge
14 local get_time = require "socket".gettime;
15 local t_insert = table.insert;
16 local pairs = pairs;
17 local type = type;
18 local debug_traceback = debug.traceback;
19 local tostring = tostring;
20 local xpcall = xpcall;
21
22 local data = {};
23 local new_data = {};
24
25 module "timer"
26
27 local _add_task;
28 if not server.event then
29         function _add_task(delay, callback)
30                 local current_time = get_time();
31                 delay = delay + current_time;
32                 if delay >= current_time then
33                         t_insert(new_data, {delay, callback});
34                 else
35                         local r = callback(current_time);
36                         if r and type(r) == "number" then
37                                 return _add_task(r, callback);
38                         end
39                 end
40         end
41
42         server._addtimer(function()
43                 local current_time = get_time();
44                 if #new_data > 0 then
45                         for _, d in pairs(new_data) do
46                                 t_insert(data, d);
47                         end
48                         new_data = {};
49                 end
50
51                 local next_time = math_huge;
52                 for i, d in pairs(data) do
53                         local t, callback = d[1], d[2];
54                         if t <= current_time then
55                                 data[i] = nil;
56                                 local r = callback(current_time);
57                                 if type(r) == "number" then
58                                         _add_task(r, callback);
59                                         next_time = math_min(next_time, r);
60                                 end
61                         else
62                                 next_time = math_min(next_time, t - current_time);
63                         end
64                 end
65                 return next_time;
66         end);
67 else
68         local event = server.event;
69         local event_base = server.event_base;
70         local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1;
71
72         function _add_task(delay, callback)
73                 local event_handle;
74                 event_handle = event_base:addevent(nil, 0, function ()
75                         local ret = callback(get_time());
76                         if ret then
77                                 return 0, ret;
78                         elseif event_handle then
79                                 return EVENT_LEAVE;
80                         end
81                 end
82                 , delay);
83         end
84 end
85
86 --add_task = _add_task;
87
88 local h = indexedbheap.create();
89 local params = {};
90 local next_time = nil;
91 local _id, _callback, _now, _param;
92 local function _call() return _callback(_now, _id, _param); end
93 local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end
94 local function _on_timer(now)
95         local peek;
96         while true do
97                 peek = h:peek();
98                 if peek == nil or peek > now then break; end
99                 local _;
100                 _, _callback, _id = h:pop();
101                 _now = now;
102                 _param = params[_id];
103                 params[_id] = nil;
104                 --item(now, id, _param); -- FIXME pcall
105                 local success, err = xpcall(_call, _traceback_handler);
106                 if success and type(err) == "number" then
107                         h:insert(_callback, err + now, _id); -- re-add
108                         params[_id] = _param;
109                 end
110         end
111         next_time = peek;
112         if peek ~= nil then
113                 return peek - now;
114         end
115 end
116 function add_task(delay, callback, param)
117         local current_time = get_time();
118         local event_time = current_time + delay;
119
120         local id = h:insert(callback, event_time);
121         params[id] = param;
122         if next_time == nil or event_time < next_time then
123                 next_time = event_time;
124                 _add_task(next_time - current_time, _on_timer);
125         end
126         return id;
127 end
128 function stop(id)
129         params[id] = nil;
130         return h:remove(id);
131 end
132 function reschedule(id, delay)
133         local current_time = get_time();
134         local event_time = current_time + delay;
135         h:reprioritize(id, delay);
136         if next_time == nil or event_time < next_time then
137                 next_time = event_time;
138                 _add_task(next_time - current_time, _on_timer);
139         end
140         return id;
141 end
142
143 return _M;