Skip to content

Commit

Permalink
src: move AsyncHooks out of Environment
Browse files Browse the repository at this point in the history
PR-URL: #26824
Refs: #26776
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
joyeecheung authored and targos committed Mar 27, 2019
1 parent a545cfe commit 4af9ff0
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 112 deletions.
2 changes: 0 additions & 2 deletions src/api/callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ using v8::Object;
using v8::String;
using v8::Value;

using AsyncHooks = Environment::AsyncHooks;

CallbackScope::CallbackScope(Isolate* isolate,
Local<Object> object,
async_context asyncContext)
Expand Down
10 changes: 4 additions & 6 deletions src/async_wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ inline double AsyncWrap::get_trigger_async_id() const {
inline AsyncWrap::AsyncScope::AsyncScope(AsyncWrap* wrap)
: wrap_(wrap) {
Environment* env = wrap->env();
if (env->async_hooks()->fields()[Environment::AsyncHooks::kBefore] == 0)
return;
if (env->async_hooks()->fields()[AsyncHooks::kBefore] == 0) return;
EmitBefore(env, wrap->get_async_id());
}

inline AsyncWrap::AsyncScope::~AsyncScope() {
Environment* env = wrap_->env();
if (env->async_hooks()->fields()[Environment::AsyncHooks::kAfter] == 0)
return;
if (env->async_hooks()->fields()[AsyncHooks::kAfter] == 0) return;
EmitAfter(env, wrap_->get_async_id());
}

Expand Down Expand Up @@ -94,8 +92,8 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(


// Defined here to avoid a circular dependency with env-inl.h.
inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope
::DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap)
inline AsyncHooks::DefaultTriggerAsyncIdScope ::DefaultTriggerAsyncIdScope(
AsyncWrap* async_wrap)
: DefaultTriggerAsyncIdScope(async_wrap->env(),
async_wrap->get_async_id()) {}

Expand Down
1 change: 0 additions & 1 deletion src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ using v8::Value;
using v8::WeakCallbackInfo;
using v8::WeakCallbackType;

using AsyncHooks = node::Environment::AsyncHooks;
using TryCatchScope = node::errors::TryCatchScope;

namespace node {
Expand Down
35 changes: 15 additions & 20 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ inline MultiIsolatePlatform* IsolateData::platform() const {
return platform_;
}

inline Environment::AsyncHooks::AsyncHooks()
inline AsyncHooks::AsyncHooks()
: async_ids_stack_(env()->isolate(), 16 * 2),
fields_(env()->isolate(), kFieldsCount),
async_id_fields_(env()->isolate(), kUidFieldsCount) {
Expand Down Expand Up @@ -102,36 +102,33 @@ inline Environment::AsyncHooks::AsyncHooks()
#undef V
}

inline AliasedBuffer<uint32_t, v8::Uint32Array>&
Environment::AsyncHooks::fields() {
inline AliasedBuffer<uint32_t, v8::Uint32Array>& AsyncHooks::fields() {
return fields_;
}

inline AliasedBuffer<double, v8::Float64Array>&
Environment::AsyncHooks::async_id_fields() {
inline AliasedBuffer<double, v8::Float64Array>& AsyncHooks::async_id_fields() {
return async_id_fields_;
}

inline AliasedBuffer<double, v8::Float64Array>&
Environment::AsyncHooks::async_ids_stack() {
inline AliasedBuffer<double, v8::Float64Array>& AsyncHooks::async_ids_stack() {
return async_ids_stack_;
}

inline v8::Local<v8::String> Environment::AsyncHooks::provider_string(int idx) {
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
return providers_[idx].Get(env()->isolate());
}

inline void Environment::AsyncHooks::no_force_checks() {
inline void AsyncHooks::no_force_checks() {
fields_[kCheck] -= 1;
}

inline Environment* Environment::AsyncHooks::env() {
inline Environment* AsyncHooks::env() {
return Environment::ForAsyncHooks(this);
}

// Remember to keep this code aligned with pushAsyncIds() in JS.
inline void Environment::AsyncHooks::push_async_ids(double async_id,
double trigger_async_id) {
inline void AsyncHooks::push_async_ids(double async_id,
double trigger_async_id) {
// Since async_hooks is experimental, do only perform the check
// when async_hooks is enabled.
if (fields_[kCheck] > 0) {
Expand All @@ -150,7 +147,7 @@ inline void Environment::AsyncHooks::push_async_ids(double async_id,
}

// Remember to keep this code aligned with popAsyncIds() in JS.
inline bool Environment::AsyncHooks::pop_async_id(double async_id) {
inline bool AsyncHooks::pop_async_id(double async_id) {
// In case of an exception then this may have already been reset, if the
// stack was multiple MakeCallback()'s deep.
if (fields_[kStackLength] == 0) return false;
Expand Down Expand Up @@ -183,7 +180,7 @@ inline bool Environment::AsyncHooks::pop_async_id(double async_id) {
}

// Keep in sync with clearAsyncIdStack in lib/internal/async_hooks.js.
inline void Environment::AsyncHooks::clear_async_id_stack() {
inline void AsyncHooks::clear_async_id_stack() {
async_id_fields_[kExecutionAsyncId] = 0;
async_id_fields_[kTriggerAsyncId] = 0;
fields_[kStackLength] = 0;
Expand All @@ -192,9 +189,8 @@ inline void Environment::AsyncHooks::clear_async_id_stack() {
// The DefaultTriggerAsyncIdScope(AsyncWrap*) constructor is defined in
// async_wrap-inl.h to avoid a circular dependency.

inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope
::DefaultTriggerAsyncIdScope(Environment* env,
double default_trigger_async_id)
inline AsyncHooks::DefaultTriggerAsyncIdScope ::DefaultTriggerAsyncIdScope(
Environment* env, double default_trigger_async_id)
: async_hooks_(env->async_hooks()) {
if (env->async_hooks()->fields()[AsyncHooks::kCheck] > 0) {
CHECK_GE(default_trigger_async_id, 0);
Expand All @@ -206,8 +202,7 @@ inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope
default_trigger_async_id;
}

inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope
::~DefaultTriggerAsyncIdScope() {
inline AsyncHooks::DefaultTriggerAsyncIdScope ::~DefaultTriggerAsyncIdScope() {
async_hooks_->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] =
old_default_trigger_async_id_;
}
Expand Down Expand Up @@ -430,7 +425,7 @@ inline void Environment::set_is_in_inspector_console_call(bool value) {
}
#endif

inline Environment::AsyncHooks* Environment::async_hooks() {
inline AsyncHooks* Environment::async_hooks() {
return &async_hooks_;
}

Expand Down
3 changes: 1 addition & 2 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,7 @@ void Environment::CollectUVExceptionInfo(Local<Value> object,
syscall, message, path, dest);
}


void Environment::AsyncHooks::grow_async_ids_stack() {
void AsyncHooks::grow_async_ids_stack() {
async_ids_stack_.reserve(async_ids_stack_.Length() * 3);

env()->async_hooks_binding()->Set(
Expand Down
143 changes: 71 additions & 72 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -532,87 +532,86 @@ class AsyncRequest : public MemoryRetainer {
std::atomic_bool stopped_ {true};
};

class Environment {
class AsyncHooks {
public:
Environment(const Environment&) = delete;
Environment& operator=(const Environment&) = delete;
// Reason for both UidFields and Fields are that one is stored as a double*
// and the other as a uint32_t*.
enum Fields {
kInit,
kBefore,
kAfter,
kDestroy,
kPromiseResolve,
kTotals,
kCheck,
kStackLength,
kFieldsCount,
};

class AsyncHooks {
public:
// Reason for both UidFields and Fields are that one is stored as a double*
// and the other as a uint32_t*.
enum Fields {
kInit,
kBefore,
kAfter,
kDestroy,
kPromiseResolve,
kTotals,
kCheck,
kStackLength,
kFieldsCount,
};
enum UidFields {
kExecutionAsyncId,
kTriggerAsyncId,
kAsyncIdCounter,
kDefaultTriggerAsyncId,
kUidFieldsCount,
};

enum UidFields {
kExecutionAsyncId,
kTriggerAsyncId,
kAsyncIdCounter,
kDefaultTriggerAsyncId,
kUidFieldsCount,
};
inline AliasedBuffer<uint32_t, v8::Uint32Array>& fields();
inline AliasedBuffer<double, v8::Float64Array>& async_id_fields();
inline AliasedBuffer<double, v8::Float64Array>& async_ids_stack();

inline AliasedBuffer<uint32_t, v8::Uint32Array>& fields();
inline AliasedBuffer<double, v8::Float64Array>& async_id_fields();
inline AliasedBuffer<double, v8::Float64Array>& async_ids_stack();

inline v8::Local<v8::String> provider_string(int idx);

inline void no_force_checks();
inline Environment* env();

inline void push_async_ids(double async_id, double trigger_async_id);
inline bool pop_async_id(double async_id);
inline void clear_async_id_stack(); // Used in fatal exceptions.

AsyncHooks(const AsyncHooks&) = delete;
AsyncHooks& operator=(const AsyncHooks&) = delete;

// Used to set the kDefaultTriggerAsyncId in a scope. This is instead of
// passing the trigger_async_id along with other constructor arguments.
class DefaultTriggerAsyncIdScope {
public:
DefaultTriggerAsyncIdScope() = delete;
explicit DefaultTriggerAsyncIdScope(Environment* env,
double init_trigger_async_id);
explicit DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap);
~DefaultTriggerAsyncIdScope();

DefaultTriggerAsyncIdScope(const DefaultTriggerAsyncIdScope&) = delete;
DefaultTriggerAsyncIdScope& operator=(const DefaultTriggerAsyncIdScope&) =
delete;

private:
AsyncHooks* async_hooks_;
double old_default_trigger_async_id_;
};
inline v8::Local<v8::String> provider_string(int idx);

inline void no_force_checks();
inline Environment* env();

private:
friend class Environment; // So we can call the constructor.
inline AsyncHooks();
// Keep a list of all Persistent strings used for Provider types.
v8::Eternal<v8::String> providers_[AsyncWrap::PROVIDERS_LENGTH];
// Stores the ids of the current execution context stack.
AliasedBuffer<double, v8::Float64Array> async_ids_stack_;
// Attached to a Uint32Array that tracks the number of active hooks for
// each type.
AliasedBuffer<uint32_t, v8::Uint32Array> fields_;
// Attached to a Float64Array that tracks the state of async resources.
AliasedBuffer<double, v8::Float64Array> async_id_fields_;
inline void push_async_ids(double async_id, double trigger_async_id);
inline bool pop_async_id(double async_id);
inline void clear_async_id_stack(); // Used in fatal exceptions.

void grow_async_ids_stack();
AsyncHooks(const AsyncHooks&) = delete;
AsyncHooks& operator=(const AsyncHooks&) = delete;

// Used to set the kDefaultTriggerAsyncId in a scope. This is instead of
// passing the trigger_async_id along with other constructor arguments.
class DefaultTriggerAsyncIdScope {
public:
DefaultTriggerAsyncIdScope() = delete;
explicit DefaultTriggerAsyncIdScope(Environment* env,
double init_trigger_async_id);
explicit DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap);
~DefaultTriggerAsyncIdScope();

DefaultTriggerAsyncIdScope(const DefaultTriggerAsyncIdScope&) = delete;
DefaultTriggerAsyncIdScope& operator=(const DefaultTriggerAsyncIdScope&) =
delete;

private:
AsyncHooks* async_hooks_;
double old_default_trigger_async_id_;
};

private:
friend class Environment; // So we can call the constructor.
inline AsyncHooks();
// Keep a list of all Persistent strings used for Provider types.
v8::Eternal<v8::String> providers_[AsyncWrap::PROVIDERS_LENGTH];
// Stores the ids of the current execution context stack.
AliasedBuffer<double, v8::Float64Array> async_ids_stack_;
// Attached to a Uint32Array that tracks the number of active hooks for
// each type.
AliasedBuffer<uint32_t, v8::Uint32Array> fields_;
// Attached to a Float64Array that tracks the state of async resources.
AliasedBuffer<double, v8::Float64Array> async_id_fields_;

void grow_async_ids_stack();
};

class Environment {
public:
Environment(const Environment&) = delete;
Environment& operator=(const Environment&) = delete;

class AsyncCallbackScope {
public:
AsyncCallbackScope() = delete;
Expand Down
2 changes: 0 additions & 2 deletions src/pipe_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ using v8::Object;
using v8::String;
using v8::Value;

using AsyncHooks = Environment::AsyncHooks;

MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
AsyncWrap* parent,
PipeWrap::SocketType type) {
Expand Down
2 changes: 0 additions & 2 deletions src/stream_base-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ using v8::PropertyCallbackInfo;
using v8::String;
using v8::Value;

using AsyncHooks = Environment::AsyncHooks;

inline void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField),
nullptr);
Expand Down
2 changes: 0 additions & 2 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ using v8::String;
using v8::Uint32;
using v8::Value;

using AsyncHooks = Environment::AsyncHooks;

MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
AsyncWrap* parent,
TCPWrap::SocketType type) {
Expand Down
3 changes: 0 additions & 3 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ using v8::Uint32;
using v8::Undefined;
using v8::Value;

using AsyncHooks = Environment::AsyncHooks;


class SendWrap : public ReqWrap<uv_udp_send_t> {
public:
SendWrap(Environment* env, Local<Object> req_wrap_obj, bool have_callback);
Expand Down

0 comments on commit 4af9ff0

Please sign in to comment.