Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
async_hooks: rename initTriggerId
rename initTriggerId to defaultTriggerAsyncId such it matches the rest
of our naming.
  • Loading branch information
AndreasMadsen committed Dec 19, 2017
commit d4ae984bd106a62a9c40195fea081f2d9c394243
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ function responseKeepAlive(res, req) {
socket.removeListener('error', socketErrorListener);
socket.once('error', freeSocketErrorListener);
// There are cases where _handle === null. Avoid those. Passing null to
// nextTick() will call initTriggerId() to retrieve the id.
// nextTick() will call getDefaultTriggerAsyncId() to retrieve the id.
const asyncId = socket._handle ? socket._handle.getAsyncId() : null;
// Mark this socket as available, AFTER user-added end
// handlers have a chance to run.
Expand Down
4 changes: 2 additions & 2 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
disableHooks,
// Internal Embedder API
newUid,
initTriggerId,
getDefaultTriggerAsyncId,
emitInit,
emitBefore,
emitAfter,
Expand Down Expand Up @@ -147,7 +147,7 @@ class AsyncResource {
if (typeof opts === 'number') {
opts = { triggerAsyncId: opts, requireManualDestroy: false };
} else if (opts.triggerAsyncId === undefined) {
opts.triggerAsyncId = initTriggerId();
opts.triggerAsyncId = getDefaultTriggerAsyncId();
}

// Unlike emitInitScript, AsyncResource doesn't supports null as the
Expand Down
4 changes: 2 additions & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const dns = require('dns');
const util = require('util');
const { isUint8Array } = require('internal/util/types');
const EventEmitter = require('events');
const { setInitTriggerId } = require('internal/async_hooks');
const { setDefaultTriggerAsyncId } = 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');
Expand Down Expand Up @@ -481,7 +481,7 @@ function doSend(ex, self, ip, list, address, port, callback) {
// node::SendWrap isn't instantiated and attached to the JS instance of
// SendWrap above until send() is called. So don't set the init trigger id
// until now.
setInitTriggerId(self[async_id_symbol]);
setDefaultTriggerAsyncId(self[async_id_symbol]);
var err = self._handle.send(req,
list,
list.length,
Expand Down
36 changes: 18 additions & 18 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const async_wrap = process.binding('async_wrap');
* kTriggerAsyncId: The trigger_async_id of the resource responsible for
* the current execution stack.
* kAsyncIdCounter: Incremental counter tracking the next assigned async_id.
* kInitTriggerAsyncId: Written immediately before a resource's constructor
* kDefaultTriggerAsyncId: Written immediately before a resource's constructor
* that sets the value of the init()'s triggerAsyncId. The order of
* retrieving the triggerAsyncId value is passing directly to the
* constructor -> value set in kInitTriggerAsyncId -> executionAsyncId of
* constructor -> value set in kDefaultTriggerAsyncId -> executionAsyncId of
* the current resource.
*/
const { async_hook_fields, async_id_fields } = async_wrap;
Expand Down Expand Up @@ -61,7 +61,7 @@ const active_hooks = {
// for a given step, that step can bail out early.
const { kInit, kBefore, kAfter, kDestroy, kPromiseResolve,
kCheck, kExecutionAsyncId, kAsyncIdCounter,
kInitTriggerAsyncId } = async_wrap.constants;
kDefaultTriggerAsyncId } = async_wrap.constants;

// Used in AsyncHook and AsyncResource.
const init_symbol = Symbol('init');
Expand Down Expand Up @@ -245,25 +245,25 @@ function newUid() {
return ++async_id_fields[kAsyncIdCounter];
}


// Return the triggerAsyncId meant for the constructor calling it. It's up to
// the user to safeguard this call and make sure it's zero'd out when the
// constructor is complete.
function initTriggerId() {
var triggerAsyncId = async_id_fields[kInitTriggerAsyncId];
function getDefaultTriggerAsyncId() {
var defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
// Reset value after it's been called so the next constructor doesn't
// inherit it by accident.
async_id_fields[kInitTriggerAsyncId] = 0;
if (triggerAsyncId <= 0)
triggerAsyncId = async_id_fields[kExecutionAsyncId];
return triggerAsyncId;
async_id_fields[kDefaultTriggerAsyncId] = 0;
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
if (defaultTriggerAsyncId <= 0)
defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId];
return defaultTriggerAsyncId;
}


function setInitTriggerId(triggerAsyncId) {
function setDefaultTriggerAsyncId(triggerAsyncId) {
// CHECK(Number.isSafeInteger(triggerAsyncId))
// CHECK(triggerAsyncId > 0)
async_id_fields[kInitTriggerAsyncId] = triggerAsyncId;
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;
}


Expand All @@ -282,13 +282,13 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
return;

// This can run after the early return check b/c running this function
// manually means that the embedder must have used initTriggerId().
// manually means that the embedder must have used getDefaultTriggerAsyncId().
if (triggerAsyncId === null) {
triggerAsyncId = initTriggerId();
triggerAsyncId = getDefaultTriggerAsyncId();
} else {
// If a triggerAsyncId was passed, any kInitTriggerAsyncId still must be
// If a triggerAsyncId was passed, any kDefaultTriggerAsyncId still must be
// null'd.
async_id_fields[kInitTriggerAsyncId] = 0;
async_id_fields[kDefaultTriggerAsyncId] = 0;
}

emitInitNative(asyncId, type, triggerAsyncId, resource);
Expand Down Expand Up @@ -340,8 +340,8 @@ module.exports = {
disableHooks,
// Internal Embedder API
newUid,
initTriggerId,
setInitTriggerId,
getDefaultTriggerAsyncId,
setDefaultTriggerAsyncId,
emitInit: emitInitScript,
emitBefore: emitBeforeScript,
emitAfter: emitAfterScript,
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,14 @@
// Internal functions needed to manipulate the stack.
const { clearAsyncIdStack, asyncIdStackSize } = async_wrap;
const { kAfter, kExecutionAsyncId,
kInitTriggerAsyncId } = async_wrap.constants;
kDefaultTriggerAsyncId } = async_wrap.constants;

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

// It's possible that kInitTriggerAsyncId was set for a constructor call
// that threw and was never cleared. So clear it now.
async_id_fields[kInitTriggerAsyncId] = 0;
// It's possible that kDefaultTriggerAsyncId was set for a constructor
// call that threw and was never cleared. So clear it now.
async_id_fields[kDefaultTriggerAsyncId] = 0;

if (exceptionHandlerState.captureFn !== null) {
exceptionHandlerState.captureFn(er);
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/process/next_tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function setupNextTick() {
const promises = require('internal/process/promises');
const errors = require('internal/errors');
const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks);
const initTriggerId = async_hooks.initTriggerId;
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.
Expand Down Expand Up @@ -210,7 +210,7 @@ function setupNextTick() {
nextTickQueue.push(new TickObject(callback,
args,
++async_id_fields[kAsyncIdCounter],
initTriggerId()));
getDefaultTriggerAsyncId()));
}

// `internalNextTick()` will not enqueue any callback when the process is
Expand All @@ -237,7 +237,7 @@ function setupNextTick() {
}

if (triggerAsyncId === null)
triggerAsyncId = initTriggerId();
triggerAsyncId = getDefaultTriggerAsyncId();
// In V8 6.2, moving tickInfo & async_id_fields[kAsyncIdCounter] into the
// TickObject incurs a significant performance penalty in the
// next-tick-breadth-args benchmark (revisit later)
Expand Down
10 changes: 5 additions & 5 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ 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, setInitTriggerId } = require('internal/async_hooks');
const { newUid, setDefaultTriggerAsyncId } = require('internal/async_hooks');
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
const dns = require('dns');
Expand Down Expand Up @@ -304,7 +304,7 @@ function onSocketFinish() {
// node::ShutdownWrap isn't instantiated and attached to the JS instance of
// ShutdownWrap above until shutdown() is called. So don't set the init
// trigger id until now.
setInitTriggerId(this[async_id_symbol]);
setDefaultTriggerAsyncId(this[async_id_symbol]);
var err = this._handle.shutdown(req);

if (err)
Expand Down Expand Up @@ -948,7 +948,7 @@ function internalConnect(
// node::TCPConnectWrap isn't instantiated and attached to the JS instance
// of TCPConnectWrap above until connect() is called. So don't set the init
// trigger id until now.
setInitTriggerId(self[async_id_symbol]);
setDefaultTriggerAsyncId(self[async_id_symbol]);
if (addressType === 4)
err = self._handle.connect(req, address, port);
else
Expand All @@ -961,7 +961,7 @@ function internalConnect(
// node::PipeConnectWrap isn't instantiated and attached to the JS instance
// of PipeConnectWrap above until connect() is called. So don't set the
// init trigger id until now.
setInitTriggerId(self[async_id_symbol]);
setDefaultTriggerAsyncId(self[async_id_symbol]);
err = self._handle.connect(req, address, afterConnect);
}

Expand Down Expand Up @@ -1097,7 +1097,7 @@ function lookupAndConnect(self, options) {
debug('connect: dns options', dnsopts);
self._host = host;
var lookup = options.lookup || dns.lookup;
setInitTriggerId(self[async_id_symbol]);
setDefaultTriggerAsyncId(self[async_id_symbol]);
lookup(host, dnsopts, function emitLookup(err, ip, addressType) {
self.emit('lookup', err, ip, addressType, host);

Expand Down
8 changes: 4 additions & 4 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const kOnTimeout = TimerWrap.kOnTimeout | 0;
// Two arrays that share state between C++ and JS.
const { async_hook_fields, async_id_fields } = async_wrap;
const {
initTriggerId,
getDefaultTriggerAsyncId,
// The needed emit*() functions.
emitInit,
emitBefore,
Expand Down Expand Up @@ -181,7 +181,7 @@ function insert(item, unrefed) {
if (!item[async_id_symbol] || item._destroyed) {
item._destroyed = false;
item[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
item[trigger_async_id_symbol] = initTriggerId();
item[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
if (async_hook_fields[kInit] > 0) {
emitInit(item[async_id_symbol],
'Timeout',
Expand Down Expand Up @@ -560,7 +560,7 @@ function Timeout(callback, after, args, isRepeat) {
this._destroyed = false;

this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
this[trigger_async_id_symbol] = initTriggerId();
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
if (async_hook_fields[kInit] > 0) {
emitInit(this[async_id_symbol],
'Timeout',
Expand Down Expand Up @@ -786,7 +786,7 @@ function Immediate(callback, args) {
this._destroyed = false;

this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
this[trigger_async_id_symbol] = initTriggerId();
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
if (async_hook_fields[kInit] > 0) {
emitInit(this[async_id_symbol],
'Immediate',
Expand Down
13 changes: 7 additions & 6 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
}
// get id from parentWrap
double trigger_async_id = parent_wrap->get_async_id();
env->set_init_trigger_async_id(trigger_async_id);
env->set_default_trigger_async_id(trigger_async_id);
}

wrap = PromiseWrap::New(env, promise, parent_wrap, silent);
Expand Down Expand Up @@ -542,9 +542,10 @@ void AsyncWrap::Initialize(Local<Object> target,
//
// kAsyncUid: Maintains the state of the next unique id to be assigned.
//
// kInitTriggerAsyncId: Write the id of the resource responsible for a
// kDefaultTriggerAsyncId: Write the id of the resource responsible for a
// handle's creation just before calling the new handle's constructor.
// After the new handle is constructed kInitTriggerAsyncId is set back to 0.
// After the new handle is constructed kDefaultTriggerAsyncId is set back
// to 0.
FORCE_SET_TARGET_FIELD(target,
"async_id_fields",
env->async_hooks()->async_id_fields().GetJSArray());
Expand All @@ -564,7 +565,7 @@ void AsyncWrap::Initialize(Local<Object> target,
SET_HOOKS_CONSTANT(kExecutionAsyncId);
SET_HOOKS_CONSTANT(kTriggerAsyncId);
SET_HOOKS_CONSTANT(kAsyncIdCounter);
SET_HOOKS_CONSTANT(kInitTriggerAsyncId);
SET_HOOKS_CONSTANT(kDefaultTriggerAsyncId);
#undef SET_HOOKS_CONSTANT
FORCE_SET_TARGET_FIELD(target, "constants", constants);

Expand Down Expand Up @@ -677,7 +678,7 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
async_id_ =
execution_async_id == -1 ? env()->new_async_id() : execution_async_id;
trigger_async_id_ = env()->get_init_trigger_async_id();
trigger_async_id_ = env()->get_default_trigger_async_id();

switch (provider_type()) {
#define V(PROVIDER) \
Expand Down Expand Up @@ -778,7 +779,7 @@ async_context EmitAsyncInit(Isolate* isolate,

// Initialize async context struct
if (trigger_async_id == -1)
trigger_async_id = env->get_init_trigger_async_id();
trigger_async_id = env->get_default_trigger_async_id();

async_context context = {
env->new_async_id(), // async_id_
Expand Down
2 changes: 1 addition & 1 deletion src/connection_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,
};

if (status == 0) {
env->set_init_trigger_async_id(wrap_data->get_async_id());
env->set_default_trigger_async_id(wrap_data->get_async_id());
// Instantiate the client javascript object and handle.
Local<Object> client_obj = WrapType::Instantiate(env,
wrap_data,
Expand Down
23 changes: 12 additions & 11 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,18 @@ inline double Environment::trigger_async_id() {
return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
}

inline double Environment::get_init_trigger_async_id() {
AliasedBuffer<double, v8::Float64Array>& async_id_fields =
async_hooks()->async_id_fields();
double tid = async_id_fields[AsyncHooks::kInitTriggerAsyncId];
async_id_fields[AsyncHooks::kInitTriggerAsyncId] = 0;
if (tid <= 0) tid = execution_async_id();
return tid;
}

inline void Environment::set_init_trigger_async_id(const double id) {
async_hooks()->async_id_fields()[AsyncHooks::kInitTriggerAsyncId] = id;
inline double Environment::get_default_trigger_async_id() {
double default_trigger_async_id =
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = 0;
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
if (default_trigger_async_id <= 0)
default_trigger_async_id = execution_async_id();
return default_trigger_async_id;
}

inline void Environment::set_default_trigger_async_id(const double id) {
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = id;
}

inline double* Environment::heap_statistics_buffer() const {
Expand Down
6 changes: 3 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class Environment {
kExecutionAsyncId,
kTriggerAsyncId,
kAsyncIdCounter,
kInitTriggerAsyncId,
kDefaultTriggerAsyncId,
kUidFieldsCount,
};

Expand Down Expand Up @@ -558,8 +558,8 @@ class Environment {
inline double new_async_id();
inline double execution_async_id();
inline double trigger_async_id();
inline double get_init_trigger_async_id();
inline void set_init_trigger_async_id(const double id);
inline double get_default_trigger_async_id();
inline void set_default_trigger_async_id(const double id);

// List of id's that have been destroyed and need the destroy() cb called.
inline std::vector<double>* destroy_async_id_list();
Expand Down
Loading