Skip to content

Commit

Permalink
n-api: simplify uv_idle wrangling
Browse files Browse the repository at this point in the history
uv_idle_init(), uv_idle_start() and uv_idle_stop() always succeed.
Remove the superfluous error handling.

Refs: libuv/libuv#2803

PR-URL: #32997
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
bnoordhuis authored and targos committed May 7, 2020
1 parent 85c3488 commit 72bca64
Showing 1 changed file with 13 additions and 33 deletions.
46 changes: 13 additions & 33 deletions src/node_api.cc
Original file line number Diff line number Diff line change
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

0 comments on commit 72bca64

Please sign in to comment.