summaryrefslogtreecommitdiff
path: root/src/thread_pool.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/thread_pool.cpp')
-rw-r--r--src/thread_pool.cpp30
1 files changed, 14 insertions, 16 deletions
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 <functional>
#include <future>
@@ -17,11 +19,8 @@ struct ThreadPool {
};
const PkeHandleItemIndex_T MAX_THREADS_PER_BUCKET = 8;
-struct ThreadBucket {
- ThreadPool threadPools[MAX_THREADS_PER_BUCKET];
-};
-BucketContainer<ThreadBucket, ThreadPoolHandle> ThreadPool_BucketContainer{};
+BucketContainer<ThreadPool, ThreadPoolHandle> ThreadPool_BucketContainer{};
void ThreadRun(ThreadPool *tp) {
std::packaged_task<void()> *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<void()> *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<void()> *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);