diff options
author | Anatoli Papirovski <apapirovski@mac.com> | 2018-02-12 00:35:59 +0300 |
---|---|---|
committer | Anatoli Papirovski <apapirovski@mac.com> | 2018-02-16 22:23:14 +0300 |
commit | e9ac80bb397293feef3b47f3ed609c86edb48681 (patch) | |
tree | 357523992d1b469f800e58853a3a79214633453e /lib/internal/async_hooks.js | |
parent | 7748865cd3322ff9421458ccc862291eb26cec62 (diff) |
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/internal/async_hooks.js')
-rw-r--r-- | lib/internal/async_hooks.js | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index d1310ad2cac..dc720a22305 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, |