diff options
| author | gingerBill <bill@gingerbill.org> | 2021-07-14 00:34:34 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2021-07-14 00:34:34 +0100 |
| commit | bd8e2f82bed231493718c811ea43e7aebed1fe8d (patch) | |
| tree | 544e78f168cb680ef9fb8cd804968789d6bf3884 /src/queue.cpp | |
| parent | 69027b6840ae3e46291ff65353eb164ccb2f013e (diff) | |
Replace non-recursive mutexes with `BlockingMutex`; Minor improves to initialization improves
Diffstat (limited to 'src/queue.cpp')
| -rw-r--r-- | src/queue.cpp | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/src/queue.cpp b/src/queue.cpp index 33ba5af28..da3cef687 100644 --- a/src/queue.cpp +++ b/src/queue.cpp @@ -1,7 +1,13 @@ template <typename T> struct MPMCQueueNode { - T data; std::atomic<isize> idx; + T data; +}; + +template <typename T> +struct MPMCQueueNodeNonAtomic { + isize idx; + T data; }; typedef char CacheLinePad[64]; @@ -27,14 +33,25 @@ struct MPMCQueue { template <typename T> void mpmc_init(MPMCQueue<T> *q, gbAllocator a, isize size) { + size = gb_max(size, 8); size = next_pow2_isize(size); GB_ASSERT(gb_is_power_of_two(size)); gb_mutex_init(&q->mutex); q->mask = size-1; array_init(&q->buffer, a, size); - for (isize i = 0; i < size; i++) { - q->buffer[i].idx.store(i, std::memory_order_relaxed); + + // NOTE(bill): pretend it's not atomic for performance + auto *raw_data = cast(MPMCQueueNodeNonAtomic<T> *)q->buffer.data; + for (isize i = 0; i < size; i += 8) { + raw_data[i+0].idx = i+0; + raw_data[i+1].idx = i+1; + raw_data[i+2].idx = i+2; + raw_data[i+3].idx = i+3; + raw_data[i+4].idx = i+4; + raw_data[i+5].idx = i+5; + raw_data[i+6].idx = i+6; + raw_data[i+7].idx = i+7; } } @@ -71,8 +88,10 @@ isize mpmc_enqueue(MPMCQueue<T> *q, T const &data) { gb_mutex_unlock(&q->mutex); return -1; } + // NOTE(bill): pretend it's not atomic for performance + auto *raw_data = cast(MPMCQueueNodeNonAtomic<T> *)q->buffer.data; for (isize i = old_size; i < new_size; i++) { - q->buffer.data[i].idx.store(i, std::memory_order_relaxed); + raw_data[i].idx = i; } q->mask = new_size-1; gb_mutex_unlock(&q->mutex); |