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 <cjihrig@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bnoordhuis authored and targos committed May 13, 2020
1 parent 58bd92a commit 4d7f986
Showing 1 changed file with 13 additions and 33 deletions.
46 changes: 13 additions & 33 deletions src/node_api.cc
Expand Up @@ -227,8 +227,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 @@ -268,7 +268,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 @@ -294,43 +293,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 @@ -412,7 +392,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 4d7f986

Please sign in to comment.