From eb0775ad53e9651ca01ad236dd3fe83786f0cecc Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 20 Dec 2022 14:45:01 +0000 Subject: [PATCH] Move `mutex` use around in thread pool --- src/thread_pool.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index a1ee11523..6df991d7d 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -83,10 +83,8 @@ gb_internal void thread_pool_queue_push(ThreadPool *pool, WorkerTask *task) { gb_internal bool thread_pool_add_task(ThreadPool *pool, WorkerTaskProc *proc, void *data) { GB_ASSERT(proc != nullptr); - mutex_lock(&pool->mutex); WorkerTask *task = gb_alloc_item(permanent_allocator(), WorkerTask); if (task == nullptr) { - mutex_unlock(&pool->mutex); GB_PANIC("Out of memory"); return false; } @@ -94,9 +92,10 @@ gb_internal bool thread_pool_add_task(ThreadPool *pool, WorkerTaskProc *proc, vo task->do_work = proc; task->data = data; + mutex_lock(&pool->mutex); thread_pool_queue_push(pool, task); GB_ASSERT(pool->ready >= 0); - pool->ready++; + pool->ready.fetch_add(1); condition_broadcast(&pool->task_cond); mutex_unlock(&pool->mutex); return true; @@ -111,7 +110,7 @@ gb_internal void thread_pool_wait(ThreadPool *pool) { if (pool->threads.count == 0) { while (!thread_pool_queue_empty(pool)) { thread_pool_do_task(thread_pool_queue_pop(pool)); - --pool->ready; + pool->ready.fetch_sub(1); } GB_ASSERT(pool->ready == 0); return;