Skip to content

Commit 2a483fa

Browse files
committed
WorkerThreadPool: Refactor running and exit-requested as runlevels
1 parent 2640960 commit 2a483fa

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

core/object/worker_thread_pool.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
521537
void 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

696712
void 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
}

core/object/worker_thread_pool.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ class WorkerThreadPool : public Object {
124124
};
125125

126126
TightLocalVector<ThreadData> threads;
127-
bool exit_threads = false;
127+
enum Runlevel {
128+
RUNLEVEL_NORMAL,
129+
RUNLEVEL_EXIT,
130+
} runlevel = RUNLEVEL_NORMAL;
128131

129132
HashMap<Thread::ID, int> thread_ids;
130133
HashMap<
@@ -193,6 +196,9 @@ class WorkerThreadPool : public Object {
193196

194197
void _wait_collaboratively(ThreadData *p_caller_pool_thread, Task *p_task);
195198

199+
void _switch_runlevel(Runlevel p_runlevel);
200+
bool _handle_runlevel();
201+
196202
#ifdef THREADS_ENABLED
197203
static uint32_t _thread_enter_unlock_allowance_zone(THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &p_ulock);
198204
#endif

0 commit comments

Comments
 (0)