From f947590b909b287c0a724def6ef03937f85c361b Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Tue, 9 Jan 2024 13:37:25 -0500 Subject: refactor and simplify bucketed-array --- src/thread_pool.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'src/thread_pool.cpp') diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index 050db20..4a67054 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -1,6 +1,8 @@ #include "thread_pool.hpp" +#include "bucketed-array.hpp" + #include #include @@ -17,11 +19,8 @@ struct ThreadPool { }; const PkeHandleItemIndex_T MAX_THREADS_PER_BUCKET = 8; -struct ThreadBucket { - ThreadPool threadPools[MAX_THREADS_PER_BUCKET]; -}; -BucketContainer ThreadPool_BucketContainer{}; +BucketContainer ThreadPool_BucketContainer{}; void ThreadRun(ThreadPool *tp) { std::packaged_task *j = nullptr; @@ -126,14 +125,13 @@ void inline PkeThreads_Shutdown_Inner(ThreadPool &tp) { } ThreadPoolHandle PkeThreads_Init(uint8_t threadCount, uint8_t maxQueueCount, MemBucket *bkt) { - if (!ThreadPool_BucketContainer.buckets) { - Buckets_Init(ThreadPool_BucketContainer); + if (ThreadPool_BucketContainer.pkeHandle.hash == 0) { + Buckets_Init(ThreadPool_BucketContainer, MAX_THREADS_PER_BUCKET); } assert(threadCount > 0); - bool moved; - ThreadPoolHandle newHandle{Buckets_NewHandle(255, ThreadPool_BucketContainer, moved)}; + ThreadPoolHandle newHandle{Buckets_NewHandle(ThreadPool_BucketContainer)}; - auto *tp = &ThreadPool_BucketContainer.buckets[newHandle.bucketIndex].threadPools[newHandle.itemIndex]; + auto *tp = &ThreadPool_BucketContainer.buckets[newHandle.bucketIndex][newHandle.itemIndex]; tp->bkt = bkt; tp->isRunning = true; @@ -153,13 +151,13 @@ ThreadPoolHandle PkeThreads_Init(uint8_t threadCount, uint8_t maxQueueCount, Mem void PkeThreads_Reset(ThreadPoolHandle handle) { assert(handle != ThreadPoolHandle_MAX); - auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex][handle.itemIndex]; PkeThreads_Reset_Inner(*tp); } bool PkeThreads_Enqueue(ThreadPoolHandle handle, std::packaged_task *job) { assert(handle != ThreadPoolHandle_MAX); - auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex][handle.itemIndex]; if (tp->bkt != nullptr) { /* 2023-12-22 JCB * Note that if this becomes an issue we can change it. @@ -173,27 +171,27 @@ bool PkeThreads_Enqueue(ThreadPoolHandle handle, std::packaged_task *job } int64_t PkeThreads_GetQueueCount (ThreadPoolHandle handle) { - auto &threadPool = ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; + auto &threadPool = ThreadPool_BucketContainer.buckets[handle.bucketIndex][handle.itemIndex]; return threadPool.jobQueue->Count(); } void PkeThreads_Pause(ThreadPoolHandle handle) { assert(handle != ThreadPoolHandle_MAX); - auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex][handle.itemIndex]; PkeThreads_Pause_Inner(*tp); } void PkeThreads_Resume(ThreadPoolHandle handle) { assert(handle != ThreadPoolHandle_MAX); - auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex][handle.itemIndex]; PkeThreads_Resume_Inner(*tp); } void PkeThreads_Shutdown(ThreadPoolHandle handle) { assert(handle != ThreadPoolHandle_MAX); - auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex][handle.itemIndex]; PkeThreads_Shutdown_Inner(*tp); PkeThreads_JoinAll_Inner(*tp); @@ -201,7 +199,7 @@ void PkeThreads_Shutdown(ThreadPoolHandle handle) { void PkeThreads_Teardown(ThreadPoolHandle handle) { assert(handle != ThreadPoolHandle_MAX); - auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex][handle.itemIndex]; PkeThreads_Shutdown_Inner(*tp); PkeThreads_JoinAll_Inner(*tp); -- cgit v1.2.3