From ed8a6f872dbcd8b195940dec40a0d86d59f11eaa Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 10 Jul 2021 21:29:49 +0100 Subject: Move things around for sanity checking for multithread preparation --- src/queue.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/queue.cpp') diff --git a/src/queue.cpp b/src/queue.cpp index 7087af03e..048f8fcb9 100644 --- a/src/queue.cpp +++ b/src/queue.cpp @@ -15,6 +15,7 @@ struct MPMCQueue { isize mask; Array> buffer; gbMutex mutex; + std::atomic count; CacheLinePad pad1; std::atomic head_idx; @@ -42,7 +43,7 @@ void mpmc_init(MPMCQueue *q, gbAllocator a, isize size) { template void mpmc_destroy(MPMCQueue *q) { gb_mutex_destroy(&q->mutex); - gb_array_free(&q->buffer); + gb_free(q->buffer.allocator, q->buffer.data); } @@ -60,6 +61,7 @@ bool mpmc_enqueue(MPMCQueue *q, T const &data) { if (q->head_idx.compare_exchange_weak(head_idx, next_head_idx)) { node->data = data; node->idx.store(next_head_idx, std::memory_order_release); + q->count.fetch_add(1, std::memory_order_release); return true; } } else if (diff < 0) { @@ -98,6 +100,7 @@ bool mpmc_dequeue(MPMCQueue *q, T *data_) { if (q->tail_idx.compare_exchange_weak(tail_idx, next_tail_idx)) { if (data_) *data_ = node->data; node->idx.store(tail_idx + q->mask + 1, std::memory_order_release); + q->count.fetch_sub(1, std::memory_order_release); return true; } } else if (diff < 0) { -- cgit v1.2.3