diff options
author | Anna Henningsen <anna@addaleax.net> | 2020-03-30 11:35:16 +0300 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2020-04-11 11:05:39 +0300 |
commit | 0faaa7c84cfe3dfe52e3fa8d175843e3eed49bd6 (patch) | |
tree | 960bf02a6c267c09c821be826a03a9162c378986 /src/node_worker.cc | |
parent | b6f71969a0530d987adbd2ebee44a51d2655029d (diff) |
src: clean up worker thread creation code
Instead of setting and then in the case of error un-setting properties,
only set them when no error occurs.
Refs: https://github.com/nodejs/node/pull/32344
PR-URL: https://github.com/nodejs/node/pull/32562
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_worker.cc')
-rw-r--r-- | src/node_worker.cc | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/src/node_worker.cc b/src/node_worker.cc index 154fb8d6ae5..2e91ee54db4 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -609,16 +609,7 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) { ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); Mutex::ScopedLock lock(w->mutex_); - // The object now owns the created thread and should not be garbage collected - // until that finishes. - w->ClearWeak(); - - w->env()->add_sub_worker_context(w); w->stopped_ = false; - w->thread_joined_ = false; - - if (w->has_ref_) - w->env()->add_refs(1); uv_thread_options_t thread_options; thread_options.flags = UV_THREAD_HAS_STACK_SIZE; @@ -645,21 +636,27 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) { // implicitly delete w }); }, static_cast<void*>(w)); - if (ret != 0) { + + if (ret == 0) { + // The object now owns the created thread and should not be garbage + // collected until that finishes. + w->ClearWeak(); + w->thread_joined_ = false; + + if (w->has_ref_) + w->env()->add_refs(1); + + w->env()->add_sub_worker_context(w); + } else { + w->stopped_ = true; + char err_buf[128]; uv_err_name_r(ret, err_buf, sizeof(err_buf)); - w->custom_error_ = "ERR_WORKER_INIT_FAILED"; - w->custom_error_str_ = err_buf; - w->loop_init_failed_ = true; - w->thread_joined_ = true; - w->stopped_ = true; - w->env()->remove_sub_worker_context(w); { Isolate* isolate = w->env()->isolate(); HandleScope handle_scope(isolate); THROW_ERR_WORKER_INIT_FAILED(isolate, err_buf); } - w->MakeWeak(); } } |