From d6c54148d93a649dbf8d75f97c5c487431b0c8fd Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 12 Jan 2023 15:23:59 +0000 Subject: Minor clean up --- src/queue.cpp | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'src/queue.cpp') diff --git a/src/queue.cpp b/src/queue.cpp index 5585b772f..05076cfbd 100644 --- a/src/queue.cpp +++ b/src/queue.cpp @@ -11,37 +11,32 @@ struct MPSCNode { template struct MPSCQueue { gbAllocator allocator; - std::atomic count; + MPSCNode sentinel; std::atomic *> head; std::atomic *> tail; - MPSCNode sentinel; + std::atomic count; }; -template gb_internal void mpsc_init (MPSCQueue *q, gbAllocator const &allocator); -template gb_internal void mpsc_destroy(MPSCQueue *q); -template gb_internal isize mpsc_enqueue(MPSCQueue *q, T const &value); -template gb_internal bool mpsc_dequeue(MPSCQueue *q, T *value_); -template gb_internal MPSCNode *mpsc_tail (MPSCQueue *q); +template gb_internal void mpsc_init (MPSCQueue *q, gbAllocator const &allocator); +template gb_internal void mpsc_destroy(MPSCQueue *q); +template gb_internal isize mpsc_enqueue(MPSCQueue *q, T const &value); +template gb_internal bool mpsc_dequeue(MPSCQueue *q, T *value_); template gb_internal void mpsc_init(MPSCQueue *q, gbAllocator const &allocator) { q->allocator = allocator; - q->count.store(0, std::memory_order_relaxed); + q->sentinel.next.store(nullptr, std::memory_order_relaxed); q->head.store(&q->sentinel, std::memory_order_relaxed); q->tail.store(&q->sentinel, std::memory_order_relaxed); - - q->sentinel.next.store(nullptr, std::memory_order_relaxed); + q->count.store(0, std::memory_order_relaxed); } template gb_internal void mpsc_destroy(MPSCQueue *q) { - while (mpsc_dequeue(q, (T *)nullptr)) {} - // DO NOTHING for the time being - // free the nodes later + GB_ASSERT(q->count.load() == 0); } - template gb_internal MPSCNode *mpsc_alloc_node(MPSCQueue *q, T const &value) { auto new_node = gb_alloc_item(q->allocator, MPSCNode); @@ -51,7 +46,7 @@ gb_internal MPSCNode *mpsc_alloc_node(MPSCQueue *q, T const &value) { template gb_internal void mpsc_free_node(MPSCQueue *q, MPSCNode *node) { - // TODO(bill): reuse the free nodes + // TODO(bill): determine a good way to handle the freed nodes rather than letting them leak } template @@ -59,7 +54,7 @@ gb_internal isize mpsc_enqueue(MPSCQueue *q, MPSCNode *node) { node->next.store(nullptr, std::memory_order_relaxed); auto prev = q->head.exchange(node, std::memory_order_acq_rel); prev->next.store(node, std::memory_order_release); - isize count = 1 + q->count.fetch_add(1, std::memory_order_acq_rel); + isize count = 1 + q->count.fetch_add(1, std::memory_order_relaxed); return count; } @@ -77,8 +72,8 @@ gb_internal bool mpsc_dequeue(MPSCQueue *q, T *value_) { if (next) { q->tail.store(next, std::memory_order_relaxed); if (value_) *value_ = next->value; + q->count.fetch_sub(1, std::memory_order_relaxed); mpsc_free_node(q, tail); - q->count.fetch_sub(1, std::memory_order_acq_rel); return true; } GB_ASSERT(q->count.load(std::memory_order_acquire) == 0); -- cgit v1.2.3