@@ -376,10 +376,10 @@ bool Worker::CreateEnvMessagePort(Environment* env) {
376
376
}
377
377
378
378
void Worker::JoinThread () {
379
- if (thread_joined_ )
379
+ if (!tid_. has_value () )
380
380
return ;
381
- CHECK_EQ (uv_thread_join (&tid_), 0 );
382
- thread_joined_ = true ;
381
+ CHECK_EQ (uv_thread_join (&tid_. value () ), 0 );
382
+ tid_. reset () ;
383
383
384
384
env ()->remove_sub_worker_context (this );
385
385
@@ -406,7 +406,7 @@ void Worker::JoinThread() {
406
406
MakeCallback (env ()->onexit_string (), arraysize (args), args);
407
407
}
408
408
409
- // If we get here, the !thread_joined_ condition at the top of the function
409
+ // If we get here, the tid_.has_value() condition at the top of the function
410
410
// implies that the thread was running. In that case, its final action will
411
411
// be to schedule a callback on the parent thread which will delete this
412
412
// object, so there's nothing more to do here.
@@ -417,7 +417,7 @@ Worker::~Worker() {
417
417
418
418
CHECK (stopped_);
419
419
CHECK_NULL (env_);
420
- CHECK (thread_joined_ );
420
+ CHECK (!tid_. has_value () );
421
421
422
422
Debug (this , " Worker %llu destroyed" , thread_id_.id );
423
423
}
@@ -598,7 +598,9 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
598
598
uv_thread_options_t thread_options;
599
599
thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
600
600
thread_options.stack_size = w->stack_size_ ;
601
- int ret = uv_thread_create_ex (&w->tid_ , &thread_options, [](void * arg) {
601
+
602
+ uv_thread_t * tid = &w->tid_ .emplace (); // Create uv_thread_t instance
603
+ int ret = uv_thread_create_ex (tid, &thread_options, [](void * arg) {
602
604
// XXX: This could become a std::unique_ptr, but that makes at least
603
605
// gcc 6.3 detect undefined behaviour when there shouldn't be any.
604
606
// gcc 7+ handles this well.
@@ -625,14 +627,14 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
625
627
// The object now owns the created thread and should not be garbage
626
628
// collected until that finishes.
627
629
w->ClearWeak ();
628
- w->thread_joined_ = false ;
629
630
630
631
if (w->has_ref_ )
631
632
w->env ()->add_refs (1 );
632
633
633
634
w->env ()->add_sub_worker_context (w);
634
635
} else {
635
636
w->stopped_ = true ;
637
+ w->tid_ .reset ();
636
638
637
639
char err_buf[128 ];
638
640
uv_err_name_r (ret, err_buf, sizeof (err_buf));
@@ -655,7 +657,7 @@ void Worker::StopThread(const FunctionCallbackInfo<Value>& args) {
655
657
void Worker::Ref (const FunctionCallbackInfo<Value>& args) {
656
658
Worker* w;
657
659
ASSIGN_OR_RETURN_UNWRAP (&w, args.This ());
658
- if (!w->has_ref_ && ! w->thread_joined_ ) {
660
+ if (!w->has_ref_ && w->tid_ . has_value () ) {
659
661
w->has_ref_ = true ;
660
662
w->env ()->add_refs (1 );
661
663
}
@@ -664,7 +666,7 @@ void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
664
666
void Worker::Unref (const FunctionCallbackInfo<Value>& args) {
665
667
Worker* w;
666
668
ASSIGN_OR_RETURN_UNWRAP (&w, args.This ());
667
- if (w->has_ref_ && ! w->thread_joined_ ) {
669
+ if (w->has_ref_ && w->tid_ . has_value () ) {
668
670
w->has_ref_ = false ;
669
671
w->env ()->add_refs (-1 );
670
672
}
0 commit comments