diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/memory.cpp | 5 | ||||
| -rw-r--r-- | src/memory.hpp | 1 | ||||
| -rw-r--r-- | src/thread_pool.cpp | 18 | ||||
| -rw-r--r-- | src/thread_pool.hpp | 17 |
4 files changed, 28 insertions, 13 deletions
diff --git a/src/memory.cpp b/src/memory.cpp index 08df8f0..daaba8d 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -230,6 +230,11 @@ void inline Pke_CollapseBlocks(MemBucket *bkt) { } } +bool Pke_InBucket(const void *ptr, const MemBucket *bkt) { + if (ptr >= bkt->ptr && ptr < bkt->ptr + bkt->size) return true; + return false; +} + void Pke_Delete(const void *ptr, std::size_t sz, MemBucket *bkt) { assert(ptr >= bkt->ptr && ptr < bkt->ptr + bkt->size && "pointer not in memory bucket range"); bkt->allocs--; diff --git a/src/memory.hpp b/src/memory.hpp index bf0972e..c2fc3bb 100644 --- a/src/memory.hpp +++ b/src/memory.hpp @@ -28,6 +28,7 @@ void *Pke_New(std::size_t sz, std::size_t alignment); void *Pke_New(std::size_t sz, std::size_t alignment, MemBucket *bkt); void Pke_Delete(const void *ptr, std::size_t sz); void Pke_Delete(const void *ptr, std::size_t sz, MemBucket *bkt); +bool Pke_InBucket(const void *ptr, const MemBucket *bkt); void Pke_DebugPrint(); MemBucket *Pke_BeginTransientBucket(int64_t sz = DEFAULT_BUCKET_SIZE); diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index 3af2273..c3c8743 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -143,14 +143,24 @@ void PkeThreads_Reset(ThreadPoolHandle handle) { PkeThreads_Reset_Inner(*tp); } -bool PkeThreads_Enqueue(ThreadPoolHandle handle, std::packaged_task<void()> job) { +bool PkeThreads_Enqueue(ThreadPoolHandle handle, std::packaged_task<void()> *job) { assert(handle != ThreadPoolHandle_MAX); auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; + if (tp->bkt != nullptr) { + /* 2023-12-22 JCB + * Note that if this becomes an issue we can change it. + * Technically speaking, if we call the right Pke_Delete + * we don't even need to worry about passing the MemBucket + */ + assert(Pke_InBucket(job, tp->bkt) == true && "cannot enqueue packaged task from a non-matching MemBucket"); + } - auto jobPtr = Pke_New<std::packaged_task<void()>>(tp->bkt); - *jobPtr = std::move(job); + return PkeThreads_Enqueue_Inner(*tp, job); +} - return PkeThreads_Enqueue_Inner(*tp, jobPtr); +int64_t PkeThreads_GetQueueCount (ThreadPoolHandle handle) { + auto &threadPool = ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex]; + return threadPool.jobQueue->Count(); } void PkeThreads_Pause(ThreadPoolHandle handle) { diff --git a/src/thread_pool.hpp b/src/thread_pool.hpp index dc75f33..a2c48f7 100644 --- a/src/thread_pool.hpp +++ b/src/thread_pool.hpp @@ -4,8 +4,6 @@ #include "dynamic-array.hpp" #include "macros.hpp" -#include <atomic> -#include <condition_variable> #include <cstdint> #include <future> @@ -13,12 +11,13 @@ struct ThreadPoolHandle : public PkeHandle { }; constexpr ThreadPoolHandle ThreadPoolHandle_MAX = ThreadPoolHandle{}; -ThreadPoolHandle PkeThreads_Init (uint8_t threadCount, uint8_t maxQueueCount, MemBucket *bkt = nullptr); -void PkeThreads_Reset (ThreadPoolHandle handle); -bool PkeThreads_Enqueue (ThreadPoolHandle handle, std::packaged_task<void()> job); -void PkeThreads_Pause (ThreadPoolHandle handle); -void PkeThreads_Resume (ThreadPoolHandle handle); -void PkeThreads_Shutdown (ThreadPoolHandle handle); -void PkeThreads_Teardown (ThreadPoolHandle handle); +ThreadPoolHandle PkeThreads_Init (uint8_t threadCount, uint8_t maxQueueCount, MemBucket *bkt = nullptr); +void PkeThreads_Reset (ThreadPoolHandle handle); +bool PkeThreads_Enqueue (ThreadPoolHandle handle, std::packaged_task<void()> *job); +int64_t PkeThreads_GetQueueCount (ThreadPoolHandle handle); +void PkeThreads_Pause (ThreadPoolHandle handle); +void PkeThreads_Resume (ThreadPoolHandle handle); +void PkeThreads_Shutdown (ThreadPoolHandle handle); +void PkeThreads_Teardown (ThreadPoolHandle handle); #endif /* PKE_THREADING_HPP */ |
