Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: add hasRef() to the handle object #42756

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,12 @@ void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
}
}

void Worker::HasRef(const FunctionCallbackInfo<Value>& args) {
Worker* w;
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
args.GetReturnValue().Set(w->has_ref_);
}

void Worker::Unref(const FunctionCallbackInfo<Value>& args) {
Worker* w;
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
Expand Down Expand Up @@ -827,6 +833,7 @@ void InitWorker(Local<Object> target,

env->SetProtoMethod(w, "startThread", Worker::StartThread);
env->SetProtoMethod(w, "stopThread", Worker::StopThread);
env->SetProtoMethod(w, "hasRef", Worker::HasRef);
env->SetProtoMethod(w, "ref", Worker::Ref);
env->SetProtoMethod(w, "unref", Worker::Unref);
env->SetProtoMethod(w, "getResourceLimits", Worker::GetResourceLimits);
Expand Down Expand Up @@ -890,6 +897,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Worker::New);
registry->Register(Worker::StartThread);
registry->Register(Worker::StopThread);
registry->Register(Worker::HasRef);
registry->Register(Worker::Ref);
registry->Register(Worker::Unref);
registry->Register(Worker::GetResourceLimits);
Expand Down
1 change: 1 addition & 0 deletions src/node_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Worker : public AsyncWrap {
static void SetEnvVars(const v8::FunctionCallbackInfo<v8::Value>& args);
static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args);
static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args);
static void HasRef(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetResourceLimits(
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-worker-hasref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
const common = require('../common');

const { Worker } = require('worker_threads');
const { createHook } = require('async_hooks');
const { strictEqual } = require('assert');

let handle;

createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (type === 'WORKER') {
handle = resource;
this.disable();
}
}
}).enable();

const w = new Worker('', { eval: true });

strictEqual(handle.hasRef(), true);
w.unref();
strictEqual(handle.hasRef(), false);
w.ref();
strictEqual(handle.hasRef(), true);

w.on('exit', common.mustCall((exitCode) => {
strictEqual(exitCode, 0);
strictEqual(handle.hasRef(), true);
addaleax marked this conversation as resolved.
Show resolved Hide resolved
setTimeout(common.mustCall(() => {
strictEqual(handle.hasRef(), undefined);
}), 0);
}));