mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
5.7 KiB
225 lines
5.7 KiB
14 years ago
|
// 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.
|
||
|
|
||
14 years ago
|
var Timer = process.binding('timer').Timer;
|
||
14 years ago
|
var L = require('_linklist');
|
||
14 years ago
|
var assert = require('assert').ok;
|
||
14 years ago
|
|
||
14 years ago
|
var debug;
|
||
14 years ago
|
if (process.env.NODE_DEBUG && /timer/.test(process.env.NODE_DEBUG)) {
|
||
14 years ago
|
debug = function() { require('util').error.apply(this, arguments); };
|
||
|
} else {
|
||
|
debug = function() { };
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
|
||
14 years ago
|
// 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 = {};
|
||
14 years ago
|
|
||
|
|
||
|
// the main function - creates lists on demand and the watchers associated
|
||
|
// with them.
|
||
14 years ago
|
function insert(item, msecs) {
|
||
14 years ago
|
item._idleStart = new Date();
|
||
|
item._idleTimeout = msecs;
|
||
14 years ago
|
|
||
14 years ago
|
if (msecs < 0) return;
|
||
14 years ago
|
|
||
|
var list;
|
||
|
|
||
|
if (lists[msecs]) {
|
||
|
list = lists[msecs];
|
||
|
} else {
|
||
|
list = new Timer();
|
||
14 years ago
|
L.init(list);
|
||
14 years ago
|
|
||
|
lists[msecs] = list;
|
||
|
|
||
14 years ago
|
list.callback = function() {
|
||
14 years ago
|
debug('timeout callback ' + msecs);
|
||
|
// TODO - don't stop and start the watcher all the time.
|
||
|
// just set its repeat
|
||
|
var now = new Date();
|
||
14 years ago
|
debug('now: ' + now);
|
||
14 years ago
|
|
||
14 years ago
|
var first;
|
||
14 years ago
|
while (first = L.peek(list)) {
|
||
14 years ago
|
var diff = now - first._idleStart;
|
||
14 years ago
|
if (diff + 1 < msecs) {
|
||
14 years ago
|
list.again(msecs - diff);
|
||
14 years ago
|
debug(msecs + ' list wait because diff is ' + diff);
|
||
14 years ago
|
return;
|
||
|
} else {
|
||
14 years ago
|
L.remove(first);
|
||
|
assert(first !== L.peek(list));
|
||
14 years ago
|
if (first._onTimeout) first._onTimeout();
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
debug(msecs + ' list empty');
|
||
14 years ago
|
assert(L.isEmpty(list));
|
||
14 years ago
|
list.stop();
|
||
14 years ago
|
};
|
||
|
}
|
||
|
|
||
14 years ago
|
if (L.isEmpty(list)) {
|
||
|
// if empty (re)start the timer
|
||
|
list.again(msecs);
|
||
|
}
|
||
|
|
||
14 years ago
|
L.append(list, item);
|
||
|
assert(!L.isEmpty(list)); // list is not empty
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
14 years ago
|
var unenroll = exports.unenroll = function(item) {
|
||
14 years ago
|
L.remove(item);
|
||
14 years ago
|
|
||
14 years ago
|
var list = lists[item._idleTimeout];
|
||
|
// if empty then stop the watcher
|
||
|
debug('unenroll');
|
||
14 years ago
|
if (list && L.isEmpty(list)) {
|
||
14 years ago
|
debug('unenroll: list empty');
|
||
14 years ago
|
list.stop();
|
||
14 years ago
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
// Does not start the time, just sets up the members needed.
|
||
14 years ago
|
exports.enroll = function(item, msecs) {
|
||
14 years ago
|
// if this item was already in a list somewhere
|
||
14 years ago
|
// then we should unenroll it from that
|
||
14 years ago
|
if (item._idleNext) unenroll(item);
|
||
14 years ago
|
|
||
14 years ago
|
item._idleTimeout = msecs;
|
||
14 years ago
|
L.init(item);
|
||
14 years ago
|
};
|
||
|
|
||
14 years ago
|
|
||
14 years ago
|
// call this whenever the item is active (not idle)
|
||
14 years ago
|
// it will reset its timeout.
|
||
14 years ago
|
exports.active = function(item) {
|
||
14 years ago
|
var msecs = item._idleTimeout;
|
||
14 years ago
|
if (msecs >= 0) {
|
||
14 years ago
|
var list = lists[msecs];
|
||
14 years ago
|
if (item._idleNext == item) {
|
||
|
insert(item, msecs);
|
||
14 years ago
|
} else {
|
||
14 years ago
|
item._idleStart = new Date();
|
||
14 years ago
|
L.append(list, item);
|
||
14 years ago
|
}
|
||
|
}
|
||
|
};
|
||
14 years ago
|
|
||
|
|
||
14 years ago
|
/*
|
||
|
* DOM-style timers
|
||
|
*/
|
||
14 years ago
|
|
||
|
|
||
14 years ago
|
exports.setTimeout = function(callback, after) {
|
||
14 years ago
|
var timer;
|
||
14 years ago
|
|
||
14 years ago
|
if (after <= 0) {
|
||
|
// Use the slow case for after == 0
|
||
|
timer = new Timer();
|
||
14 years ago
|
timer.callback = callback;
|
||
14 years ago
|
} else {
|
||
14 years ago
|
timer = { _idleTimeout: after, _onTimeout: callback };
|
||
14 years ago
|
timer._idlePrev = timer;
|
||
|
timer._idleNext = timer;
|
||
14 years ago
|
}
|
||
|
|
||
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
if (arguments.length > 2) {
|
||
|
var args = Array.prototype.slice.call(arguments, 2);
|
||
|
var c = function() {
|
||
|
callback.apply(timer, args);
|
||
|
};
|
||
14 years ago
|
|
||
14 years ago
|
if (timer instanceof Timer) {
|
||
|
timer.callback = c;
|
||
14 years ago
|
} else {
|
||
14 years ago
|
timer._onTimeout = c;
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
14 years ago
|
if (timer instanceof Timer) {
|
||
|
timer.start(0, 0);
|
||
|
} else {
|
||
14 years ago
|
exports.active(timer);
|
||
|
}
|
||
14 years ago
|
|
||
|
return timer;
|
||
|
};
|
||
|
|
||
14 years ago
|
|
||
14 years ago
|
exports.clearTimeout = function(timer) {
|
||
14 years ago
|
if (timer && (timer.callback || timer._onTimeout)) {
|
||
|
timer.callback = timer._onTimeout = null;
|
||
|
exports.unenroll(timer);
|
||
|
if (timer instanceof Timer) timer.stop(); // for after === 0
|
||
14 years ago
|
}
|
||
14 years ago
|
};
|
||
|
|
||
|
|
||
14 years ago
|
exports.setInterval = function(callback, repeat) {
|
||
14 years ago
|
var timer = new Timer();
|
||
14 years ago
|
|
||
14 years ago
|
if (arguments.length > 2) {
|
||
|
var args = Array.prototype.slice.call(arguments, 2);
|
||
|
timer.callback = function() {
|
||
|
callback.apply(timer, args);
|
||
|
};
|
||
|
} else {
|
||
|
timer.callback = callback;
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
timer.start(repeat, repeat ? repeat : 1);
|
||
|
return timer;
|
||
|
};
|
||
|
|
||
14 years ago
|
|
||
14 years ago
|
exports.clearInterval = function(timer) {
|
||
14 years ago
|
if (timer instanceof Timer) {
|
||
14 years ago
|
timer.callback = null;
|
||
|
timer.stop();
|
||
14 years ago
|
}
|
||
|
};
|