radlink: handle thread pool release correctly

This commit is contained in:
Nikita Smith authored and Ryan Fleury committed 2026-09-21 09:08:04 -07:00
1 parent 458e5029db
commit 4663d1a89a
1 file changed
+21 -10
+21 -10
View File
@@ -1,11 +1,16 @@
// Copyright (c) Epic Games Tools // Copyright (c) Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/) // Licensed under the MIT license (https://opensource.org/license/mit/)
internal void internal B32
tp_run_tasks(TP_Context *pool, TP_Worker *worker) tp_run_tasks(TP_Context *pool, TP_Worker *worker)
{ {
barrier_wait(pool->run_barrier); barrier_wait(pool->run_barrier);
// @pool_shutdown
if (!pool->is_live) {
return 0;
}
for (;;) { for (;;) {
S64 task_left = ins_atomic_u64_dec_eval(&pool->task_left); S64 task_left = ins_atomic_u64_dec_eval(&pool->task_left);
@@ -24,6 +29,7 @@ tp_run_tasks(TP_Context *pool, TP_Worker *worker)
} }
barrier_wait(pool->run_barrier); barrier_wait(pool->run_barrier);
return 1;
} }
internal void internal void
@@ -31,9 +37,7 @@ tp_worker_main(void *raw_worker)
{ {
TP_Worker *worker = raw_worker; TP_Worker *worker = raw_worker;
TP_Context *pool = worker->pool; TP_Context *pool = worker->pool;
for (; pool->is_live; ) { while (tp_run_tasks(pool, worker)) {}
tp_run_tasks(pool, worker);
}
} }
internal void internal void
@@ -41,9 +45,11 @@ tp_worker_main_shared(void *raw_worker)
{ {
TP_Worker *worker = raw_worker; TP_Worker *worker = raw_worker;
TP_Context *pool = worker->pool; TP_Context *pool = worker->pool;
for (; pool->is_live; ) { for (;;) {
if (semaphore_take(pool->exec_semaphore, max_U64)) { if (semaphore_take(pool->exec_semaphore, max_U64)) {
tp_run_tasks(pool, worker); if (!tp_run_tasks(pool, worker)) {
break;
}
} }
} }
} }
@@ -101,16 +107,21 @@ tp_release(TP_Context *pool)
B32 is_shared = pool->exec_semaphore.u64[0] != 0; B32 is_shared = pool->exec_semaphore.u64[0] != 0;
if (is_shared) { if (is_shared) {
for EachIndex(i, pool->worker_count) { semaphore_drop_count(pool->exec_semaphore, pool->worker_count - 1);
semaphore_drop(pool->exec_semaphore);
}
} }
// rendezvous with all workers at the task dispatch barrier so
// they observe @pool_shutdown and exit without running another batch
barrier_wait(pool->run_barrier);
for (U64 i = 1; i < pool->worker_count; i += 1) { for (U64 i = 1; i < pool->worker_count; i += 1) {
thread_detach(pool->worker_arr[i].handle); thread_join(pool->worker_arr[i].handle, max_U64);
} }
if (is_shared) { if (is_shared) {
semaphore_release(pool->exec_semaphore); semaphore_release(pool->exec_semaphore);
} }
barrier_release(pool->run_barrier); barrier_release(pool->run_barrier);
barrier_release(pool->barrier); barrier_release(pool->barrier);