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

n-api: simplify uv_idle wrangling #32997

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 13 additions & 33 deletions src/node_api.cc
Expand Up @@ -250,8 +250,8 @@ class ThreadSafeFunction : public node::AsyncResource {
if (max_queue_size > 0) {
cond = std::make_unique<node::ConditionVariable>();
}
if ((max_queue_size == 0 || cond) &&
uv_idle_init(loop, &idle) == 0) {
if (max_queue_size == 0 || cond) {
CHECK_EQ(0, uv_idle_init(loop, &idle));
return napi_ok;
}

Expand Down Expand Up @@ -291,7 +291,6 @@ class ThreadSafeFunction : public node::AsyncResource {
void DispatchOne() {
void* data = nullptr;
bool popped_value = false;
bool idle_stop_failed = false;

{
node::Mutex::ScopedLock lock(this->mutex);
Expand All @@ -317,43 +316,24 @@ class ThreadSafeFunction : public node::AsyncResource {
}
CloseHandlesAndMaybeDelete();
} else {
if (uv_idle_stop(&idle) != 0) {
idle_stop_failed = true;
}
CHECK_EQ(0, uv_idle_stop(&idle));
}
}
}
}

if (popped_value || idle_stop_failed) {
if (popped_value) {
v8::HandleScope scope(env->isolate);
CallbackScope cb_scope(this);

if (idle_stop_failed) {
CHECK(napi_throw_error(env,
"ERR_NAPI_TSFN_STOP_IDLE_LOOP",
"Failed to stop the idle loop") == napi_ok);
} else {
napi_value js_callback = nullptr;
if (!ref.IsEmpty()) {
v8::Local<v8::Function> js_cb =
v8::Local<v8::Function>::New(env->isolate, ref);
js_callback = v8impl::JsValueFromV8LocalValue(js_cb);
}
env->CallIntoModuleThrow([&](napi_env env) {
call_js_cb(env, js_callback, context, data);
});
napi_value js_callback = nullptr;
if (!ref.IsEmpty()) {
v8::Local<v8::Function> js_cb =
v8::Local<v8::Function>::New(env->isolate, ref);
js_callback = v8impl::JsValueFromV8LocalValue(js_cb);
}
}
}

void MaybeStartIdle() {
if (uv_idle_start(&idle, IdleCb) != 0) {
v8::HandleScope scope(env->isolate);
CallbackScope cb_scope(this);
CHECK(napi_throw_error(env,
"ERR_NAPI_TSFN_START_IDLE_LOOP",
"Failed to start the idle loop") == napi_ok);
env->CallIntoModuleThrow([&](napi_env env) {
call_js_cb(env, js_callback, context, data);
});
}
}

Expand Down Expand Up @@ -435,7 +415,7 @@ class ThreadSafeFunction : public node::AsyncResource {
static void AsyncCb(uv_async_t* async) {
ThreadSafeFunction* ts_fn =
node::ContainerOf(&ThreadSafeFunction::async, async);
ts_fn->MaybeStartIdle();
CHECK_EQ(0, uv_idle_start(&ts_fn->idle, IdleCb));
}

static void Cleanup(void* data) {
Expand Down