-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
async_hooks: clean up usage in internal code #18720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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], | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
executionAsyncIdandtriggerAsyncIdshould probably stay together. Either both move or neither.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me. I'll move both. (I wanted to avoid requiring both files during bootstrap.)