Comparison

util/timer.lua @ 6408:7e69d61a0ef7

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 11 Sep 2014 01:17:56 +0200
parent 5898:bf9aba718c01
child 6481:dbc72cd1332e
comparison
equal deleted inserted replaced
6407:4bbd198cf3e6 6408:7e69d61a0ef7
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local indexedbheap = require "util.indexedbheap";
10 local log = require "util.logger".init("timer");
9 local server = require "net.server"; 11 local server = require "net.server";
10 local math_min = math.min 12 local math_min = math.min
11 local math_huge = math.huge 13 local math_huge = math.huge
12 local get_time = require "socket".gettime; 14 local get_time = require "socket".gettime;
13 local t_insert = table.insert; 15 local t_insert = table.insert;
14 local pairs = pairs; 16 local pairs = pairs;
15 local type = type; 17 local type = type;
18 local debug_traceback = debug.traceback;
19 local tostring = tostring;
20 local xpcall = xpcall;
16 21
17 local data = {}; 22 local data = {};
18 local new_data = {}; 23 local new_data = {};
19 24
20 module "timer" 25 module "timer"
76 end 81 end
77 , delay); 82 , delay);
78 end 83 end
79 end 84 end
80 85
81 add_task = _add_task; 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
82 142
83 return _M; 143 return _M;