Browse Source

async_wrap: allow user to pass execution_async_id

Allow the user to pass in an execution_async_id instead of always
generating one. This way the JS API can be used to pre-allocate the
execution_async_id when the JS object is instantiated, before the native
resource is created.

Also allow the new execution_async_id to be passed via asyncReset().

PR-URL: https://github.com/nodejs/node/pull/14208
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
canary-base
Trevor Norris 8 years ago
parent
commit
2b9b46cd55
No known key found for this signature in database GPG Key ID: 251CA676820DC7F3
  1. 15
      src/async-wrap.cc
  2. 5
      src/async-wrap.h

15
src/async-wrap.cc

@ -451,7 +451,8 @@ void AsyncWrap::ClearAsyncIdStack(const FunctionCallbackInfo<Value>& args) {
void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) { void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
AsyncWrap* wrap; AsyncWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
wrap->AsyncReset(); double execution_async_id = args[0]->IsNumber() ? args[0]->NumberValue() : -1;
wrap->AsyncReset(execution_async_id);
} }
@ -573,7 +574,8 @@ void LoadAsyncWrapperInfo(Environment* env) {
AsyncWrap::AsyncWrap(Environment* env, AsyncWrap::AsyncWrap(Environment* env,
Local<Object> object, Local<Object> object,
ProviderType provider) ProviderType provider,
double execution_async_id)
: BaseObject(env, object), : BaseObject(env, object),
provider_type_(provider) { provider_type_(provider) {
CHECK_NE(provider, PROVIDER_NONE); CHECK_NE(provider, PROVIDER_NONE);
@ -583,7 +585,7 @@ AsyncWrap::AsyncWrap(Environment* env,
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider); persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider);
// Use AsyncReset() call to execute the init() callbacks. // Use AsyncReset() call to execute the init() callbacks.
AsyncReset(); AsyncReset(execution_async_id);
} }
@ -599,7 +601,7 @@ AsyncWrap::AsyncWrap(Environment* env,
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider_type_); persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider_type_);
// Use AsyncReset() call to execute the init() callbacks. // Use AsyncReset() call to execute the init() callbacks.
AsyncReset(silent); AsyncReset(-1, silent);
} }
@ -611,8 +613,9 @@ AsyncWrap::~AsyncWrap() {
// Generalized call for both the constructor and for handles that are pooled // Generalized call for both the constructor and for handles that are pooled
// and reused over their lifetime. This way a new uid can be assigned when // and reused over their lifetime. This way a new uid can be assigned when
// the resource is pulled out of the pool and put back into use. // the resource is pulled out of the pool and put back into use.
void AsyncWrap::AsyncReset(bool silent) { void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
async_id_ = env()->new_async_id(); 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_init_trigger_async_id();
if (silent) return; if (silent) return;

5
src/async-wrap.h

@ -94,7 +94,8 @@ class AsyncWrap : public BaseObject {
AsyncWrap(Environment* env, AsyncWrap(Environment* env,
v8::Local<v8::Object> object, v8::Local<v8::Object> object,
ProviderType provider); ProviderType provider,
double execution_async_id = -1);
virtual ~AsyncWrap(); virtual ~AsyncWrap();
@ -132,7 +133,7 @@ class AsyncWrap : public BaseObject {
inline double get_trigger_async_id() const; inline double get_trigger_async_id() const;
void AsyncReset(bool silent = false); void AsyncReset(double execution_async_id = -1, bool silent = false);
// Only call these within a valid HandleScope. // Only call these within a valid HandleScope.
v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb, v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,

Loading…
Cancel
Save