timers: improvements to TimersList management

Move all the TimersList instantiation code into the constructor.

Compare values to undefined & null as appropriate instead of
truthy or falsy.

PR-URL: https://github.com/nodejs/node/pull/17429
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Anatoli Papirovski 2017-11-29 12:46:13 -05:00
parent a413df40b6
commit e55b7d6286
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0

View File

@ -55,7 +55,7 @@ const activateImmediateCheck = process._activateImmediateCheck;
delete process._activateImmediateCheck;
// Timeout values > TIMEOUT_MAX are set to 1.
const TIMEOUT_MAX = 2147483647; // 2^31-1
const TIMEOUT_MAX = 2 ** 31 - 1;
// HOW and WHY the timers implementation works the way it does.
@ -173,9 +173,9 @@ function insert(item, unrefed) {
// Use an existing list if there is one, otherwise we need to make a new one.
var list = lists[msecs];
if (!list) {
if (list === undefined) {
debug('no %d list was found in insert, creating a new one', msecs);
lists[msecs] = list = createTimersList(msecs, unrefed);
lists[msecs] = list = new TimersList(msecs, unrefed);
}
if (!item[async_id_symbol] || item._destroyed) {
@ -194,28 +194,21 @@ function insert(item, unrefed) {
assert(!L.isEmpty(list)); // list is not empty
}
function createTimersList(msecs, unrefed) {
// Make a new linked list of timers, and create a TimerWrap to schedule
// processing for the list.
const list = new TimersList(msecs, unrefed);
L.init(list);
list._timer._list = list;
if (unrefed === true) list._timer.unref();
list._timer.start(msecs);
list._timer[kOnTimeout] = listOnTimeout;
return list;
}
function TimersList(msecs, unrefed) {
this._idleNext = null; // Create the list with the linkedlist properties to
this._idlePrev = null; // prevent any unnecessary hidden class changes.
this._timer = new TimerWrap();
this._idleNext = this; // Create the list with the linkedlist properties to
this._idlePrev = this; // prevent any unnecessary hidden class changes.
this._unrefed = unrefed;
this.msecs = msecs;
this.nextTick = false;
const timer = this._timer = new TimerWrap();
timer._list = this;
if (unrefed === true)
timer.unref();
timer.start(msecs);
timer[kOnTimeout] = listOnTimeout;
}
function listOnTimeout() {
@ -359,7 +352,7 @@ function reuse(item) {
var list = refedLists[item._idleTimeout];
// if empty - reuse the watcher
if (list && L.isEmpty(list)) {
if (list !== undefined && L.isEmpty(list)) {
debug('reuse hit');
list._timer.stop();
delete refedLists[item._idleTimeout];
@ -382,7 +375,7 @@ const unenroll = exports.unenroll = function(item) {
}
var handle = reuse(item);
if (handle) {
if (handle !== null) {
debug('unenroll: list empty');
handle.close();
}
@ -610,7 +603,7 @@ Timeout.prototype.unref = function() {
}
var handle = reuse(this);
if (handle) {
if (handle !== null) {
handle._list = undefined;
}
@ -659,7 +652,7 @@ function ImmediateList() {
// Appends an item to the end of the linked list, adjusting the current tail's
// previous and next pointers where applicable
ImmediateList.prototype.append = function(item) {
if (this.tail) {
if (this.tail !== null) {
this.tail._idleNext = item;
item._idlePrev = this.tail;
} else {
@ -671,11 +664,11 @@ ImmediateList.prototype.append = function(item) {
// Removes an item from the linked list, adjusting the pointers of adjacent
// items and the linked list's head or tail pointers as necessary
ImmediateList.prototype.remove = function(item) {
if (item._idleNext) {
if (item._idleNext !== null) {
item._idleNext._idlePrev = item._idlePrev;
}
if (item._idlePrev) {
if (item._idlePrev !== null) {
item._idlePrev._idleNext = item._idleNext;
}
@ -701,7 +694,7 @@ function processImmediate() {
// immediate callbacks are executed
immediateQueue.head = immediateQueue.tail = null;
while (immediate) {
while (immediate !== null) {
domain = immediate.domain;
if (!immediate._onImmediate) {
@ -722,7 +715,7 @@ function processImmediate() {
// If `clearImmediate(immediate)` wasn't called from the callback, use the
// `immediate`'s next item
if (immediate._idleNext)
if (immediate._idleNext !== null)
immediate = immediate._idleNext;
else
immediate = next;
@ -754,11 +747,11 @@ function tryOnImmediate(immediate, oldTail) {
}
}
if (threw && immediate._idleNext) {
if (threw && immediate._idleNext !== null) {
// Handle any remaining on next tick, assuming we're still alive to do so.
const curHead = immediateQueue.head;
const next = immediate._idleNext;
if (curHead) {
if (curHead !== null) {
curHead._idlePrev = oldTail;
oldTail._idleNext = curHead;
next._idlePrev = null;