aboutsummaryrefslogtreecommitdiff
path: root/src/threading.cpp
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2024-07-14 14:39:49 +0100
committerGitHub <noreply@github.com>2024-07-14 14:39:49 +0100
commiteff46c2e5f325fc5b8422a9bc1d59436db13b471 (patch)
treed9cbc51ec77e3f2d94acc00709f03ea3a7117fa5 /src/threading.cpp
parent8319917898013e00423f9ac3dad5d2f33bee5fbc (diff)
parent64feb7599e8ec01c2ec7c8d709df1cc70651c06b (diff)
Merge pull request #3919 from colrdavidson/growing_workpool
move to a growing workstealing queue
Diffstat (limited to 'src/threading.cpp')
-rw-r--r--src/threading.cpp39
1 files changed, 29 insertions, 10 deletions
diff --git a/src/threading.cpp b/src/threading.cpp
index 717dcb874..ac79efb05 100644
--- a/src/threading.cpp
+++ b/src/threading.cpp
@@ -46,6 +46,18 @@ typedef struct WorkerTask {
void *data;
} WorkerTask;
+typedef struct TaskRingBuffer {
+ std::atomic<isize> size;
+ std::atomic<WorkerTask *> buffer;
+} TaskRingBuffer;
+
+typedef struct TaskQueue {
+ std::atomic<isize> top;
+ std::atomic<isize> bottom;
+
+ std::atomic<TaskRingBuffer *> ring;
+} TaskQueue;
+
struct Thread {
#if defined(GB_SYSTEM_WINDOWS)
void *win32_handle;
@@ -54,12 +66,9 @@ struct Thread {
#endif
isize idx;
-
- WorkerTask *queue;
- size_t capacity;
- std::atomic<uint64_t> head_and_tail;
-
isize stack_size;
+
+ struct TaskQueue queue;
struct ThreadPool *pool;
};
@@ -551,6 +560,18 @@ gb_internal void *internal_thread_proc(void *arg) {
}
#endif
+TaskRingBuffer *taskring_init(isize size) {
+ TaskRingBuffer *ring = (TaskRingBuffer *)gb_alloc(heap_allocator(), sizeof(TaskRingBuffer));
+ ring->size = size;
+ ring->buffer = (WorkerTask *)gb_alloc_array(heap_allocator(), WorkerTask, ring->size);
+ return ring;
+}
+
+void thread_queue_destroy(TaskQueue *q) {
+ gb_free(heap_allocator(), (*q->ring).buffer);
+ gb_free(heap_allocator(), q->ring);
+}
+
gb_internal void thread_init(ThreadPool *pool, Thread *t, isize idx) {
gb_zero_item(t);
#if defined(GB_SYSTEM_WINDOWS)
@@ -559,14 +580,12 @@ gb_internal void thread_init(ThreadPool *pool, Thread *t, isize idx) {
t->posix_handle = 0;
#endif
- t->capacity = 1 << 14; // must be a power of 2
- t->queue = gb_alloc_array(heap_allocator(), WorkerTask, t->capacity);
- t->head_and_tail = 0;
+ // Size must be a power of 2
+ t->queue.ring = taskring_init(1 << 14);
t->pool = pool;
t->idx = idx;
}
-
gb_internal void thread_init_and_start(ThreadPool *pool, Thread *t, isize idx) {
thread_init(pool, t, idx);
isize stack_size = 0;
@@ -598,7 +617,7 @@ gb_internal void thread_join_and_destroy(Thread *t) {
t->posix_handle = 0;
#endif
- gb_free(heap_allocator(), t->queue);
+ thread_queue_destroy(&t->queue);
}
gb_internal void thread_set_name(Thread *t, char const *name) {