1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
template <typename T>
struct MPMCQueueNode {
std::atomic<isize> idx;
T data;
};
template <typename T>
struct MPMCQueueNodeNonAtomic {
isize idx;
T data;
};
typedef char CacheLinePad[64];
// Multiple Producer Multiple Consumer Queue
template <typename T>
struct MPMCQueue {
CacheLinePad pad0;
isize mask;
Array<MPMCQueueNode<T>> buffer;
BlockingMutex mutex;
std::atomic<isize> count;
CacheLinePad pad1;
std::atomic<isize> head_idx;
CacheLinePad pad2;
std::atomic<isize> tail_idx;
CacheLinePad pad3;
};
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));
mutex_init(&q->mutex);
q->mask = size-1;
array_init(&q->buffer, a, size);
// 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;
}
}
template <typename T>
void mpmc_destroy(MPMCQueue<T> *q) {
mutex_destroy(&q->mutex);
gb_free(q->buffer.allocator, q->buffer.data);
}
template <typename T>
isize mpmc_enqueue(MPMCQueue<T> *q, T const &data) {
if (q->mask == 0) {
return -1;
}
isize head_idx = q->head_idx.load(std::memory_order_relaxed);
for (;;) {
auto node = &q->buffer.data[head_idx & q->mask];
isize node_idx = node->idx.load(std::memory_order_acquire);
isize diff = node_idx - head_idx;
if (diff == 0) {
isize next_head_idx = head_idx+1;
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);
return q->count.fetch_add(1, std::memory_order_release);
}
} else if (diff < 0) {
mutex_lock(&q->mutex);
isize old_size = q->buffer.count;
isize new_size = old_size*2;
array_resize(&q->buffer, new_size);
if (q->buffer.data == nullptr) {
GB_PANIC("Unable to resize enqueue: %td -> %td", old_size, new_size);
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++) {
raw_data[i].idx = i;
}
q->mask = new_size-1;
mutex_unlock(&q->mutex);
} else {
head_idx = q->head_idx.load(std::memory_order_relaxed);
}
}
}
template <typename T>
bool mpmc_dequeue(MPMCQueue<T> *q, T *data_) {
if (q->mask == 0) {
return false;
}
isize tail_idx = q->tail_idx.load(std::memory_order_relaxed);
for (;;) {
auto node = &q->buffer.data[tail_idx & q->mask];
isize node_idx = node->idx.load(std::memory_order_acquire);
isize diff = node_idx - (tail_idx+1);
if (diff == 0) {
isize next_tail_idx = tail_idx+1;
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) {
return false;
} else {
tail_idx = q->tail_idx.load(std::memory_order_relaxed);
}
}
}
template <typename T>
struct MPSCQueueNode {
std::atomic<MPSCQueueNode<T> *> next;
T data;
};
template <typename T>
struct MPSCQueue {
gbAllocator allocator;
std::atomic<isize> count;
std::atomic<MPSCQueueNode<T> *> head;
std::atomic<MPSCQueueNode<T> *> tail;
};
template <typename T>
void mpsc_init(MPSCQueue<T> *q, gbAllocator a) {
using Node = MPSCQueueNode<T>;
q->allocator = a;
Node *front = cast(Node *)gb_alloc_align(q->allocator, gb_size_of(Node), 64);
front->next.store(nullptr, std::memory_order_relaxed);
q->head.store(front, std::memory_order_relaxed);
q->tail.store(front, std::memory_order_relaxed);
}
template <typename T>
isize mpsc_enqueue(MPSCQueue<T> *q, T const &value) {
using Node = MPSCQueueNode<T>;
Node *node = cast(Node *)gb_alloc_align(q->allocator, gb_size_of(Node), 64);
node->data = value;
node->next.store(nullptr, std::memory_order_relaxed);
auto *prev_head = q->head.exchange(node, std::memory_order_acq_rel);
prev_head->next.store(node, std::memory_order_release);
return q->count.fetch_add(1, std::memory_order_release);
}
template <typename T>
bool mpsc_dequeue(MPSCQueue<T> *q, T *value_) {
auto *tail = q->tail.load(std::memory_order_relaxed);
auto *next = tail->next.load(std::memory_order_acquire);
if (next == nullptr) {
return false;
}
if (value_) *value_ = next->data;
q->tail.store(next, std::memory_order_release);
q->count.fetch_sub(1, std::memory_order_release);
gb_free(q->allocator, tail);
return true;
}
template <typename T>
void mpsc_destroy(MPSCQueue<T> *q) {
T output = {};
while (mpsc_dequeue(q, &output)) {
// okay
}
auto *front = q->head.load(std::memory_order_relaxed);
gb_free(q->allocator, front);
}
|