util/timer.lua
author Kim Alvefur <zash@zash.se>
Wed, 27 Mar 2024 19:33:11 +0100
changeset 13471 c2a476f4712a
parent 12979 d10957394a3c
permissions -rw-r--r--
util.startup: Fix exiting on pidfile trouble prosody.shutdown() relies on prosody.main_thread, which has not been set yet at this point. Doing a clean shutdown might actually be harmful in case it tears down things set up by the conflicting Prosody, such as the very pidfile we were looking at. Thanks again SigmaTel71 for noticing
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
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1871
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4871
diff changeset
     4
--
832
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
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 11268
diff changeset
     9
local indexedbheap = require "prosody.util.indexedbheap";
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 11268
diff changeset
    10
local log = require "prosody.util.logger".init("timer");
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 11268
diff changeset
    11
local server = require "prosody.net.server";
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 11268
diff changeset
    12
local get_time = require "prosody.util.time".now
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    13
local type = type;
5880
11f14d44438e util.timer: Import all require upvalues.
Waqas Hussain <waqas20@gmail.com>
parents: 5879
diff changeset
    14
local debug_traceback = debug.traceback;
11f14d44438e util.timer: Import all require upvalues.
Waqas Hussain <waqas20@gmail.com>
parents: 5879
diff changeset
    15
local tostring = tostring;
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 11268
diff changeset
    16
local xpcall = require "prosody.util.xpcall".xpcall;
8765
2f8523bf7ff2 util.timer: Ensure we don't try to schedule negative timeouts (which rightly upset libevent). Fixes #1135
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
    17
local math_max = math.max;
11267
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    18
local pairs = pairs;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    19
10985
e6c1e92cc7a7 util.timer: Defer to selected net.server if it implements this API
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    20
if server.timer then
e6c1e92cc7a7 util.timer: Defer to selected net.server if it implements this API
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    21
	-- The selected net.server implements this API, so defer to that
e6c1e92cc7a7 util.timer: Defer to selected net.server if it implements this API
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    22
	return server.timer;
e6c1e92cc7a7 util.timer: Defer to selected net.server if it implements this API
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    23
end
e6c1e92cc7a7 util.timer: Defer to selected net.server if it implements this API
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    24
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
    25
local _ENV = nil;
8558
4f0f5b49bb03 vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7992
diff changeset
    26
-- luacheck: std none
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    27
6484
dbc72cd1332e Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents: 5898
diff changeset
    28
local _add_task = server.add_task;
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    29
6935
f5fd2c5cdf28 util.timer: If possible, close the existing timer handle in order to have only one
Kim Alvefur <zash@zash.se>
parents: 6934
diff changeset
    30
local _server_timer;
6933
58e260832334 util.timer: Keep count of how many timer instances are active
Kim Alvefur <zash@zash.se>
parents: 6839
diff changeset
    31
local _active_timers = 0;
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    32
local h = indexedbheap.create();
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    33
local params = {};
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    34
local next_time = nil;
5880
11f14d44438e util.timer: Import all require upvalues.
Waqas Hussain <waqas20@gmail.com>
parents: 5879
diff changeset
    35
local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    36
local function _on_timer(now)
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    37
	local peek;
11267
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    38
	local readd;
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    39
	while true do
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    40
		peek = h:peek();
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    41
		if peek == nil or peek > now then break; end
9565
acf74ad0b795 Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents: 8998
diff changeset
    42
		local _, callback, id = h:pop();
acf74ad0b795 Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents: 8998
diff changeset
    43
		local param = params[id];
acf74ad0b795 Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents: 8998
diff changeset
    44
		params[id] = nil;
acf74ad0b795 Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents: 8998
diff changeset
    45
		--item(now, id, _param);
acf74ad0b795 Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents: 8998
diff changeset
    46
		local success, err = xpcall(callback, _traceback_handler, now, id, param);
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    47
		if success and type(err) == "number" then
11267
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    48
			if readd then
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    49
				readd[id] = { callback, err + now };
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    50
			else
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    51
				readd = { [id] = { callback, err + now } };
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    52
			end
9565
acf74ad0b795 Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents: 8998
diff changeset
    53
			params[id] = param;
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
    54
		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
    55
	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
    56
