util/timer.lua
author Matthew Wild <mwild1@gmail.com>
Sat, 21 Nov 2009 02:40:21 +0000
changeset 2095 3256c5d00901
parent 1871 838d1317bca4
child 2098 a1ad06f1c090
permissions -rw-r--r--
util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
     1
-- Prosody IM
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;
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    11
local event = require "net.server".event;
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    12
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    13
local get_time = os.time;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    14
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
    15
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
    16
local ipairs, pairs = ipairs, pairs;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    17
local type = type;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    18
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    19
local data = {};
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    20
local new_data = {};
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
module "timer"
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    23
2095
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    24
local _add_task;
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    25
if not event then
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    26
	function _add_task(delay, func)
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    27
		local current_time = get_time();
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    28
		delay = delay + current_time;
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    29
		if delay >= current_time then
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    30
			t_insert(new_data, {delay, func});
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    31
		else
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    32
			func();
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    33
		end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    34
	end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    35
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    36
	ns_addtimer(function()
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    37
		local current_time = get_time();
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    38
		if #new_data > 0 then
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    39
			for _, d in pairs(new_data) do
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    40
				t_insert(data, d);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    41
			end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    42
			new_data = {};
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    43
		end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    44
		
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    45
		for i, d in pairs(data) do
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    46
			local t, func = d[1], d[2];
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    47
			if t <= current_time then
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    48
				data[i] = nil;
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    49
				local r = func(current_time);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    50
				if type(r) == "number" then _add_task(r, func); end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    51
			end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    52
		end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    53
	end);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    54
else
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    55
	local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1;
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    56
	function _add_task(delay, func)
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    57
		event.base:addevent(nil, event.EV_TIMEOUT, function ()
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    58
			local ret = func();
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    59
			if ret then
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    60
				_add_task(ret, func);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    61
			else
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    62
				return EVENT_LEAVE;
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    63
			end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    64
		end
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    65
		, delay);
3256c5d00901 util.timer: Use libevent for lightweight timers if available and configured (use_libevent option)
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
    66
	end
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    67
end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    68
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    69
add_task = _add_task;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    70
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    71
return _M;