From ba250cc496b2e617823ff8111ef463b6adea27f4 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Mon, 11 Dec 2023 14:46:50 -0500 Subject: replace handles with union struct --- src/thread_pool.cpp | 51 ++++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) (limited to 'src/thread_pool.cpp') diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index 15cc4e9..48338e5 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -4,8 +4,6 @@ #include #include -TypeSafeInt_B(ThreadPoolHandle); - struct ThreadPool { bool isRunning; bool isPaused; @@ -18,11 +16,12 @@ struct ThreadPool { MemBucket *bkt = nullptr; }; +const uint32_t MAX_THREADS_PER_BUCKET = 8; struct ThreadBucket { - ThreadPool threadPools[8]; + ThreadPool threadPools[MAX_THREADS_PER_BUCKET]; }; -BucketContainer ThreadPool_BucketContainer{}; +BucketContainer ThreadPool_BucketContainer{}; void ThreadRun(ThreadPool *tp) { std::packaged_task *j = nullptr; @@ -118,11 +117,9 @@ ThreadPoolHandle PkeThreads_Init(uint8_t threadCount, uint8_t maxQueueCount, Mem } assert(threadCount > 0); bool moved; - ThreadPoolHandle_T newHandle{Buckets_NewHandle(255, ThreadPool_BucketContainer, moved)}; + ThreadPoolHandle newHandle{Buckets_NewHandle(255, ThreadPool_BucketContainer, moved)}; - auto b = Buckets_GetBucketIndex(newHandle); - auto i = Buckets_GetItemIndex(newHandle); - auto *tp = &ThreadPool_BucketContainer.buckets[b].threadPools[i]; + auto *tp = &ThreadPool_BucketContainer.buckets[newHandle.bucketIndex].threadPools[newHandle.itemIndex]; tp->bkt = bkt; tp->isRunning = true; @@ -137,22 +134,18 @@ ThreadPoolHandle PkeThreads_Init(uint8_t threadCount, uint8_t maxQueueCount, Mem (*tp->threads)[l] = std::thread(std::bind(ThreadRun, tp)); } - return ThreadPoolHandle{newHandle}; + return newHandle; } void PkeThreads_Reset(ThreadPoolHandle handle) { - ThreadPoolHandle_T handle_T{static_cast(handle)}; - auto b = Buckets_GetBucketIndex(handle_T); - auto i = Buckets_GetItemIndex(handle_T); - auto *tp = &ThreadPool_BucketContainer.buckets[b].threadPools[i]; + assert(handle != ThreadPoolHandle_MAX); + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; PkeThreads_Reset_Inner(*tp); } bool PkeThreads_Enqueue(ThreadPoolHandle handle, std::packaged_task job) { - ThreadPoolHandle_T handle_T{static_cast(handle)}; - auto b = Buckets_GetBucketIndex(handle_T); - auto i = Buckets_GetItemIndex(handle_T); - auto *tp = &ThreadPool_BucketContainer.buckets[b].threadPools[i]; + assert(handle != ThreadPoolHandle_MAX); + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; auto jobPtr = Pke_New>(tp->bkt); *jobPtr = std::move(job); @@ -161,37 +154,29 @@ bool PkeThreads_Enqueue(ThreadPoolHandle handle, std::packaged_task job) } void PkeThreads_Pause(ThreadPoolHandle handle) { - ThreadPoolHandle_T handle_T{static_cast(handle)}; - auto b = Buckets_GetBucketIndex(handle_T); - auto i = Buckets_GetItemIndex(handle_T); - auto *tp = &ThreadPool_BucketContainer.buckets[b].threadPools[i]; + assert(handle != ThreadPoolHandle_MAX); + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; PkeThreads_Pause_Inner(*tp); } void PkeThreads_Resume(ThreadPoolHandle handle) { - ThreadPoolHandle_T handle_T{static_cast(handle)}; - auto b = Buckets_GetBucketIndex(handle_T); - auto i = Buckets_GetItemIndex(handle_T); - auto *tp = &ThreadPool_BucketContainer.buckets[b].threadPools[i]; + assert(handle != ThreadPoolHandle_MAX); + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; PkeThreads_Resume_Inner(*tp); } void PkeThreads_Shutdown(ThreadPoolHandle handle) { - ThreadPoolHandle_T handle_T{static_cast(handle)}; - auto b = Buckets_GetBucketIndex(handle_T); - auto i = Buckets_GetItemIndex(handle_T); - auto *tp = &ThreadPool_BucketContainer.buckets[b].threadPools[i]; + assert(handle != ThreadPoolHandle_MAX); + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; PkeThreads_Shutdown_Inner(*tp); } void PkeThreads_Teardown(ThreadPoolHandle handle) { - ThreadPoolHandle_T handle_T{static_cast(handle)}; - auto b = Buckets_GetBucketIndex(handle_T); - auto i = Buckets_GetItemIndex(handle_T); - auto *tp = &ThreadPool_BucketContainer.buckets[b].threadPools[i]; + assert(handle != ThreadPoolHandle_MAX); + auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; PkeThreads_Shutdown_Inner(*tp); PkeThreads_Reset_Inner(*tp); -- cgit v1.2.3