11267
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    57
	if readd then
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    58
		for id,timer in pairs(readd) do
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    59
			h:insert(timer[1], timer[2], id);
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    60
		end
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    61
		peek = h:peek();
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    62
	end
1274deeab39a util.timer: Ensure timers can't run more than once per tick (fixes #1620)
Kim Alvefur <zash@zash.se>
parents: 9565
diff changeset
    63
6934
5c2c8aeb4690 util.timer: Expire timer instance if another instance is already set to take care of the next scheduled event
Kim Alvefur <zash@zash.se>
parents: 6933
diff changeset
    64
	if peek ~= nil and _active_timers > 1 and peek == next_time then
5c2c8aeb4690 util.timer: Expire timer instance if another instance is already set to take care of the next scheduled event
Kim Alvefur <zash@zash.se>
parents: 6933
diff changeset
    65
		-- Another instance of _on_timer already set next_time to the same value,
5c2c8aeb4690 util.timer: Expire timer instance if another instance is already set to take care of the next scheduled event
Kim Alvefur <zash@zash.se>
parents: 6933
diff changeset
    66
		-- so it should be safe to not renew this timer event
5c2c8aeb4690 util.timer: Expire timer instance if another instance is already set to take care of the next scheduled event
Kim Alvefur <zash@zash.se>
parents: 6933
diff changeset
    67
		peek = nil;
5c2c8aeb4690 util.timer: Expire timer instance if another instance is already set to take care of the next scheduled event
Kim Alvefur <zash@zash.se>
parents: 6933
diff changeset
    68
	else
5c2c8aeb4690 util.timer: Expire timer instance if another instance is already set to take care of the next scheduled event
Kim Alvefur <zash@zash.se>
parents: 6933
diff changeset
    69
		next_time = peek;
5c2c8aeb4690 util.timer: Expire timer instance if another instance is already set to take care of the next scheduled event
Kim Alvefur <zash@zash.se>
parents: 6933
diff changeset
    70
	end
5c2c8aeb4690 util.timer: Expire timer instance if another instance is already set to take care of the next scheduled event
Kim Alvefur <zash@zash.se>
parents: 6933
diff changeset
    71
5c2c8aeb4690 util.timer: Expire timer instance if another instance is already set to take care of the next scheduled event
Kim Alvefur <zash@zash.se>
parents: 6933
diff changeset
    72
	if peek then
5c2c8aeb4690 util.timer: Expire timer instance if another instance is already set to take care of the next scheduled event
Kim Alvefur <zash@zash.se>
parents: 6933
diff changeset
    73
		-- peek is the time of the next event
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    74
		return peek - now;
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
    75
	end
6933
58e260832334 util.timer: Keep count of how many timer instances are active
Kim Alvefur <zash@zash.se>
parents: 6839
diff changeset
    76
	_active_timers = _active_timers - 1;
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    77
end
6794
e813e8cf6046 Merge 0.10->trunk
Kim Alvefur <zash@zash.se>
parents: 6484 6780
diff changeset
    78
local function add_task(delay, callback, param)
6839
9f45f0fe5aef util.timer: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 6794
diff changeset
    79
	local current_time = get_time();
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    80
	local event_time = current_time + delay;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4871
diff changeset
    81
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    82
	local id = h:insert(callback, event_time);
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    83
	params[id] = param;
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    84
	if next_time == nil or event_time < next_time then
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    85
		next_time = event_time;
6935
f5fd2c5cdf28 util.timer: If possible, close the existing timer handle in order to have only one
Kim Alvefur <zash@zash.se>
parents: 6934
diff changeset
    86
		if _server_timer then
f5fd2c5cdf28 util.timer: If possible, close the existing timer handle in order to have only one
Kim Alvefur <zash@zash.se>
parents: 6934
diff changeset
    87
			_server_timer:close();
f5fd2c5cdf28 util.timer: If possible, close the existing timer handle in order to have only one
Kim Alvefur <zash@zash.se>
parents: 6934
diff changeset
    88
			_server_timer = nil;
f5fd2c5cdf28 util.timer: If possible, close the existing timer handle in order to have only one
Kim Alvefur <zash@zash.se>
parents: 6934
diff changeset
    89
		else
f5fd2c5cdf28 util.timer: If possible, close the existing timer handle in order to have only one
Kim Alvefur <zash@zash.se>
parents: 6934
diff changeset
    90
			_active_timers = _active_timers + 1;
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
    91
		end
