worker: make MessagePort uv_async_t
inline field
It’s not obvious why this was a heap allocation in the first place, but it’s unneccessary. Most other `HandleWrap`s also store the libuv handle directly. PR-URL: https://github.com/nodejs/node/pull/26271 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
This commit is contained in:
parent
4dd7c4b2e4
commit
0fc40c8049
@ -469,18 +469,18 @@ MessagePort::MessagePort(Environment* env,
|
|||||||
Local<Object> wrap)
|
Local<Object> wrap)
|
||||||
: HandleWrap(env,
|
: HandleWrap(env,
|
||||||
wrap,
|
wrap,
|
||||||
reinterpret_cast<uv_handle_t*>(new uv_async_t()),
|
reinterpret_cast<uv_handle_t*>(&async_),
|
||||||
AsyncWrap::PROVIDER_MESSAGEPORT),
|
AsyncWrap::PROVIDER_MESSAGEPORT),
|
||||||
data_(new MessagePortData(this)) {
|
data_(new MessagePortData(this)) {
|
||||||
auto onmessage = [](uv_async_t* handle) {
|
auto onmessage = [](uv_async_t* handle) {
|
||||||
// Called when data has been put into the queue.
|
// Called when data has been put into the queue.
|
||||||
MessagePort* channel = static_cast<MessagePort*>(handle->data);
|
MessagePort* channel = ContainerOf(&MessagePort::async_, handle);
|
||||||
channel->OnMessage();
|
channel->OnMessage();
|
||||||
};
|
};
|
||||||
CHECK_EQ(uv_async_init(env->event_loop(),
|
CHECK_EQ(uv_async_init(env->event_loop(),
|
||||||
async(),
|
&async_,
|
||||||
onmessage), 0);
|
onmessage), 0);
|
||||||
async()->data = static_cast<void*>(this);
|
async_.data = static_cast<void*>(this);
|
||||||
|
|
||||||
Local<Value> fn;
|
Local<Value> fn;
|
||||||
if (!wrap->Get(context, env->oninit_symbol()).ToLocal(&fn))
|
if (!wrap->Get(context, env->oninit_symbol()).ToLocal(&fn))
|
||||||
@ -494,17 +494,13 @@ MessagePort::MessagePort(Environment* env,
|
|||||||
Debug(this, "Created message port");
|
Debug(this, "Created message port");
|
||||||
}
|
}
|
||||||
|
|
||||||
uv_async_t* MessagePort::async() {
|
|
||||||
return reinterpret_cast<uv_async_t*>(GetHandle());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MessagePort::IsDetached() const {
|
bool MessagePort::IsDetached() const {
|
||||||
return data_ == nullptr || IsHandleClosing();
|
return data_ == nullptr || IsHandleClosing();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MessagePort::TriggerAsync() {
|
void MessagePort::TriggerAsync() {
|
||||||
if (IsHandleClosing()) return;
|
if (IsHandleClosing()) return;
|
||||||
CHECK_EQ(uv_async_send(async()), 0);
|
CHECK_EQ(uv_async_send(&async_), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MessagePort::Close(v8::Local<v8::Value> close_callback) {
|
void MessagePort::Close(v8::Local<v8::Value> close_callback) {
|
||||||
@ -639,7 +635,6 @@ void MessagePort::OnClose() {
|
|||||||
data_->Disentangle();
|
data_->Disentangle();
|
||||||
}
|
}
|
||||||
data_.reset();
|
data_.reset();
|
||||||
delete async();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<MessagePortData> MessagePort::Detach() {
|
std::unique_ptr<MessagePortData> MessagePort::Detach() {
|
||||||
|
@ -198,9 +198,9 @@ class MessagePort : public HandleWrap {
|
|||||||
void OnClose() override;
|
void OnClose() override;
|
||||||
void OnMessage();
|
void OnMessage();
|
||||||
void TriggerAsync();
|
void TriggerAsync();
|
||||||
inline uv_async_t* async();
|
|
||||||
|
|
||||||
std::unique_ptr<MessagePortData> data_ = nullptr;
|
std::unique_ptr<MessagePortData> data_ = nullptr;
|
||||||
|
uv_async_t async_;
|
||||||
|
|
||||||
friend class MessagePortData;
|
friend class MessagePortData;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user