mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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>
This commit is contained in:
1 parent
a545cfe293
commit
4af9ff00ff
10 files changed
+95
-116
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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()) {}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+15
-20
@@ -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_;
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -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(
|
||||
|
||||
@@ -532,87 +532,86 @@ class AsyncRequest : public MemoryRetainer {
|
||||
std::atomic_bool stopped_ {true};
|
||||
};
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
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_;
|
||||
};
|
||||
|
||||
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 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,
|
||||
};
|
||||
|
||||
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_;
|
||||
};
|
||||
|
||||
|
||||
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 AsyncCallbackScope {
|
||||
public:
|
||||
AsyncCallbackScope() = delete;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user