@@ -186,7 +186,8 @@ void WorkerThreadPool::_thread_function(void *p_user) {
186186 {
187187 MutexLock lock (singleton->task_mutex );
188188
189- if (unlikely (singleton->exit_threads )) {
189+ bool exit = singleton->_handle_runlevel ();
190+ if (unlikely (exit)) {
190191 break ;
191192 }
192193
@@ -453,7 +454,8 @@ void WorkerThreadPool::_wait_collaboratively(ThreadData *p_caller_pool_thread, T
453454 bool was_signaled = p_caller_pool_thread->signaled ;
454455 p_caller_pool_thread->signaled = false ;
455456
456- if (unlikely (exit_threads)) {
457+ bool exit = _handle_runlevel ();
458+ if (unlikely (exit)) {
457459 break ;
458460 }
459461
@@ -518,6 +520,20 @@ void WorkerThreadPool::_wait_collaboratively(ThreadData *p_caller_pool_thread, T
518520 }
519521}
520522
523+ void WorkerThreadPool::_switch_runlevel (Runlevel p_runlevel) {
524+ DEV_ASSERT (p_runlevel > runlevel);
525+ runlevel = p_runlevel;
526+ for (uint32_t i = 0 ; i < threads.size (); i++) {
527+ threads[i].cond_var .notify_one ();
528+ threads[i].signaled = true ;
529+ }
530+ }
531+
532+ // Returns whether threads have to exit. This may perform the check about handling needed.
533+ bool WorkerThreadPool::_handle_runlevel () {
534+ return runlevel == RUNLEVEL_EXIT;
535+ }
536+
521537void WorkerThreadPool::yield () {
522538 int th_index = get_thread_index ();
523539 ERR_FAIL_COND_MSG (th_index == -1 , " This function can only be called from a worker thread." );
@@ -695,6 +711,9 @@ void WorkerThreadPool::thread_exit_unlock_allowance_zone(uint32_t p_zone_id) {
695711
696712void WorkerThreadPool::init (int p_thread_count, float p_low_priority_task_ratio) {
697713 ERR_FAIL_COND (threads.size () > 0 );
714+
715+ runlevel = RUNLEVEL_NORMAL;
716+
698717 if (p_thread_count < 0 ) {
699718 p_thread_count = OS::get_singleton ()->get_default_thread_pool_size ();
700719 }
@@ -724,15 +743,10 @@ void WorkerThreadPool::finish() {
724743 print_error (" Task waiting was never re-claimed: " + E->self ()->description );
725744 E = E->next ();
726745 }
727- }
728746
729- {
730- MutexLock lock (task_mutex);
731- exit_threads = true ;
732- }
733- for (ThreadData &data : threads) {
734- data.cond_var .notify_one ();
747+ _switch_runlevel (RUNLEVEL_EXIT);
735748 }
749+
736750 for (ThreadData &data : threads) {
737751 data.thread .wait_to_finish ();
738752 }
0 commit comments