diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-03-20 19:05:02 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2019-03-27 10:30:09 -0400 |
commit | 51970537ee06f824160fcd035b018117ce264345 (patch) | |
tree | 824f710fce8db38c858e9deaa604a6964fd3c2bb | |
parent | a8eac78f8dbe778a2aef69d0989416e47c6c403e (diff) | |
download | node-new-51970537ee06f824160fcd035b018117ce264345.tar.gz |
src: move AsyncHooks out of Environment
PR-URL: https://github.com/nodejs/node/pull/26824
Refs: https://github.com/nodejs/node/issues/26776
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r-- | src/api/callback.cc | 2 | ||||
-rw-r--r-- | src/async_wrap-inl.h | 10 | ||||
-rw-r--r-- | src/async_wrap.cc | 1 | ||||
-rw-r--r-- | src/env-inl.h | 35 | ||||
-rw-r--r-- | src/env.cc | 3 | ||||
-rw-r--r-- | src/env.h | 143 | ||||
-rw-r--r-- | src/pipe_wrap.cc | 2 | ||||
-rw-r--r-- | src/stream_base-inl.h | 2 | ||||
-rw-r--r-- | src/tcp_wrap.cc | 2 | ||||
-rw-r--r-- | src/udp_wrap.cc | 3 |
10 files changed, 91 insertions, 112 deletions
diff --git a/src/api/callback.cc b/src/api/callback.cc index 47a1549e93..7c6e8d8697 100644 --- a/src/api/callback.cc +++ b/src/api/callback.cc @@ -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) diff --git a/src/async_wrap-inl.h b/src/async_wrap-inl.h index 4405bb3a9b..6ef968933b 100644 --- a/src/async_wrap-inl.h +++ b/src/async_wrap-inl.h @@ -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()); } @@ -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()) {} diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 582744ff2d..0cee1b2db1 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -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 { diff --git a/src/env-inl.h b/src/env-inl.h index 79c9312e66..8d2327034a 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -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) { @@ -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) { @@ -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; @@ -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; @@ -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); @@ -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_; } @@ -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_; } diff --git a/src/env.cc b/src/env.cc index 47fabf5e2b..608bada5d7 100644 --- a/src/env.cc +++ b/src/env.cc @@ -818,8 +818,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( @@ -537,87 +537,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; diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index a6f6f7bb37..cafa33f9af 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -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) { diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index dbf3c07b92..08a02e186c 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -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); diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 4d16fa4141..aedb079ccf 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -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) { diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index f1693aa39b..e568bb66a6 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -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); |