diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2021-08-23 09:32:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-23 09:32:41 +0100 |
| commit | 7a00ef1879719a6304a80eff7febae93273d862c (patch) | |
| tree | bf2879903e045c2ef4e6c08c8a86be96b1da1708 /src/thread_pool.cpp | |
| parent | daced956e3066ac536987217e6ee5bc1e909e306 (diff) | |
| parent | 35204e3cc5f860229a30fbe408206d9d3adaddce (diff) | |
Merge pull request #1096 from nakst/master
thread_pool.cpp: fix with 1 thread; gb.h: remove buggy /proc/cpuinfo code
Diffstat (limited to 'src/thread_pool.cpp')
| -rw-r--r-- | src/thread_pool.cpp | 38 |
1 files changed, 16 insertions, 22 deletions
diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index 5f21e0c3d..54d6cd72c 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -11,30 +11,27 @@ struct WorkerTask { struct ThreadPool { std::atomic<isize> outstanding_task_count; - WorkerTask *next_task; + WorkerTask *volatile next_task; BlockingMutex task_list_mutex; }; -void thread_pool_init(ThreadPool *pool, gbAllocator const &a, isize thread_count, char const *worker_prefix = nullptr); -void thread_pool_destroy(ThreadPool *pool); -void thread_pool_wait(ThreadPool *pool); -void thread_pool_add_task(ThreadPool *pool, WorkerTaskProc *proc, void *data); -void worker_thread_internal(); - void thread_pool_thread_entry(ThreadPool *pool) { while (pool->outstanding_task_count) { - mutex_lock(&pool->task_list_mutex); - - if (pool->next_task) { - WorkerTask *task = pool->next_task; - pool->next_task = task->next_task; - mutex_unlock(&pool->task_list_mutex); - task->do_work(task->data); - pool->outstanding_task_count.fetch_sub(1); - gb_free(heap_allocator(), task); + if (!pool->next_task) { + yield(); // No need to grab the mutex. } else { - mutex_unlock(&pool->task_list_mutex); - yield(); + mutex_lock(&pool->task_list_mutex); + + if (pool->next_task) { + WorkerTask *task = pool->next_task; + pool->next_task = task->next_task; + mutex_unlock(&pool->task_list_mutex); + task->do_work(task->data); + pool->outstanding_task_count.fetch_sub(1); + gb_free(heap_allocator(), task); + } else { + mutex_unlock(&pool->task_list_mutex); + } } } } @@ -77,10 +74,7 @@ void thread_pool_destroy(ThreadPool *pool) { void thread_pool_wait(ThreadPool *pool) { pool->outstanding_task_count.fetch_sub(1); - - while (pool->outstanding_task_count.load() != 0) { - yield(); - } + thread_pool_thread_entry(pool); } void thread_pool_add_task(ThreadPool *pool, WorkerTaskProc *proc, void *data) { |