@@ -462,15 +462,16 @@ void Environment::InitializeLibuv(bool start_profiler_idle_notifier) {
462462 uv_check_init (event_loop (), &idle_check_handle_);
463463 uv_async_init (
464464 event_loop (),
465- &cleanup_finalization_groups_async_ ,
465+ &task_queues_async_ ,
466466 [](uv_async_t * async) {
467467 Environment* env = ContainerOf (
468- &Environment::cleanup_finalization_groups_async_ , async);
468+ &Environment::task_queues_async_ , async);
469469 env->CleanupFinalizationGroups ();
470+ env->RunAndClearNativeImmediates ();
470471 });
471472 uv_unref (reinterpret_cast <uv_handle_t *>(&idle_prepare_handle_));
472473 uv_unref (reinterpret_cast <uv_handle_t *>(&idle_check_handle_));
473- uv_unref (reinterpret_cast <uv_handle_t *>(&cleanup_finalization_groups_async_ ));
474+ uv_unref (reinterpret_cast <uv_handle_t *>(&task_queues_async_ ));
474475
475476 thread_stopper ()->Install (
476477 this , static_cast <void *>(this ), [](uv_async_t * handle) {
@@ -534,7 +535,7 @@ void Environment::RegisterHandleCleanups() {
534535 close_and_finish,
535536 nullptr );
536537 RegisterHandleCleanup (
537- reinterpret_cast <uv_handle_t *>(&cleanup_finalization_groups_async_ ),
538+ reinterpret_cast <uv_handle_t *>(&task_queues_async_ ),
538539 close_and_finish,
539540 nullptr );
540541}
@@ -665,6 +666,15 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
665666 " RunAndClearNativeImmediates" , this );
666667 size_t ref_count = 0 ;
667668
669+ // It is safe to check .size() first, because there is a causal relationship
670+ // between pushes to the threadsafe and this function being called.
671+ // For the common case, it's worth checking the size first before establishing
672+ // a mutex lock.
673+ if (native_immediates_threadsafe_.size () > 0 ) {
674+ Mutex::ScopedLock lock (native_immediates_threadsafe_mutex_);
675+ native_immediates_.ConcatMove (std::move (native_immediates_threadsafe_));
676+ }
677+
668678 NativeImmediateQueue queue;
669679 queue.ConcatMove (std::move (native_immediates_));
670680
@@ -1078,7 +1088,7 @@ void Environment::CleanupFinalizationGroups() {
10781088 if (try_catch.HasCaught () && !try_catch.HasTerminated ())
10791089 errors::TriggerUncaughtException (isolate (), try_catch);
10801090 // Re-schedule the execution of the remainder of the queue.
1081- uv_async_send (&cleanup_finalization_groups_async_ );
1091+ uv_async_send (&task_queues_async_ );
10821092 return ;
10831093 }
10841094 }
0 commit comments