Comparison

util/timer.lua @ 3683:c2158221e2e6

util.timer: Activate higher timer precision.
author Waqas Hussain <waqas20@gmail.com>
date Thu, 02 Dec 2010 22:41:26 +0500
parent 2964:49b5c87d2fa0
child 4385:c94167139f27
comparison
equal deleted inserted replaced
3682:d12141cbbaa0 3683:c2158221e2e6
9 9
10 local ns_addtimer = require "net.server".addtimer; 10 local ns_addtimer = require "net.server".addtimer;
11 local event = require "net.server".event; 11 local event = require "net.server".event;
12 local event_base = require "net.server".event_base; 12 local event_base = require "net.server".event_base;
13 13
14 local get_time = os.time; 14 local math_min = math.min
15 local math_huge = math.huge
16 local get_time = require "socket".gettime;
15 local t_insert = table.insert; 17 local t_insert = table.insert;
16 local t_remove = table.remove; 18 local t_remove = table.remove;
17 local ipairs, pairs = ipairs, pairs; 19 local ipairs, pairs = ipairs, pairs;
18 local type = type; 20 local type = type;
19 21
41 t_insert(data, d); 43 t_insert(data, d);
42 end 44 end
43 new_data = {}; 45 new_data = {};
44 end 46 end
45 47
48 local next_time = math_huge;
46 for i, d in pairs(data) do 49 for i, d in pairs(data) do
47 local t, func = d[1], d[2]; 50 local t, func = d[1], d[2];
48 if t <= current_time then 51 if t <= current_time then
49 data[i] = nil; 52 data[i] = nil;
50 local r = func(current_time); 53 local r = func(current_time);
51 if type(r) == "number" then _add_task(r, func); end 54 if type(r) == "number" then
55 _add_task(r, func);
56 next_time = math_min(next_time, r);
57 end
58 else
59 next_time = math_min(next_time, t - current_time);
52 end 60 end
53 end 61 end
62 return next_time;
54 end); 63 end);
55 else 64 else
56 local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1; 65 local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1;
57 function _add_task(delay, func) 66 function _add_task(delay, func)
58 local event_handle; 67 local event_handle;