summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAnatoli Papirovski <apapirovski@mac.com>2018-02-11 16:35:59 -0500
committerAnatoli Papirovski <apapirovski@mac.com>2018-02-16 14:23:14 -0500
commite9ac80bb397293feef3b47f3ed609c86edb48681 (patch)
tree357523992d1b469f800e58853a3a79214633453e /lib
parent7748865cd3322ff9421458ccc862291eb26cec62 (diff)
downloadandroid-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')
-rw-r--r--lib/_http_agent.js2
-rw-r--r--lib/_http_outgoing.js2
-rw-r--r--lib/async_hooks.js23
-rw-r--r--lib/dgram.js6
-rw-r--r--lib/internal/async_hooks.js60
-rw-r--r--lib/internal/bootstrap_node.js26
-rw-r--r--lib/internal/http2/core.js2
-rw-r--r--lib/internal/process/next_tick.js27
-rw-r--r--lib/internal/timers.js12
-rw-r--r--lib/net.js9
-rw-r--r--lib/timers.js45
11 files changed, 125 insertions, 89 deletions
diff --git a/lib/_http_agent.js b/lib/_http_agent.js
index 5f1e56caea..13abe9c210 100644
--- a/lib/_http_agent.js
+++ b/lib/_http_agent.js
@@ -25,7 +25,7 @@ const net = require('net');
const util = require('util');
const EventEmitter = require('events');
const debug = util.debuglog('http');
-const { async_id_symbol } = process.binding('async_wrap');
+const { async_id_symbol } = require('internal/async_hooks').symbols;
const { nextTick } = require('internal/process/next_tick');
// New Agent code.
diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js
index 425cfbcc29..f513315b7c 100644
--- a/lib/_http_outgoing.js
+++ b/lib/_http_outgoing.js
@@ -31,7 +31,7 @@ const common = require('_http_common');
const checkIsHttpToken = common._checkIsHttpToken;
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
const { outHeadersKey } = require('internal/http');
-const { async_id_symbol } = process.binding('async_wrap');
+const { async_id_symbol } = require('internal/async_hooks').symbols;
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
diff --git a/lib/async_hooks.js b/lib/async_hooks.js
index cc9e893885..babb705aad 100644
--- a/lib/async_hooks.js
+++ b/lib/async_hooks.js
@@ -9,12 +9,14 @@ const internal_async_hooks = require('internal/async_hooks');
// resource gets gced.
const { registerDestroyHook } = async_wrap;
const {
+ executionAsyncId,
+ triggerAsyncId,
// Private API
getHookArrays,
enableHooks,
disableHooks,
// Internal Embedder API
- newUid,
+ newAsyncId,
getDefaultTriggerAsyncId,
emitInit,
emitBefore,
@@ -22,21 +24,16 @@ const {
emitDestroy,
} = internal_async_hooks;
-// Get fields
-const { async_id_fields } = async_wrap;
-
// Get symbols
const {
+ async_id_symbol, trigger_async_id_symbol,
init_symbol, before_symbol, after_symbol, destroy_symbol,
promise_resolve_symbol
} = internal_async_hooks.symbols;
-const { async_id_symbol, trigger_async_id_symbol } = async_wrap;
-
// Get constants
const {
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
- kExecutionAsyncId, kTriggerAsyncId
} = async_wrap.constants;
// Listener API //
@@ -125,16 +122,6 @@ function createHook(fns) {
}
-function executionAsyncId() {
- return async_id_fields[kExecutionAsyncId];
-}
-
-
-function triggerAsyncId() {
- return async_id_fields[kTriggerAsyncId];
-}
-
-
// Embedder API //
const destroyedSymbol = Symbol('destroyed');
@@ -170,7 +157,7 @@ class AsyncResource {
triggerAsyncId);
}
- this[async_id_symbol] = newUid();
+ this[async_id_symbol] = newAsyncId();
this[trigger_async_id_symbol] = triggerAsyncId;
// this prop name (destroyed) has to be synchronized with C++
this[destroyedSymbol] = { destroyed: false };
diff --git a/lib/dgram.js b/lib/dgram.js
index bed6129b47..a31384a586 100644
--- a/lib/dgram.js
+++ b/lib/dgram.js
@@ -28,9 +28,11 @@ const dns = require('dns');
const util = require('util');
const { isUint8Array } = require('internal/util/types');
const EventEmitter = require('events');
-const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
+const {
+ defaultTriggerAsyncIdScope,
+ symbols: { async_id_symbol }
+} = require('internal/async_hooks');
const { UV_UDP_REUSEADDR } = process.binding('constants').os;
-const { async_id_symbol } = process.binding('async_wrap');
const { nextTick } = require('internal/process/next_tick');
const { UDP, SendWrap } = process.binding('udp_wrap');
diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js
index d1310ad2ca..dc720a2230 100644
--- a/lib/internal/async_hooks.js
+++ b/lib/internal/async_hooks.js
@@ -27,7 +27,7 @@ const async_wrap = process.binding('async_wrap');
* It has a fixed size, so if that is exceeded, calls to the native
* side are used instead in pushAsyncIds() and popAsyncIds().
*/
-const { async_id_symbol, async_hook_fields, async_id_fields } = async_wrap;
+const { async_hook_fields, async_id_fields } = async_wrap;
// Store the pair executionAsyncId and triggerAsyncId in a std::stack on
// Environment::AsyncHooks::async_ids_stack_ tracks the resource responsible for
// the current execution stack. This is unwound as each resource exits. In the
@@ -71,6 +71,8 @@ const { kInit, kBefore, kAfter, kDestroy, kPromiseResolve,
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
// Used in AsyncHook and AsyncResource.
+const async_id_symbol = Symbol('asyncId');
+const trigger_async_id_symbol = Symbol('triggerAsyncId');
const init_symbol = Symbol('init');
const before_symbol = Symbol('before');
const after_symbol = Symbol('after');
@@ -245,7 +247,7 @@ function disableHooks() {
// Increment the internal id counter and return the value. Important that the
// counter increment first. Since it's done the same way in
// Environment::new_async_uid()
-function newUid() {
+function newAsyncId() {
return ++async_id_fields[kAsyncIdCounter];
}
@@ -254,7 +256,7 @@ function getOrSetAsyncId(object) {
return object[async_id_symbol];
}
- return object[async_id_symbol] = newUid();
+ return object[async_id_symbol] = newAsyncId();
}
@@ -270,6 +272,11 @@ function getDefaultTriggerAsyncId() {
}
+function clearDefaultTriggerAsyncId() {
+ async_id_fields[kDefaultTriggerAsyncId] = -1;
+}
+
+
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
// CHECK(Number.isSafeInteger(triggerAsyncId))
// CHECK(triggerAsyncId > 0)
@@ -287,6 +294,19 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
}
+function initHooksExist() {
+ return async_hook_fields[kInit] > 0;
+}
+
+function afterHooksExist() {
+ return async_hook_fields[kAfter] > 0;
+}
+
+function destroyHooksExist() {
+ return async_hook_fields[kDestroy] > 0;
+}
+
+
function emitInitScript(asyncId, type, triggerAsyncId, resource) {
validateAsyncId(asyncId, 'asyncId');
if (triggerAsyncId !== null)
@@ -345,6 +365,20 @@ function emitDestroyScript(asyncId) {
}
+// Keep in sync with Environment::AsyncHooks::clear_async_id_stack
+// in src/env-inl.h.
+function clearAsyncIdStack() {
+ async_id_fields[kExecutionAsyncId] = 0;
+ async_id_fields[kTriggerAsyncId] = 0;
+ async_hook_fields[kStackLength] = 0;
+}
+
+
+function hasAsyncIdStack() {
+ return async_hook_fields[kStackLength] > 0;
+}
+
+
// This is the equivalent of the native push_async_ids() call.
function pushAsyncIds(asyncId, triggerAsyncId) {
const offset = async_hook_fields[kStackLength];
@@ -377,20 +411,38 @@ function popAsyncIds(asyncId) {
}
+function executionAsyncId() {
+ return async_id_fields[kExecutionAsyncId];
+}
+
+function triggerAsyncId() {
+ return async_id_fields[kTriggerAsyncId];
+}
+
+
module.exports = {
+ executionAsyncId,
+ triggerAsyncId,
// Private API
getHookArrays,
symbols: {
+ async_id_symbol, trigger_async_id_symbol,
init_symbol, before_symbol, after_symbol, destroy_symbol,
promise_resolve_symbol
},
enableHooks,
disableHooks,
+ clearDefaultTriggerAsyncId,
+ clearAsyncIdStack,
+ hasAsyncIdStack,
// Internal Embedder API
- newUid,
+ newAsyncId,
getOrSetAsyncId,
getDefaultTriggerAsyncId,
defaultTriggerAsyncIdScope,
+ initHooksExist,
+ afterHooksExist,
+ destroyHooksExist,
emitInit: emitInitScript,
emitBefore: emitBeforeScript,
emitAfter: emitAfterScript,
diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js
index 24a4e8e2da..f85e557d5e 100644
--- a/lib/internal/bootstrap_node.js
+++ b/lib/internal/bootstrap_node.js
@@ -431,18 +431,19 @@
function noop() {}
function setupProcessFatal() {
- const async_wrap = process.binding('async_wrap');
- // Arrays containing hook flags and ids for async_hook calls.
- const { async_hook_fields, async_id_fields } = async_wrap;
- // Internal functions needed to manipulate the stack.
- const { clearAsyncIdStack } = async_wrap;
- const { kAfter, kExecutionAsyncId,
- kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
+ const {
+ executionAsyncId,
+ clearDefaultTriggerAsyncId,
+ clearAsyncIdStack,
+ hasAsyncIdStack,
+ afterHooksExist,
+ emitAfter
+ } = NativeModule.require('internal/async_hooks');
process._fatalException = function(er) {
- // It's possible that kDefaultTriggerAsyncId was set for a constructor
+ // It's possible that defaultTriggerAsyncId was set for a constructor
// call that threw and was never cleared. So clear it now.
- async_id_fields[kDefaultTriggerAsyncId] = -1;
+ clearDefaultTriggerAsyncId();
if (exceptionHandlerState.captureFn !== null) {
exceptionHandlerState.captureFn(er);
@@ -465,11 +466,10 @@
NativeModule.require('timers').setImmediate(noop);
// Emit the after() hooks now that the exception has been handled.
- if (async_hook_fields[kAfter] > 0) {
- const { emitAfter } = NativeModule.require('internal/async_hooks');
+ if (afterHooksExist()) {
do {
- emitAfter(async_id_fields[kExecutionAsyncId]);
- } while (async_hook_fields[kStackLength] > 0);
+ emitAfter(executionAsyncId());
+ } while (hasAsyncIdStack());
// Or completely empty the id stack.
} else {
clearAsyncIdStack();
diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js
index dc01fb14e6..b7a56850a3 100644
--- a/lib/internal/http2/core.js
+++ b/lib/internal/http2/core.js
@@ -4,7 +4,7 @@
require('internal/util').assertCrypto();
-const { async_id_symbol } = process.binding('async_wrap');
+const { async_id_symbol } = require('internal/async_hooks').symbols;
const http = require('http');
const binding = process.binding('http2');
const assert = require('assert');
diff --git a/lib/internal/process/next_tick.js b/lib/internal/process/next_tick.js
index 84a7402117..f95d9cc1b8 100644
--- a/lib/internal/process/next_tick.js
+++ b/lib/internal/process/next_tick.js
@@ -5,19 +5,20 @@ exports.setup = setupNextTick;
exports.nextTick = null;
function setupNextTick() {
- const async_wrap = process.binding('async_wrap');
- const async_hooks = require('internal/async_hooks');
+ const {
+ getDefaultTriggerAsyncId,
+ newAsyncId,
+ initHooksExist,
+ destroyHooksExist,
+ emitInit,
+ emitBefore,
+ emitAfter,
+ emitDestroy,
+ symbols: { async_id_symbol, trigger_async_id_symbol }
+ } = require('internal/async_hooks');
const promises = require('internal/process/promises');
const errors = require('internal/errors');
const { emitPromiseRejectionWarnings } = promises;
- const getDefaultTriggerAsyncId = async_hooks.getDefaultTriggerAsyncId;
- // Two arrays that share state between C++ and JS.
- const { async_hook_fields, async_id_fields } = async_wrap;
- // Used to change the state of the async id stack.
- const { emitInit, emitBefore, emitAfter, emitDestroy } = async_hooks;
- // Grab the constants necessary for working with internal arrays.
- const { kInit, kDestroy, kAsyncIdCounter } = async_wrap.constants;
- const { async_id_symbol, trigger_async_id_symbol } = async_wrap;
// tickInfo is used so that the C++ code in src/node.cc can
// have easy access to our nextTick state, and avoid unnecessary
@@ -104,7 +105,7 @@ function setupNextTick() {
// that nextTick() doesn't allow the event loop to proceed, but if
// any async hooks are enabled during the callback's execution then
// this tock's after hook will be called, but not its destroy hook.
- if (async_hook_fields[kDestroy] > 0)
+ if (destroyHooksExist())
emitDestroy(asyncId);
const callback = tock.callback;
@@ -128,11 +129,11 @@ function setupNextTick() {
this.callback = callback;
this.args = args;
- const asyncId = ++async_id_fields[kAsyncIdCounter];
+ const asyncId = newAsyncId();
this[async_id_symbol] = asyncId;
this[trigger_async_id_symbol] = triggerAsyncId;
- if (async_hook_fields[kInit] > 0) {
+ if (initHooksExist()) {
emitInit(asyncId,
'TickObject',
triggerAsyncId,
diff --git a/lib/internal/timers.js b/lib/internal/timers.js
index df2a88558f..0c140811f8 100644
--- a/lib/internal/timers.js
+++ b/lib/internal/timers.js
@@ -1,15 +1,11 @@
'use strict';
-const async_wrap = process.binding('async_wrap');
-// Two arrays that share state between C++ and JS.
-const { async_hook_fields, async_id_fields } = async_wrap;
const {
getDefaultTriggerAsyncId,
- // The needed emit*() functions.
+ newAsyncId,
+ initHooksExist,
emitInit
} = require('internal/async_hooks');
-// Grab the constants necessary for working with internal arrays.
-const { kInit, kAsyncIdCounter } = async_wrap.constants;
// Symbols for storing async id state.
const async_id_symbol = Symbol('asyncId');
const trigger_async_id_symbol = Symbol('triggerId');
@@ -70,9 +66,9 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) {
this[unrefedSymbol] = isUnrefed;
- 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],
'Timeout',
this[trigger_async_id_symbol],
diff --git a/lib/net.js b/lib/net.js
index 41ddda767d..9c7986ceb7 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -47,8 +47,11 @@ const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
const { TCPConnectWrap } = process.binding('tcp_wrap');
const { PipeConnectWrap } = process.binding('pipe_wrap');
const { ShutdownWrap, WriteWrap } = process.binding('stream_wrap');
-const { async_id_symbol } = process.binding('async_wrap');
-const { newUid, defaultTriggerAsyncIdScope } = require('internal/async_hooks');
+const {
+ newAsyncId,
+ defaultTriggerAsyncIdScope,
+ symbols: { async_id_symbol }
+} = require('internal/async_hooks');
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
const dns = require('dns');
@@ -91,7 +94,7 @@ function createHandle(fd, is_server) {
function getNewAsyncId(handle) {
return (!handle || typeof handle.getAsyncId !== 'function') ?
- newUid() : handle.getAsyncId();
+ newAsyncId() : handle.getAsyncId();
}
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]);
}