aboutsummaryrefslogtreecommitdiff
path: root/src/queue.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-07-10 21:29:49 +0100
committergingerBill <bill@gingerbill.org>2021-07-10 21:29:49 +0100
commited8a6f872dbcd8b195940dec40a0d86d59f11eaa (patch)
treebbf4d7fc301a432583f8f2121742a83c1d4cc6af /src/queue.cpp
parent0a61d4bf2b2d6e8c8d0c92410f6dcfd2b6046f86 (diff)
Move things around for sanity checking for multithread preparation
Diffstat (limited to 'src/queue.cpp')
-rw-r--r--src/queue.cpp5
1 files changed, 4 insertions, 1 deletions
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<MPMCQueueNode<T>> buffer;
gbMutex mutex;
+ std::atomic<isize> count;
CacheLinePad pad1;
std::atomic<isize> head_idx;
@@ -42,7 +43,7 @@ void mpmc_init(MPMCQueue<T> *q, gbAllocator a, isize size) {
template <typename T>
void mpmc_destroy(MPMCQueue<T> *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<T> *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<T> *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) {