Skip to content

Commit

Permalink
src: add async context frame to AsyncResource
Browse files Browse the repository at this point in the history
Add member to hold the async context frame to AsyncResource to avoid
the need for the async_resource_context_frames_ map in env.

Semver major because it changes ABI.

PR-URL: #56082
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
  • Loading branch information
Flarna authored Dec 1, 2024
1 parent de5f91d commit 58982d7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 48 deletions.
25 changes: 8 additions & 17 deletions src/api/async_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,23 @@ AsyncResource::AsyncResource(Isolate* isolate,
const char* name,
async_id trigger_async_id)
: env_(Environment::GetCurrent(isolate)),
resource_(isolate, resource) {
resource_(isolate, resource),
context_frame_(isolate, async_context_frame::current(isolate)) {
CHECK_NOT_NULL(env_);
env_->SetAsyncResourceContextFrame(
reinterpret_cast<std::uintptr_t>(this),
{isolate, async_context_frame::current(isolate)});
async_context_ = EmitAsyncInit(isolate, resource, name, trigger_async_id);
}

AsyncResource::~AsyncResource() {
CHECK_NOT_NULL(env_);
EmitAsyncDestroy(env_, async_context_);
env_->RemoveAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this));
}

MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
auto context_frame =
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
.Get(isolate);
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
async_context_frame::Scope async_context_frame_scope(
isolate, context_frame_.Get(isolate));

return node::MakeCallback(
isolate, get_resource(), callback, argc, argv, async_context_);
Expand All @@ -48,10 +43,8 @@ MaybeLocal<Value> AsyncResource::MakeCallback(const char* method,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
auto context_frame =
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
.Get(isolate);
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
async_context_frame::Scope async_context_frame_scope(
isolate, context_frame_.Get(isolate));

return node::MakeCallback(
isolate, get_resource(), method, argc, argv, async_context_);
Expand All @@ -61,10 +54,8 @@ MaybeLocal<Value> AsyncResource::MakeCallback(Local<String> symbol,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
auto context_frame =
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
.Get(isolate);
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
async_context_frame::Scope async_context_frame_scope(
isolate, context_frame_.Get(isolate));

return node::MakeCallback(
isolate, get_resource(), symbol, argc, argv, async_context_);
Expand Down
20 changes: 0 additions & 20 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -908,26 +908,6 @@ inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback(
heap_limit);
}

inline void Environment::SetAsyncResourceContextFrame(
std::uintptr_t async_resource_handle,
v8::Global<v8::Value>&& context_frame) {
async_resource_context_frames_.emplace(
std::make_pair(async_resource_handle, std::move(context_frame)));
}

inline const v8::Global<v8::Value>& Environment::GetAsyncResourceContextFrame(
std::uintptr_t async_resource_handle) {
auto&& async_resource_context_frame =
async_resource_context_frames_.find(async_resource_handle);
CHECK_NE(async_resource_context_frame, async_resource_context_frames_.end());

return async_resource_context_frame->second;
}

inline void Environment::RemoveAsyncResourceContextFrame(
std::uintptr_t async_resource_handle) {
async_resource_context_frames_.erase(async_resource_handle);
}
} // namespace node

// These two files depend on each other. Including base_object-inl.h after this
Expand Down
11 changes: 0 additions & 11 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1070,14 +1070,6 @@ class Environment final : public MemoryRetainer {

v8::Global<v8::Module> temporary_required_module_facade_original;

void SetAsyncResourceContextFrame(std::uintptr_t async_resource_handle,
v8::Global<v8::Value>&&);

const v8::Global<v8::Value>& GetAsyncResourceContextFrame(
std::uintptr_t async_resource_handle);

void RemoveAsyncResourceContextFrame(std::uintptr_t async_resource_handle);

private:
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>,
v8::Local<v8::Value>),
Expand Down Expand Up @@ -1252,9 +1244,6 @@ class Environment final : public MemoryRetainer {
// track of the BackingStore for a given pointer.
std::unordered_map<char*, std::unique_ptr<v8::BackingStore>>
released_allocated_buffers_;

std::unordered_map<std::uintptr_t, v8::Global<v8::Value>>
async_resource_context_frames_;
};

} // namespace node
Expand Down
1 change: 1 addition & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,7 @@ class NODE_EXTERN AsyncResource {
private:
Environment* env_;
v8::Global<v8::Object> resource_;
v8::Global<v8::Value> context_frame_;
async_context async_context_;
};

Expand Down

0 comments on commit 58982d7

Please sign in to comment.