Skip to content

Commit 1bc0c1f

Browse files
AndreasMadsenMylesBorins
authored andcommittedOct 3, 2017
async_hooks: consistent internal naming
PR-URL: #15569 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e0a7634 commit 1bc0c1f

21 files changed

+270
-250
lines changed
 

‎lib/async_hooks.js

+35-31
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ const errors = require('internal/errors');
77
* Environment::AsyncHooks::fields_[]. Each index tracks the number of active
88
* hooks for each type.
99
*
10-
* async_uid_fields is a Float64Array wrapping the double array of
10+
* async_id_fields is a Float64Array wrapping the double array of
1111
* Environment::AsyncHooks::uid_fields_[]. Each index contains the ids for the
1212
* various asynchronous states of the application. These are:
13-
* kCurrentAsyncId: The async_id assigned to the resource responsible for the
13+
* kExecutionAsyncId: The async_id assigned to the resource responsible for the
1414
* current execution stack.
15-
* kCurrentTriggerId: The trigger_async_id of the resource responsible for the
16-
* current execution stack.
17-
* kAsyncUidCntr: Incremental counter tracking the next assigned async_id.
18-
* kInitTriggerId: Written immediately before a resource's constructor that
19-
* sets the value of the init()'s triggerAsyncId. The order of retrieving
20-
* the triggerAsyncId value is passing directly to the constructor -> value
21-
* set in kInitTriggerId -> executionAsyncId of the current resource.
15+
* kTriggerAsyncId: The trigger_async_id of the resource responsible for
16+
* the current execution stack.
17+
* kAsyncIdCounter: Incremental counter tracking the next assigned async_id.
18+
* kInitTriggerAsyncId: Written immediately before a resource's constructor
19+
* that sets the value of the init()'s triggerAsyncId. The order of
20+
* retrieving the triggerAsyncId value is passing directly to the
21+
* constructor -> value set in kInitTriggerAsyncId -> executionAsyncId of
22+
* the current resource.
2223
*/
23-
const { async_hook_fields, async_uid_fields } = async_wrap;
24+
const { async_hook_fields, async_id_fields } = async_wrap;
2425
// Store the pair executionAsyncId and triggerAsyncId in a std::stack on
2526
// Environment::AsyncHooks::ids_stack_ tracks the resource responsible for the
2627
// current execution stack. This is unwound as each resource exits. In the case
@@ -59,14 +60,14 @@ const active_hooks = {
5960
// Each constant tracks how many callbacks there are for any given step of
6061
// async execution. These are tracked so if the user didn't include callbacks
6162
// for a given step, that step can bail out early.
62-
const { kInit, kBefore, kAfter, kDestroy, kPromiseResolve, kTotals,
63-
kCurrentAsyncId, kCurrentTriggerId, kAsyncUidCntr,
64-
kInitTriggerId } = async_wrap.constants;
63+
const { kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
64+
kExecutionAsyncId, kTriggerAsyncId, kAsyncIdCounter,
65+
kInitTriggerAsyncId } = async_wrap.constants;
6566

6667
// Symbols used to store the respective ids on both AsyncResource instances and
6768
// internal resources. They will also be assigned to arbitrary objects passed
6869
// in by the user that take place of internally constructed objects.
69-
const { async_id_symbol, trigger_id_symbol } = async_wrap;
70+
const { async_id_symbol, trigger_async_id_symbol } = async_wrap;
7071

7172
// Used in AsyncHook and AsyncResource.
7273
const init_symbol = Symbol('init');
@@ -235,12 +236,12 @@ function createHook(fns) {
235236

236237

237238
function executionAsyncId() {
238-
return async_uid_fields[kCurrentAsyncId];
239+
return async_id_fields[kExecutionAsyncId];
239240
}
240241

241242

242243
function triggerAsyncId() {
243-
return async_uid_fields[kCurrentTriggerId];
244+
return async_id_fields[kTriggerAsyncId];
244245
}
245246

246247

@@ -259,14 +260,16 @@ class AsyncResource {
259260
triggerAsyncId);
260261
}
261262

262-
this[async_id_symbol] = ++async_uid_fields[kAsyncUidCntr];
263-
this[trigger_id_symbol] = triggerAsyncId;
263+
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
264+
this[trigger_async_id_symbol] = triggerAsyncId;
264265

265-
emitInitScript(this[async_id_symbol], type, this[trigger_id_symbol], this);
266+
emitInitScript(
267+
this[async_id_symbol], type, this[trigger_async_id_symbol], this
268+
);
266269
}
267270

268271
emitBefore() {
269-
emitBeforeScript(this[async_id_symbol], this[trigger_id_symbol]);
272+
emitBeforeScript(this[async_id_symbol], this[trigger_async_id_symbol]);
270273
return this;
271274
}
272275

