Annotate

util/timer.lua @ 1494:bdfa5274e111

mod_uptime: Convert to unix line endings
author Matthew Wild <mwild1@gmail.com>
date Wed, 08 Jul 2009 04:22:15 +0100
parent 896:2c0b9e3c11c3
child 1523:841d61be198f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
896
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 842
diff changeset
1 -- Prosody IM v0.4
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local ns_addtimer = require "net.server".addtimer;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local get_time = os.time;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local t_insert = table.insert;
841
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
13 local t_remove = table.remove;
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
14 local ipairs, pairs = ipairs, pairs;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local type = type;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local data = {};
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local new_data = {};
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 module "timer"
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local function _add_task(delay, func)
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local current_time = get_time();
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 delay = delay + current_time;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 if delay >= current_time then
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 t_insert(new_data, {delay, func});
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 else func(); end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 add_task = _add_task;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 ns_addtimer(function()
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 local current_time = get_time();
841
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
34 if #new_data > 0 then
842
4932f94d35c1 util.timer: More small fixes I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 841
diff changeset
35 for _, d in pairs(new_data) do
841
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
36 t_insert(data, d);
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
37 end
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
38 new_data = {};
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 end
841
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
40
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
41 for i, d in pairs(data) do
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
42 local t, func = d[1], d[2];
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 if t <= current_time then
842
4932f94d35c1 util.timer: More small fixes I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 841
diff changeset
44 data[i] = nil;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 local r = func();
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 if type(r) == "number" then _add_task(r, func); end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 end);
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 return _M;