From 0b01cfd85300e1c37579dcd5c7787bdf57757a36 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 16 Jan 2023 18:18:08 +0000 Subject: [PATCH] Fix minor possible race condition --- src/main.cpp | 2 +- src/thread_pool.cpp | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ef827c77c..f08e42670 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,7 @@ gb_global ThreadPool global_thread_pool; gb_internal void init_global_thread_pool(void) { isize thread_count = gb_max(build_context.thread_count, 1); isize worker_count = thread_count; // +1 - thread_pool_init(&global_thread_pool, permanent_allocator(), worker_count, "ThreadPoolWorker"); + thread_pool_init(&global_thread_pool, worker_count, "ThreadPoolWorker"); } gb_internal bool thread_pool_add_task(WorkerTaskProc *proc, void *data) { return thread_pool_add_task(&global_thread_pool, proc, data); diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index 2c369eaad..5dbbe37c4 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -5,14 +5,13 @@ struct ThreadPool; gb_thread_local Thread *current_thread; -gb_internal void thread_pool_init(ThreadPool *pool, gbAllocator const &a, isize worker_count, char const *worker_name); +gb_internal void thread_pool_init(ThreadPool *pool, isize worker_count, char const *worker_name); gb_internal void thread_pool_destroy(ThreadPool *pool); gb_internal bool thread_pool_add_task(ThreadPool *pool, WorkerTaskProc *proc, void *data); gb_internal void thread_pool_wait(ThreadPool *pool); struct ThreadPool { - gbAllocator allocator; - + gbAllocator threads_allocator; Slice threads; std::atomic running; @@ -25,9 +24,9 @@ gb_internal isize current_thread_index(void) { return current_thread ? current_thread->idx : 0; } -gb_internal void thread_pool_init(ThreadPool *pool, gbAllocator const &a, isize worker_count, char const *worker_name) { - pool->allocator = a; - slice_init(&pool->threads, a, worker_count + 1); +gb_internal void thread_pool_init(ThreadPool *pool, isize worker_count, char const *worker_name) { + pool->threads_allocator = permanent_allocator(); + slice_init(&pool->threads, pool->threads_allocator, worker_count + 1); // NOTE: this needs to be initialized before any thread starts pool->running.store(true, std::memory_order_seq_cst); @@ -52,7 +51,7 @@ gb_internal void thread_pool_destroy(ThreadPool *pool) { thread_join_and_destroy(t); } - gb_free(pool->allocator, pool->threads.data); + gb_free(pool->threads_allocator, pool->threads.data); } void thread_pool_queue_push(Thread *thread, WorkerTask task) {