@@ -285,7 +288,7 @@ class AsyncResource {
285288
}
286289

287290
triggerAsyncId() {
288-
return this[trigger_id_symbol];
291+
return this[trigger_async_id_symbol];
289292
}
290293
}
291294

@@ -320,28 +323,28 @@ function runInAsyncIdScope(asyncId, cb) {
320323
// counter increment first. Since it's done the same way in
321324
// Environment::new_async_uid()
322325
function newUid() {
323-
return ++async_uid_fields[kAsyncUidCntr];
326+
return ++async_id_fields[kAsyncIdCounter];
324327
}
325328

326329

327330
// Return the triggerAsyncId meant for the constructor calling it. It's up to
328331
// the user to safeguard this call and make sure it's zero'd out when the
329332
// constructor is complete.
330333
function initTriggerId() {
331-
var tId = async_uid_fields[kInitTriggerId];
334+
var triggerAsyncId = async_id_fields[kInitTriggerAsyncId];
332335
// Reset value after it's been called so the next constructor doesn't
333336
// inherit it by accident.
334-
async_uid_fields[kInitTriggerId] = 0;
335-
if (tId <= 0)
336-
tId = async_uid_fields[kCurrentAsyncId];
337-
return tId;
337+
async_id_fields[kInitTriggerAsyncId] = 0;
338+
if (triggerAsyncId <= 0)
339+
triggerAsyncId = async_id_fields[kExecutionAsyncId];
340+
return triggerAsyncId;
338341
}
339342

340343

341344
function setInitTriggerId(triggerAsyncId) {
342345
// CHECK(Number.isSafeInteger(triggerAsyncId))
343346
// CHECK(triggerAsyncId > 0)
344-
async_uid_fields[kInitTriggerId] = triggerAsyncId;
347+
async_id_fields[kInitTriggerAsyncId] = triggerAsyncId;
345348
}
346349

347350

@@ -358,8 +361,9 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
358361
if (triggerAsyncId === null) {
359362
triggerAsyncId = initTriggerId();
360363
} else {
361-
// If a triggerAsyncId was passed, any kInitTriggerId still must be null'd.
362-
async_uid_fields[kInitTriggerId] = 0;
364+
// If a triggerAsyncId was passed, any kInitTriggerAsyncId still must be
365+
// null'd.
366+
async_id_fields[kInitTriggerAsyncId] = 0;
363367
}
364368

365369
if (!Number.isSafeInteger(asyncId) || asyncId < -1) {
@@ -458,7 +462,7 @@ function emitDestroyScript(asyncId) {
458462
// Return early if there are no destroy callbacks, or invalid asyncId.
459463
if (async_hook_fields[kDestroy] === 0 || asyncId <= 0)
460464
return;
461-
async_wrap.addIdToDestroyList(asyncId);
465+
async_wrap.queueDestroyAsyncId(asyncId);
462466
}
463467

464468

‎lib/internal/bootstrap_node.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,18 @@
354354
function setupProcessFatal() {
355355
const async_wrap = process.binding('async_wrap');
356356
// Arrays containing hook flags and ids for async_hook calls.
357-
const { async_hook_fields, async_uid_fields } = async_wrap;
357+
const { async_hook_fields, async_id_fields } = async_wrap;
358358
// Internal functions needed to manipulate the stack.
359-
const { clearIdStack, asyncIdStackSize } = async_wrap;
360-
const { kAfter, kCurrentAsyncId, kInitTriggerId } = async_wrap.constants;
359+
const { clearAsyncIdStack, asyncIdStackSize } = async_wrap;
360+
const { kAfter, kExecutionAsyncId,
361+
kInitTriggerAsyncId } = async_wrap.constants;
361362

362363
process._fatalException = function(er) {
363364
var caught;
364365

365-
// It's possible that kInitTriggerId was set for a constructor call that
366-
// threw and was never cleared. So clear it now.
367-
async_uid_fields[kInitTriggerId] = 0;
366+
// It's possible that kInitTriggerAsyncId was set for a constructor call
367+
// that threw and was never cleared. So clear it now.
368+
async_id_fields[kInitTriggerAsyncId] = 0;
368369

369370
if (process.domain && process.domain._errorHandler)
370371
caught = process.domain._errorHandler(er);
@@ -392,11 +393,11 @@
392393
if (async_hook_fields[kAfter] > 0) {
393394
do {
394395
NativeModule.require('async_hooks').emitAfter(
395-
async_uid_fields[kCurrentAsyncId]);
396+
async_id_fields[kExecutionAsyncId]);
396397
} while (asyncIdStackSize() > 0);
397398
// Or completely empty the id stack.
398399
} else {
399-
clearIdStack();
400+
clearAsyncIdStack();
400401
}
401402
}
402403

‎lib/internal/process/next_tick.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ function setupNextTick() {
5454
const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks);
5555
const initTriggerId = async_hooks.initTriggerId;
5656
// Two arrays that share state between C++ and JS.
57-
const { async_hook_fields, async_uid_fields } = async_wrap;
57+
const { async_hook_fields, async_id_fields } = async_wrap;
5858
// Used to change the state of the async id stack.
5959
const { emitInit, emitBefore, emitAfter, emitDestroy } = async_hooks;
6060
// Grab the constants necessary for working with internal arrays.
61-
const { kInit, kDestroy, kAsyncUidCntr } = async_wrap.constants;
62-
const { async_id_symbol, trigger_id_symbol } = async_wrap;
61+
const { kInit, kDestroy, kAsyncIdCounter } = async_wrap.constants;
62+
const { async_id_symbol, trigger_async_id_symbol } = async_wrap;
6363
var nextTickQueue = new NextTickQueue();
6464
var microtasksScheduled = false;
6565

@@ -102,7 +102,7 @@ function setupNextTick() {
102102
args: undefined,
103103
domain: null,
104104
[async_id_symbol]: 0,
105-
[trigger_id_symbol]: 0
105+
[trigger_async_id_symbol]: 0
106106
};
107107
function scheduleMicrotasks() {
108108
if (microtasksScheduled)
@@ -158,10 +158,10 @@ function setupNextTick() {
158158

159159
// CHECK(Number.isSafeInteger(tock[async_id_symbol]))
160160
// CHECK(tock[async_id_symbol] > 0)
161-
// CHECK(Number.isSafeInteger(tock[trigger_id_symbol]))
162-
// CHECK(tock[trigger_id_symbol] > 0)
161+
// CHECK(Number.isSafeInteger(tock[trigger_async_id_symbol]))
162+
// CHECK(tock[trigger_async_id_symbol] > 0)
163163

164-
emitBefore(tock[async_id_symbol], tock[trigger_id_symbol]);
164+
emitBefore(tock[async_id_symbol], tock[trigger_async_id_symbol]);
165165
// emitDestroy() places the async_id_symbol into an asynchronous queue
166166
// that calls the destroy callback in the future. It's called before
167167
// calling tock.callback so destroy will be called even if the callback
@@ -203,10 +203,10 @@ function setupNextTick() {
203203

204204
// CHECK(Number.isSafeInteger(tock[async_id_symbol]))
205205
// CHECK(tock[async_id_symbol] > 0)
206-
// CHECK(Number.isSafeInteger(tock[trigger_id_symbol]))
207-
// CHECK(tock[trigger_id_symbol] > 0)
206+
// CHECK(Number.isSafeInteger(tock[trigger_async_id_symbol]))
207+
// CHECK(tock[trigger_async_id_symbol] > 0)
208208

209-
emitBefore(tock[async_id_symbol], tock[trigger_id_symbol]);
209+
emitBefore(tock[async_id_symbol], tock[trigger_async_id_symbol]);
210210
// TODO(trevnorris): See comment in _tickCallback() as to why this
211211
// isn't a good solution.
212212
if (async_hook_fields[kDestroy] > 0)
@@ -236,7 +236,7 @@ function setupNextTick() {
236236
this.args = args;
237237
this.domain = process.domain || null;
238238
this[async_id_symbol] = asyncId;
239-
this[trigger_id_symbol] = triggerAsyncId;
239+
this[trigger_async_id_symbol] = triggerAsyncId;
240240
}
241241
}
242242

@@ -261,7 +261,7 @@ function setupNextTick() {
261261
args[i - 1] = arguments[i];
262262
}
263263

264-
const asyncId = ++async_uid_fields[kAsyncUidCntr];
264+
const asyncId = ++async_id_fields[kAsyncIdCounter];
265265
const triggerAsyncId = initTriggerId();
266266
const obj = new TickObject(callback, args, asyncId, triggerAsyncId);
267267
nextTickQueue.push(obj);
@@ -297,7 +297,7 @@ function setupNextTick() {
297297
args[i - 2] = arguments[i];
298298
}
299299

300-
const asyncId = ++async_uid_fields[kAsyncUidCntr];
300+
const asyncId = ++async_id_fields[kAsyncIdCounter];
301301
const obj = new TickObject(callback, args, asyncId, triggerAsyncId);
302302
nextTickQueue.push(obj);
303303
++tickInfo[kLength];

‎lib/timers.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ const debug = util.debuglog('timer');
3333
const kOnTimeout = TimerWrap.kOnTimeout | 0;
3434
const initTriggerId = async_hooks.initTriggerId;
3535
// Two arrays that share state between C++ and JS.
36-
const { async_hook_fields, async_uid_fields } = async_wrap;
36+
const { async_hook_fields, async_id_fields } = async_wrap;
3737
// The needed emit*() functions.
3838
const { emitInit, emitBefore, emitAfter, emitDestroy } = async_hooks;
3939
// Grab the constants necessary for working with internal arrays.
40-
const { kInit, kDestroy, kAsyncUidCntr } = async_wrap.constants;
40+
const { kInit, kDestroy, kAsyncIdCounter } = async_wrap.constants;
4141
// Symbols for storing async id state.
4242
const async_id_symbol = Symbol('asyncId');
43-
const trigger_id_symbol = Symbol('triggerAsyncId');
43+
const trigger_async_id_symbol = Symbol('triggerAsyncId');
4444

4545
// Timeout values > TIMEOUT_MAX are set to 1.
4646
const TIMEOUT_MAX = 2147483647; // 2^31-1
@@ -168,10 +168,12 @@ function insert(item, unrefed) {
168168

169169
if (!item[async_id_symbol] || item._destroyed) {
170170
item._destroyed = false;
171-
item[async_id_symbol] = ++async_uid_fields[kAsyncUidCntr];
172-
item[trigger_id_symbol] = initTriggerId();
171+
item[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
172+
item[trigger_async_id_symbol] = initTriggerId();
173173
if (async_hook_fields[kInit] > 0)
174-
emitInit(item[async_id_symbol], 'Timeout', item[trigger_id_symbol], item);
174+
emitInit(
175+
item[async_id_symbol], 'Timeout', item[trigger_async_id_symbol], item
176+
);
175177
}
176178

177179
L.append(list, item);
@@ -299,7 +301,7 @@ function tryOnTimeout(timer, list) {
299301
timer[async_id_symbol] : null;
300302
var threw = true;
301303
if (timerAsyncId !== null)
302-
emitBefore(timerAsyncId, timer[trigger_id_symbol]);
304+
emitBefore(timerAsyncId, timer[trigger_async_id_symbol]);
303305
try {
304306
ontimeout(timer);
305307
threw = false;
@@ -567,10 +569,12 @@ function Timeout(after, callback, args) {
567569
this._timerArgs = args;
568570
this._repeat = null;
569571
this._destroyed = false;
570-
this[async_id_symbol] = ++async_uid_fields[kAsyncUidCntr];
571-
this[trigger_id_symbol] = initTriggerId();
572+
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
573+
this[trigger_async_id_symbol] = initTriggerId();
572574
if (async_hook_fields[kInit] > 0)
573-
emitInit(this[async_id_symbol], 'Timeout', this[trigger_id_symbol], this);
575+
emitInit(
576+
this[async_id_symbol], 'Timeout', this[trigger_async_id_symbol], this
577+
);
574578
}
575579

576580

@@ -737,7 +741,7 @@ function processImmediate() {
737741
// 4.7) what is in this smaller function.
738742
function tryOnImmediate(immediate, oldTail) {
739743
var threw = true;
740-
emitBefore(immediate[async_id_symbol], immediate[trigger_id_symbol]);
744+
emitBefore(immediate[async_id_symbol], immediate[trigger_async_id_symbol]);
741745
try {
742746
// make the actual call outside the try/finally to allow it to be optimized
743747
runCallback(immediate);
@@ -802,10 +806,12 @@ function Immediate() {
802806
this._onImmediate = null;
803807
this._destroyed = false;
804808
this.domain = process.domain;
805-
this[async_id_symbol] = ++async_uid_fields[kAsyncUidCntr];
806-
this[trigger_id_symbol] = initTriggerId();
809+
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
810+
this[trigger_async_id_symbol] = initTriggerId();
807811
if (async_hook_fields[kInit] > 0)
808-
emitInit(this[async_id_symbol], 'Immediate', this[trigger_id_symbol], this);
812+
emitInit(
813+
this[async_id_symbol], 'Immediate', this[trigger_async_id_symbol], this
814+
);
809815
}
810816

811817
function setImmediate(callback, arg1, arg2, arg3) {

‎src/async-wrap-inl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ inline AsyncWrap::ProviderType AsyncWrap::provider_type() const {
3636
}
3737

3838

39-
inline double AsyncWrap::get_id() const {
39+
inline double AsyncWrap::get_async_id() const {
4040
return async_id_;
4141
}
4242

4343

44-
inline double AsyncWrap::get_trigger_id() const {
45-
return trigger_id_;
44+
inline double AsyncWrap::get_trigger_async_id() const {
45+
return trigger_async_id_;
4646
}
4747

4848

0 commit comments

Comments
 (0)
Please sign in to comment.