summaryrefslogtreecommitdiff
path: root/src/thread_pool.cpp
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2023-12-11 14:46:50 -0500
committerJonathan Bradley <jcb@pikum.xyz>2023-12-11 18:56:57 -0500
commitba250cc496b2e617823ff8111ef463b6adea27f4 (patch)
treedc926851da01b970aca827d6c6ca84b87a7432fa /src/thread_pool.cpp
parent8047197b62894cb1f7bb6a6871870e4b91fde992 (diff)
replace handles with union struct
Diffstat (limited to 'src/thread_pool.cpp')
-rw-r--r--src/thread_pool.cpp51
1 files changed, 18 insertions, 33 deletions
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 <functional>
#include <future>
-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<ThreadBucket, ThreadPoolHandle_T> ThreadPool_BucketContainer{};
+BucketContainer<ThreadBucket, ThreadPoolHandle> ThreadPool_BucketContainer{};
void ThreadRun(ThreadPool *tp) {
std::packaged_task<void()> *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<ThreadPoolHandle_T>(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<void()> job) {
- ThreadPoolHandle_T handle_T{static_cast<ThreadPoolHandle_T>(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<std::packaged_task<void()>>(tp->bkt);
*jobPtr = std::move(job);
@@ -161,37 +154,29 @@ bool PkeThreads_Enqueue(ThreadPoolHandle handle, std::packaged_task<void()> job)
}
void PkeThreads_Pause(ThreadPoolHandle handle) {
- ThreadPoolHandle_T handle_T{static_cast<ThreadPoolHandle_T>(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<ThreadPoolHandle_T>(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<ThreadPoolHandle_T>(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<ThreadPoolHandle_T>(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);