2011-03-10 09:54:52 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2011-06-08 04:35:50 +02:00
|
|
|
var Timer = process.binding('timer_wrap').Timer;
|
2011-01-18 23:26:32 +01:00
|
|
|
var L = require('_linklist');
|
2011-01-28 01:59:28 +01:00
|
|
|
var assert = require('assert').ok;
|
2010-10-26 20:56:32 +02:00
|
|
|
|
2011-07-12 00:30:24 +02:00
|
|
|
// Timeout values > TIMEOUT_MAX are set to 1.
|
|
|
|
var TIMEOUT_MAX = 2147483647; // 2^31-1
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
var debug;
|
2011-02-04 18:00:21 +01:00
|
|
|
if (process.env.NODE_DEBUG && /timer/.test(process.env.NODE_DEBUG)) {
|
2010-12-02 05:59:06 +01:00
|
|
|
debug = function() { require('util').error.apply(this, arguments); };
|
|
|
|
} else {
|
|
|
|
debug = function() { };
|
2010-10-26 20:56:32 +02:00
|
|
|
}
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
|
2011-01-13 11:09:03 +01:00
|
|
|
// IDLE TIMEOUTS
|
|
|
|
//
|
|
|
|
// Because often many sockets will have the same idle timeout we will not
|
|
|
|
// use one timeout watcher per item. It is too much overhead. Instead
|
|
|
|
// we'll use a single watcher for all sockets with the same timeout value
|
|
|
|
// and a linked list. This technique is described in the libev manual:
|
|
|
|
// http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Be_smart_about_timeouts
|
|
|
|
|
|
|
|
// Object containing all lists, timers
|
|
|
|
// key = time in milliseconds
|
|
|
|
// value = list
|
|
|
|
var lists = {};
|
2010-10-26 20:56:32 +02:00
|
|
|
|
|
|
|
// the main function - creates lists on demand and the watchers associated
|
|
|
|
// with them.
|
2010-12-02 05:59:06 +01:00
|
|
|
function insert(item, msecs) {
|
2012-07-10 19:39:41 +02:00
|
|
|
item._idleStart = Date.now();
|
2010-10-26 21:14:17 +02:00
|
|
|
item._idleTimeout = msecs;
|
2010-10-26 20:56:32 +02:00
|
|
|
|
2010-10-26 21:52:31 +02:00
|
|
|
if (msecs < 0) return;
|
2010-10-26 20:56:32 +02:00
|
|
|
|
|
|
|
var list;
|
|
|
|
|
|
|
|
if (lists[msecs]) {
|
|
|
|
list = lists[msecs];
|
|
|
|
} else {
|
|
|
|
list = new Timer();
|
2011-06-08 04:35:50 +02:00
|
|
|
list.start(msecs, 0);
|
|
|
|
|
2011-01-18 23:26:32 +01:00
|
|
|
L.init(list);
|
2010-10-26 20:56:32 +02:00
|
|
|
|
|
|
|
lists[msecs] = list;
|
|
|
|
|
2011-06-08 04:35:50 +02:00
|
|
|
list.ontimeout = function() {
|
2010-10-26 20:56:32 +02:00
|
|
|
debug('timeout callback ' + msecs);
|
2011-07-05 01:31:00 +02:00
|
|
|
|
2012-07-10 19:39:41 +02:00
|
|
|
var now = Date.now();
|
|
|
|
debug('now: ' + (new Date(now)));
|
2011-01-13 11:09:03 +01:00
|
|
|
|
2010-10-26 20:56:32 +02:00
|
|
|
var first;
|
2011-01-18 23:26:32 +01:00
|
|
|
while (first = L.peek(list)) {
|
2010-10-26 20:56:32 +02:00
|
|
|
var diff = now - first._idleStart;
|
2011-01-12 01:51:07 +01:00
|
|
|
if (diff + 1 < msecs) {
|
2011-07-05 01:31:00 +02:00
|
|
|
list.start(msecs - diff, 0);
|
2010-12-02 05:59:06 +01:00
|
|
|
debug(msecs + ' list wait because diff is ' + diff);
|
2010-10-26 20:56:32 +02:00
|
|
|
return;
|
|
|
|
} else {
|
2011-01-18 23:26:32 +01:00
|
|
|
L.remove(first);
|
|
|
|
assert(first !== L.peek(list));
|
2012-01-29 23:30:13 +01:00
|
|
|
|
|
|
|
if (!first._onTimeout) continue;
|
|
|
|
|
|
|
|
// v0.4 compatibility: if the timer callback throws and the user's
|
|
|
|
// uncaughtException handler ignores the exception, other timers that
|
|
|
|
// expire on this tick should still run. If #2582 goes through, this
|
|
|
|
// hack should be removed.
|
|
|
|
//
|
|
|
|
// https://github.com/joyent/node/issues/2631
|
2012-04-07 01:26:18 +02:00
|
|
|
if (first.domain) {
|
|
|
|
if (first.domain._disposed) continue;
|
|
|
|
first.domain.enter();
|
|
|
|
}
|
2012-01-29 23:30:13 +01:00
|
|
|
try {
|
|
|
|
first._onTimeout();
|
|
|
|
} catch (e) {
|
|
|
|
if (!process.listeners('uncaughtException').length) throw e;
|
|
|
|
process.emit('uncaughtException', e);
|
|
|
|
}
|
2012-04-07 01:26:18 +02:00
|
|
|
if (first.domain) first.domain.exit();
|
2010-10-26 20:56:32 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-13 11:09:03 +01:00
|
|
|
|
2010-10-26 20:56:32 +02:00
|
|
|
debug(msecs + ' list empty');
|
2011-01-18 23:26:32 +01:00
|
|
|
assert(L.isEmpty(list));
|
2011-06-08 04:35:50 +02:00
|
|
|
list.close();
|
|
|
|
delete lists[msecs];
|
2010-10-26 20:56:32 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-01-18 23:26:32 +01:00
|
|
|
L.append(list, item);
|
|
|
|
assert(!L.isEmpty(list)); // list is not empty
|
2010-10-26 20:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
var unenroll = exports.unenroll = function(item) {
|
2011-01-18 23:26:32 +01:00
|
|
|
L.remove(item);
|
2010-10-26 20:56:32 +02:00
|
|
|
|
2011-01-13 11:22:09 +01:00
|
|
|
var list = lists[item._idleTimeout];
|
|
|
|
// if empty then stop the watcher
|
|
|
|
debug('unenroll');
|
2011-01-18 23:26:32 +01:00
|
|
|
if (list && L.isEmpty(list)) {
|
2011-01-13 11:22:09 +01:00
|
|
|
debug('unenroll: list empty');
|
2011-06-08 04:35:50 +02:00
|
|
|
list.close();
|
|
|
|
delete lists[item._idleTimeout];
|
2010-10-26 20:56:32 +02:00
|
|
|
}
|
2011-12-22 14:42:20 +01:00
|
|
|
// if active is called later, then we want to make sure not to insert again
|
|
|
|
item._idleTimeout = -1;
|
2010-10-26 20:56:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Does not start the time, just sets up the members needed.
|
2010-12-02 05:59:06 +01:00
|
|
|
exports.enroll = function(item, msecs) {
|
2010-10-26 21:14:17 +02:00
|
|
|
// if this item was already in a list somewhere
|
2010-10-26 20:56:32 +02:00
|
|
|
// then we should unenroll it from that
|
2010-10-26 21:14:17 +02:00
|
|
|
if (item._idleNext) unenroll(item);
|
2010-10-26 20:56:32 +02:00
|
|
|
|
2010-10-26 21:14:17 +02:00
|
|
|
item._idleTimeout = msecs;
|
2011-01-18 23:26:32 +01:00
|
|
|
L.init(item);
|
2010-10-26 20:56:32 +02:00
|
|
|
};
|
|
|
|
|
2011-01-13 11:22:09 +01:00
|
|
|
|
2010-10-26 21:14:17 +02:00
|
|
|
// call this whenever the item is active (not idle)
|
2010-10-26 20:56:32 +02:00
|
|
|
// it will reset its timeout.
|
2010-12-02 05:59:06 +01:00
|
|
|
exports.active = function(item) {
|
2010-10-26 21:14:17 +02:00
|
|
|
var msecs = item._idleTimeout;
|
2010-10-26 21:52:31 +02:00
|
|
|
if (msecs >= 0) {
|
2012-07-13 04:19:01 +02:00
|
|
|
|
2010-10-26 20:56:32 +02:00
|
|
|
var list = lists[msecs];
|
2011-07-01 21:01:12 +02:00
|
|
|
if (!list || L.isEmpty(list)) {
|
2010-10-26 21:14:17 +02:00
|
|
|
insert(item, msecs);
|
2010-10-26 20:56:32 +02:00
|
|
|
} else {
|
2012-07-10 19:39:41 +02:00
|
|
|
item._idleStart = Date.now();
|
2011-01-18 23:26:32 +01:00
|
|
|
L.append(list, item);
|
2010-10-26 20:56:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2010-10-26 21:14:17 +02:00
|
|
|
|
|
|
|
|
2010-10-26 21:52:31 +02:00
|
|
|
/*
|
|
|
|
* DOM-style timers
|
|
|
|
*/
|
2010-10-26 21:14:17 +02:00
|
|
|
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
exports.setTimeout = function(callback, after) {
|
2011-11-12 13:31:26 +01:00
|
|
|
var timer;
|
2010-10-26 21:14:17 +02:00
|
|
|
|
2012-07-01 22:58:22 +02:00
|
|
|
after *= 1; // coalesce to number or NaN
|
|
|
|
|
|
|
|
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
|
2011-07-12 00:30:24 +02:00
|
|
|
after = 1; // schedule on next tick, follows browser behaviour
|
|
|
|
}
|
2011-06-08 04:35:50 +02:00
|
|
|
|
2012-07-13 04:19:01 +02:00
|
|
|
timer = new Timeout(after);
|
2011-06-08 04:35:50 +02:00
|
|
|
|
2011-07-12 00:30:24 +02:00
|
|
|
if (arguments.length <= 2) {
|
|
|
|
timer._onTimeout = callback;
|
2010-10-26 21:52:31 +02:00
|
|
|
} else {
|
2011-07-12 00:30:24 +02:00
|
|
|
/*
|
|
|
|
* Sometimes setTimeout is called with arguments, EG
|
|
|
|
*
|
|
|
|
* setTimeout(callback, 2000, "hello", "world")
|
|
|
|
*
|
|
|
|
* If that's the case we need to call the callback with
|
|
|
|
* those args. The overhead of an extra closure is not
|
|
|
|
* desired in the normal case.
|
|
|
|
*/
|
|
|
|
var args = Array.prototype.slice.call(arguments, 2);
|
|
|
|
timer._onTimeout = function() {
|
|
|
|
callback.apply(timer, args);
|
2010-10-26 21:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
2010-10-26 21:14:17 +02:00
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
if (process.domain) timer.domain = process.domain;
|
|
|
|
|
2011-07-12 00:30:24 +02:00
|
|
|
exports.active(timer);
|
|
|
|
|
2010-10-26 21:14:17 +02:00
|
|
|
return timer;
|
|
|
|
};
|
|
|
|
|
2010-10-26 21:52:31 +02:00
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
exports.clearTimeout = function(timer) {
|
2011-06-08 04:35:50 +02:00
|
|
|
if (timer && (timer.ontimeout || timer._onTimeout)) {
|
|
|
|
timer.ontimeout = timer._onTimeout = null;
|
2012-07-13 04:19:01 +02:00
|
|
|
if (timer instanceof Timer || timer instanceof Timeout) {
|
2011-06-08 04:35:50 +02:00
|
|
|
timer.close(); // for after === 0
|
|
|
|
} else {
|
|
|
|
exports.unenroll(timer);
|
|
|
|
}
|
2010-10-29 09:00:43 +02:00
|
|
|
}
|
2010-10-26 21:52:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
exports.setInterval = function(callback, repeat) {
|
2010-10-26 21:14:17 +02:00
|
|
|
var timer = new Timer();
|
2010-10-26 21:52:31 +02:00
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
if (process.domain) timer.domain = process.domain;
|
|
|
|
|
2012-07-01 22:58:22 +02:00
|
|
|
repeat *= 1; // coalesce to number or NaN
|
|
|
|
|
|
|
|
if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) {
|
2011-07-12 00:30:24 +02:00
|
|
|
repeat = 1; // schedule on next tick, follows browser behaviour
|
|
|
|
}
|
|
|
|
|
2011-06-08 04:35:50 +02:00
|
|
|
var args = Array.prototype.slice.call(arguments, 2);
|
|
|
|
timer.ontimeout = function() {
|
|
|
|
callback.apply(timer, args);
|
2010-10-26 21:52:31 +02:00
|
|
|
}
|
|
|
|
|
2011-07-12 00:30:24 +02:00
|
|
|
timer.start(repeat, repeat);
|
2010-10-26 21:14:17 +02:00
|
|
|
return timer;
|
|
|
|
};
|
|
|
|
|
2010-10-26 21:52:31 +02:00
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
exports.clearInterval = function(timer) {
|
2010-10-26 21:14:17 +02:00
|
|
|
if (timer instanceof Timer) {
|
2011-06-08 04:35:50 +02:00
|
|
|
timer.ontimeout = null;
|
|
|
|
timer.close();
|
2010-10-26 21:14:17 +02:00
|
|
|
}
|
|
|
|
};
|
2012-07-13 04:19:01 +02:00
|
|
|
|
|
|
|
var Timeout = function(after) {
|
|
|
|
this._idleTimeout = after;
|
|
|
|
this._idlePrev = this;
|
|
|
|
this._idleNext = this;
|
|
|
|
this._when = Date.now() + after;
|
|
|
|
};
|
|
|
|
|
|
|
|
Timeout.prototype.unref = function() {
|
|
|
|
if (!this._handle) {
|
|
|
|
exports.unenroll(this);
|
|
|
|
this._handle = new Timer();
|
|
|
|
this._handle.ontimeout = this._onTimeout;
|
|
|
|
this._handle.start(this._when - Date.now(), 0);
|
|
|
|
this._handle.unref();
|
|
|
|
} else {
|
|
|
|
this._handle.unref();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Timeout.prototype.ref = function() {
|
|
|
|
if (this._handle)
|
|
|
|
this._handle.ref();
|
|
|
|
};
|
|
|
|
|
|
|
|
Timeout.prototype.close = function() {
|
|
|
|
this._onTimeout = null;
|
|
|
|
if (this._handle) {
|
|
|
|
this._handle.ontimeout = null;
|
|
|
|
this._handle.close();
|
|
|
|
} else {
|
|
|
|
exports.unenroll(this);
|
|
|
|
}
|
|
|
|
};
|