6935
f5fd2c5cdf28 util.timer: If possible, close the existing timer handle in order to have only one
Kim Alvefur <zash@zash.se>
parents: 6934
diff changeset
    92
		_server_timer = _add_task(next_time - current_time, _on_timer);
6839
9f45f0fe5aef util.timer: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 6794
diff changeset
    93
	end
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    94
	return id;
6839
9f45f0fe5aef util.timer: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 6794
diff changeset
    95
end
6794
e813e8cf6046 Merge 0.10->trunk
Kim Alvefur <zash@zash.se>
parents: 6484 6780
diff changeset
    96
local function stop(id)
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
    97
	params[id] = nil;
7018
17e275e8bd79 util.timer: Reschedule timers from stop() if the next pending event is stopped
Kim Alvefur <zash@zash.se>
parents: 6935
diff changeset
    98
	local result, item, result_sync = h:remove(id);
17e275e8bd79 util.timer: Reschedule timers from stop() if the next pending event is stopped
Kim Alvefur <zash@zash.se>
parents: 6935
diff changeset
    99
	local peek = h:peek();
17e275e8bd79 util.timer: Reschedule timers from stop() if the next pending event is stopped
Kim Alvefur <zash@zash.se>
parents: 6935
diff changeset
   100
	if peek ~= next_time and _server_timer then
17e275e8bd79 util.timer: Reschedule timers from stop() if the next pending event is stopped
Kim Alvefur <zash@zash.se>
parents: 6935
diff changeset
   101
		next_time = peek;
17e275e8bd79 util.timer: Reschedule timers from stop() if the next pending event is stopped
Kim Alvefur <zash@zash.se>
parents: 6935
diff changeset
   102
		_server_timer:close();
17e275e8bd79 util.timer: Reschedule timers from stop() if the next pending event is stopped
Kim Alvefur <zash@zash.se>
parents: 6935
diff changeset
   103
		if next_time ~= nil then
8765
2f8523bf7ff2 util.timer: Ensure we don't try to schedule negative timeouts (which rightly upset libevent). Fixes #1135
Matthew Wild <mwild1@gmail.com>
parents: 8688
diff changeset
   104
			_server_timer = _add_task(math_max(next_time - get_time(), 0), _on_timer);
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
   105
		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
   106
	end
7018
17e275e8bd79 util.timer: Reschedule timers from stop() if the next pending event is stopped
Kim Alvefur <zash@zash.se>
parents: 6935
diff changeset
   107
	return result, item, result_sync;
6839
9f45f0fe5aef util.timer: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 6794
diff changeset
   108
end
6794
e813e8cf6046 Merge 0.10->trunk
Kim Alvefur <zash@zash.se>
parents: 6484 6780
diff changeset
   109
local function reschedule(id, delay)
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
   110
	local current_time = get_time();
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
   111
	local event_time = current_time + delay;
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
   112
	h:reprioritize(id, delay);
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
   113
	if next_time == nil or event_time < next_time then
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
   114
		next_time = event_time;
5878
2b1c0c0a2ea6 util.timer: Fix variable name typo.
Waqas Hussain <waqas20@gmail.com>
parents: 5877
diff changeset
   115
		_add_task(next_time - current_time, _on_timer);
6839
9f45f0fe5aef util.timer: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 6794
diff changeset
   116
	end
5877
615a0774e4cc util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
Waqas Hussain <waqas20@gmail.com>
parents: 5776
diff changeset
   117
	return id;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   118
end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   119
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   120
return {
6794
e813e8cf6046 Merge 0.10->trunk
Kim Alvefur <zash@zash.se>
parents: 6484 6780
diff changeset
   121
	add_task = add_task;
e813e8cf6046 Merge 0.10->trunk
Kim Alvefur <zash@zash.se>
parents: 6484 6780
diff changeset
   122
	stop = stop;
e813e8cf6046 Merge 0.10->trunk
Kim Alvefur <zash@zash.se>
parents: 6484 6780
diff changeset
   123
	reschedule = reschedule;
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   124
};
6794
e813e8cf6046 Merge 0.10->trunk
Kim Alvefur <zash@zash.se>
parents: 6484 6780
diff changeset
   125