summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2023-12-22 17:51:10 -0500
committerJonathan Bradley <jcb@pikum.xyz>2023-12-23 11:42:23 -0500
commita3937e7eef97cb0badcd65c390b9dd39d4cfd094 (patch)
treeca793f7630e0130211ed480c915968d743e7b506
parentfa7fc343a0e444da72938fad58d219cf52228976 (diff)
PkeThreads_Enqueue now takes a pointer to a task
-rw-r--r--editor/editor.cpp18
-rw-r--r--src/memory.cpp5
-rw-r--r--src/memory.hpp1
-rw-r--r--src/thread_pool.cpp18
-rw-r--r--src/thread_pool.hpp17
5 files changed, 40 insertions, 19 deletions
diff --git a/editor/editor.cpp b/editor/editor.cpp
index db97802..bb19b70 100644
--- a/editor/editor.cpp
+++ b/editor/editor.cpp
@@ -98,7 +98,8 @@ void PkeEditor_Tick(double delta) {
if (shouldRunCurrentScene) {
shouldRunCurrentScene = false;
subProgramRunning = true;
- PkeThreads_Enqueue(threadPoolHandle, std::packaged_task<void()>( [] {
+ auto *task = Pke_New<std::packaged_task<void()>>();
+ new (task) std::packaged_task<void()>( [] {
auto pid = fork();
if (pid == 0) {
int status = -1;
@@ -121,7 +122,8 @@ void PkeEditor_Tick(double delta) {
fprintf(stdout, "pke_runtime (parent) exited with a status of %i (0x%08X)\n", status, status);
subProgramRunning = false;
}
- }));
+ });
+ PkeThreads_Enqueue(threadPoolHandle, task);
}
if (shouldSetupEditor) {
PkeEditor_Setup();
@@ -144,7 +146,8 @@ void PkeEditor_Tick(double delta) {
}
if (shouldOpenLoadSceneDialog) {
shouldOpenLoadSceneDialog = false;
- PkeThreads_Enqueue(threadPoolHandle, std::packaged_task<void()>( [] {
+ auto *task = Pke_New<std::packaged_task<void()>>();
+ new (task) std::packaged_task<void()>( [] {
const char * patterns[1] = {"*.pstf"};
char *selectedScene = tinyfd_openFileDialog(nullptr, "cafebabe.pstf", 1, patterns, "Pke Scene Text File", 0);
if (selectedScene != nullptr) {
@@ -152,19 +155,22 @@ void PkeEditor_Tick(double delta) {
pkeSettings.rt.shouldLoadScene = true;
ActiveCamera = &NullCamera;
}
- }));
+ });
+ PkeThreads_Enqueue(threadPoolHandle, task);
}
if (shouldOpenSaveSceneDialog) {
shouldOpenSaveSceneDialog = false;
- PkeThreads_Enqueue(threadPoolHandle, std::packaged_task<void()>( [] {
+ auto *task = Pke_New<std::packaged_task<void()>>();
+ new (task) std::packaged_task<void()>( [] {
const char * patterns[1] = {"*.pstf"};
char *selectedScene = tinyfd_saveFileDialog(nullptr, pkeSettings.rt.sceneName, 1, patterns, "Pke Scene Text File");
if (selectedScene != nullptr) {
pkeSettings.rt.sceneName = selectedScene;
pkeSettings.rt.shouldSaveScene = true;
}
- }));
+ });
+ PkeThreads_Enqueue(threadPoolHandle, task);
}
if (pkeSettings.rt.shouldSaveScene && pkeSettings.rt.sceneName) {
pkeSettings.rt.shouldSaveScene = false;
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 */