summaryrefslogtreecommitdiff
path: root/lib/internal/async_hooks.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/internal/async_hooks.js')
-rw-r--r--lib/internal/async_hooks.js60
1 files changed, 56 insertions, 4 deletions
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,