Software /
code /
prosody
Comparison
util/timer.lua @ 6791:e813e8cf6046
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 20 Aug 2015 13:05:22 +0200 |
parent | 6481:dbc72cd1332e |
parent | 6777:5de6b93d0190 |
child | 6836:9f45f0fe5aef |
comparison
equal
deleted
inserted
replaced
6776:4412a2307c89 | 6791:e813e8cf6046 |
---|---|
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 = xpcall; |
17 | 17 |
18 module "timer" | 18 local _ENV = nil; |
19 | 19 |
20 local _add_task = server.add_task; | 20 local _add_task = server.add_task; |
21 --add_task = _add_task; | |
22 | 21 |
23 local h = indexedbheap.create(); | 22 local h = indexedbheap.create(); |
24 local params = {}; | 23 local params = {}; |
25 local next_time = nil; | 24 local next_time = nil; |
26 local _id, _callback, _now, _param; | 25 local _id, _callback, _now, _param; |
39 --item(now, id, _param); -- FIXME pcall | 38 --item(now, id, _param); -- FIXME pcall |
40 local success, err = xpcall(_call, _traceback_handler); | 39 local success, err = xpcall(_call, _traceback_handler); |
41 if success and type(err) == "number" then | 40 if success and type(err) == "number" then |
42 h:insert(_callback, err + now, _id); -- re-add | 41 h:insert(_callback, err + now, _id); -- re-add |
43 params[_id] = _param; | 42 params[_id] = _param; |
43 end | |
44 end | 44 end |
45 end | |
46 next_time = peek; | 45 next_time = peek; |
47 if peek ~= nil then | 46 if peek ~= nil then |
48 return peek - now; | 47 return peek - now; |
49 end | 48 end |
50 end | 49 end |
51 function add_task(delay, callback, param) | 50 local function add_task(delay, callback, param) |
52 local current_time = get_time(); | 51 local current_time = get_time(); |
53 local event_time = current_time + delay; | 52 local event_time = current_time + delay; |
54 | 53 |
55 local id = h:insert(callback, event_time); | 54 local id = h:insert(callback, event_time); |
56 params[id] = param; | 55 params[id] = param; |
57 if next_time == nil or event_time < next_time then | 56 if next_time == nil or event_time < next_time then |
58 next_time = event_time; | 57 next_time = event_time; |
59 _add_task(next_time - current_time, _on_timer); | 58 _add_task(next_time - current_time, _on_timer); |
60 end | 59 end |
61 return id; | 60 return id; |
62 end | 61 end |
63 function stop(id) | 62 local function stop(id) |
64 params[id] = nil; | 63 params[id] = nil; |
65 return h:remove(id); | 64 return h:remove(id); |
66 end | 65 end |
67 function reschedule(id, delay) | 66 local function reschedule(id, delay) |
68 local current_time = get_time(); | 67 local current_time = get_time(); |
69 local event_time = current_time + delay; | 68 local event_time = current_time + delay; |
70 h:reprioritize(id, delay); | 69 h:reprioritize(id, delay); |
71 if next_time == nil or event_time < next_time then | 70 if next_time == nil or event_time < next_time then |
72 next_time = event_time; | 71 next_time = event_time; |
73 _add_task(next_time - current_time, _on_timer); | 72 _add_task(next_time - current_time, _on_timer); |
74 end | 73 end |
75 return id; | 74 return id; |
76 end | 75 end |
77 | 76 |
78 return _M; | 77 return { |
78 add_task = add_task; | |
79 stop = stop; | |
80 reschedule = reschedule; | |
81 }; | |
82 |