diff options
author | Anatoli Papirovski <apapirovski@mac.com> | 2018-02-11 16:35:59 -0500 |
---|---|---|
committer | Anatoli Papirovski <apapirovski@mac.com> | 2018-02-16 14:23:14 -0500 |
commit | e9ac80bb397293feef3b47f3ed609c86edb48681 (patch) | |
tree | 357523992d1b469f800e58853a3a79214633453e /lib/timers.js | |
parent | 7748865cd3322ff9421458ccc862291eb26cec62 (diff) | |
download | android-node-v8-e9ac80bb397293feef3b47f3ed609c86edb48681.tar.gz android-node-v8-e9ac80bb397293feef3b47f3ed609c86edb48681.tar.bz2 android-node-v8-e9ac80bb397293feef3b47f3ed609c86edb48681.zip |
async_hooks: clean up usage in internal code
Instead of exposing internals of async_hooks & async_wrap throughout
the code base, create necessary helper methods within the internal
async_hooks that allows easy usage by Node.js internals. This stops
every single internal user of async_hooks from importing a ton of
functions, constants and internal Aliased Buffers from C++ async_wrap.
Adds functions initHooksExist, afterHooksExist, and destroyHooksExist
to determine whether the related emit methods need to be triggered.
Adds clearDefaultTriggerAsyncId and clearAsyncIdStack on the JS side
as an alternative to always calling C++.
Moves async_id_symbol and trigger_async_id_symbol to internal
async_hooks as they are never used in C++.
Renames newUid to newAsyncId for added clarity of its purpose.
Adjusts usage throughout the codebase, as well as in a couple of tests.
PR-URL: https://github.com/nodejs/node/pull/18720
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'lib/timers.js')
-rw-r--r-- | lib/timers.js | 45 |
1 files changed, 20 insertions, 25 deletions
diff --git a/lib/timers.js b/lib/timers.js index 08ddf16785..145550b7b5 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -21,34 +21,34 @@ 'use strict'; -const async_wrap = process.binding('async_wrap'); const { Timer: TimerWrap, setupTimers, } = process.binding('timer_wrap'); const L = require('internal/linkedlist'); -const timerInternals = require('internal/timers'); +const { + async_id_symbol, + trigger_async_id_symbol, + Timeout, + validateTimerDuration +} = require('internal/timers'); const internalUtil = require('internal/util'); const { createPromise, promiseResolve } = process.binding('util'); const assert = require('assert'); const util = require('util'); const errors = require('internal/errors'); const debug = util.debuglog('timer'); -// Two arrays that share state between C++ and JS. -const { async_hook_fields, async_id_fields } = async_wrap; const { getDefaultTriggerAsyncId, + newAsyncId, + initHooksExist, + destroyHooksExist, // The needed emit*() functions. emitInit, emitBefore, emitAfter, emitDestroy } = require('internal/async_hooks'); -// Grab the constants necessary for working with internal arrays. -const { kInit, kDestroy, kAsyncIdCounter } = async_wrap.constants; -// Symbols for storing async id state. -const async_id_symbol = timerInternals.async_id_symbol; -const trigger_async_id_symbol = timerInternals.trigger_async_id_symbol; // *Must* match Environment::ImmediateInfo::Fields in src/env.h. const kCount = 0; @@ -60,10 +60,6 @@ const [immediateInfo, toggleImmediateRef] = const kRefed = Symbol('refed'); -// The Timeout class -const Timeout = timerInternals.Timeout; - - // HOW and WHY the timers implementation works the way it does. // // Timers are crucial to Node.js. Internally, any TCP I/O connection creates a @@ -192,9 +188,9 @@ function insert(item, unrefed, start) { if (!item[async_id_symbol] || item._destroyed) { item._destroyed = false; - item[async_id_symbol] = ++async_id_fields[kAsyncIdCounter]; + item[async_id_symbol] = newAsyncId(); item[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); - if (async_hook_fields[kInit] > 0) { + if (initHooksExist()) { emitInit(item[async_id_symbol], 'Timeout', item[trigger_async_id_symbol], @@ -255,7 +251,7 @@ function listOnTimeout(handle, now) { assert(timer !== L.peek(list)); if (!timer._onTimeout) { - if (async_hook_fields[kDestroy] > 0 && !timer._destroyed && + if (destroyHooksExist() && !timer._destroyed && typeof timer[async_id_symbol] === 'number') { emitDestroy(timer[async_id_symbol]); timer._destroyed = true; @@ -306,7 +302,7 @@ function tryOnTimeout(timer, start) { if (timerAsyncId !== null) { if (!threw) emitAfter(timerAsyncId); - if (!timer._repeat && async_hook_fields[kDestroy] > 0 && + if (!timer._repeat && destroyHooksExist() && !timer._destroyed) { emitDestroy(timerAsyncId); timer._destroyed = true; @@ -341,8 +337,7 @@ function reuse(item) { // Remove a timer. Cancels the timeout and resets the relevant timer properties. function unenroll(item) { // Fewer checks may be possible, but these cover everything. - if (async_hook_fields[kDestroy] > 0 && - item && + if (destroyHooksExist() && typeof item[async_id_symbol] === 'number' && !item._destroyed) { emitDestroy(item[async_id_symbol]); @@ -368,7 +363,7 @@ exports.unenroll = util.deprecate(unenroll, // This function does not start the timer, see `active()`. // Using existing objects as timers slightly reduces object overhead. function enroll(item, msecs) { - item._idleTimeout = timerInternals.validateTimerDuration(msecs); + item._idleTimeout = validateTimerDuration(msecs); // if this item was already in a list somewhere // then we should unenroll it from that @@ -574,7 +569,7 @@ Timeout.prototype.ref = function() { Timeout.prototype.close = function() { this._onTimeout = null; if (this._handle) { - if (async_hook_fields[kDestroy] > 0 && + if (destroyHooksExist() && typeof this[async_id_symbol] === 'number' && !this._destroyed) { emitDestroy(this[async_id_symbol]); @@ -684,7 +679,7 @@ function tryOnImmediate(immediate, oldTail, count, refCount) { } finally { immediate._onImmediate = null; - if (async_hook_fields[kDestroy] > 0) { + if (destroyHooksExist()) { emitDestroy(immediate[async_id_symbol]); } @@ -725,9 +720,9 @@ const Immediate = class Immediate { this._destroyed = false; this[kRefed] = false; - this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter]; + this[async_id_symbol] = newAsyncId(); this[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); - if (async_hook_fields[kInit] > 0) { + if (initHooksExist()) { emitInit(this[async_id_symbol], 'Immediate', this[trigger_async_id_symbol], @@ -807,7 +802,7 @@ exports.clearImmediate = function(immediate) { toggleImmediateRef(false); immediate[kRefed] = undefined; - if (async_hook_fields[kDestroy] > 0) { + if (destroyHooksExist()) { emitDestroy(immediate[async_id_symbol]); } |