-
Notifications
You must be signed in to change notification settings - Fork 16
/
p64_ringbuf.c
578 lines (538 loc) · 14.6 KB
/
p64_ringbuf.c
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//Copyright (c) 2017-2018, ARM Limited. All rights reserved.
//
//SPDX-License-Identifier: BSD-3-Clause
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "p64_ringbuf.h"
#include "build_config.h"
#include "os_abstraction.h"
#include "arch.h"
#include "common.h"
#include "err_hnd.h"
#define SUPPORTED_FLAGS (P64_RINGBUF_F_SPENQ | P64_RINGBUF_F_MPENQ | \
P64_RINGBUF_F_SCDEQ | P64_RINGBUF_F_MCDEQ | \
P64_RINGBUF_F_NBENQ | P64_RINGBUF_F_NBDEQ | \
P64_RINGBUF_F_LFDEQ)
//0 means Single producer/consumer
#define FLAG_BLK 0x0001
#define FLAG_LOCKFREE 0x0002
#define FLAG_NONBLK 0x0004
typedef uint32_t ringidx_t;
#define MAXELEMS 0xFFFFFFFF
#define PENDMAX 32
struct idxpair
{
_Alignas(2 * sizeof(ringidx_t))
ringidx_t cur;
uint32_t pend;
};
struct endpoint
{
_Alignas(4 * sizeof(ringidx_t))
struct idxpair head;//tail for consumer
ringidx_t tail;//head for consumer
ringidx_t mask;
ringidx_t capacity;
uint32_t flags;
};
struct p64_ringbuf
{
_Alignas(CACHE_LINE)
struct endpoint prod;
#ifdef USE_SPLIT_PRODCONS
_Alignas(CACHE_LINE)
#endif
struct endpoint cons;//head & tail are swapped for consumer metadata
_Alignas(CACHE_LINE)
void *ring[];
};
p64_ringbuf_t *
p64_ringbuf_alloc(uint32_t nelems, uint32_t flags, size_t esize)
{
if (nelems == 0 || nelems > MAXELEMS)
{
report_error("ringbuf", "invalid number of elements", nelems);
return NULL;
}
//Can't specify both single-producer and MP non-blocking enqueue
uint32_t invalid_combo0 = P64_RINGBUF_F_SPENQ | P64_RINGBUF_F_NBENQ;
//Can't specify both single-consumer and MP non-blocking dequeue
uint32_t invalid_combo1 = P64_RINGBUF_F_SCDEQ | P64_RINGBUF_F_NBDEQ;
//Can't specify both single-consumer and MP lock-free dequeue
uint32_t invalid_combo2 = P64_RINGBUF_F_SCDEQ | P64_RINGBUF_F_LFDEQ;
//Can't specify both MP non-blocking dequeue and MP lock-free dequeue
uint32_t invalid_combo3 = P64_RINGBUF_F_NBDEQ | P64_RINGBUF_F_LFDEQ;
if ((flags & ~SUPPORTED_FLAGS) != 0 ||
(flags & invalid_combo0) == invalid_combo0 ||
(flags & invalid_combo1) == invalid_combo1 ||
(flags & invalid_combo2) == invalid_combo2 ||
(flags & invalid_combo3) == invalid_combo3)
{
report_error("ringbuf", "invalid flags", flags);
return NULL;
}
uint64_t ringsz = ROUNDUP_POW2(nelems);
size_t nbytes = sizeof(p64_ringbuf_t) + ringsz * esize;
p64_ringbuf_t *rb = p64_malloc(nbytes, CACHE_LINE);
if (rb != NULL)
{
rb->prod.head.cur = 0;
rb->prod.head.pend = 0;
rb->prod.tail = 0;
rb->prod.mask = ringsz - 1;
rb->prod.capacity = nelems;
rb->prod.flags = (flags & P64_RINGBUF_F_SPENQ) ? 0 ://SPENQ
(flags & P64_RINGBUF_F_NBENQ) ? FLAG_NONBLK ://NBENQ
FLAG_BLK;//MPENQ
rb->cons.head.cur = 0;
rb->cons.head.pend = 0;
rb->cons.tail = 0;
rb->cons.mask = ringsz - 1;
rb->cons.capacity = 0;
rb->cons.flags = (flags & P64_RINGBUF_F_SCDEQ) ? 0 ://SCDEQ
(flags & P64_RINGBUF_F_NBDEQ) ? FLAG_NONBLK ://NBDEQ
FLAG_BLK;//MCDEQ
rb->cons.flags |= (flags & P64_RINGBUF_F_LFDEQ) ? FLAG_LOCKFREE : 0;
return rb;
}
return NULL;
}
void *
p64_ringbuf_alloc_(uint32_t nelems, uint32_t flags, size_t esize)
{
p64_ringbuf_t *rb = p64_ringbuf_alloc(nelems, flags, esize);
if (rb != NULL)
{
return &rb->ring;
}
return NULL;
}
void
p64_ringbuf_free(p64_ringbuf_t *rb)
{
if (rb != NULL)
{
if (rb->prod.head.cur != rb->cons.head/*tail*/.cur)
{
report_error("ringbuf", "ring buffer not empty", rb);
return;
}
p64_mfree(rb);
}
}
#define container_of(pointer, type, member) \
((type *)(void *)(((char *)pointer) - offsetof(type, member)))
void
p64_ringbuf_free_(void *ptr)
{
if (ptr != NULL)
{
p64_ringbuf_free(container_of(ptr, p64_ringbuf_t, ring));
}
}
//MT-unsafe single producer/consumer code
static inline p64_ringbuf_result_t
acquire_slots(const ringidx_t *headp,
ringidx_t *tailp,
ringidx_t mask,
int n,
ringidx_t capacity)
{
ringidx_t tail = __atomic_load_n(tailp, __ATOMIC_RELAXED);
ringidx_t head = __atomic_load_n(headp, __ATOMIC_ACQUIRE);
int actual = MIN(n, (int)(capacity + head - tail));
if (UNLIKELY(actual <= 0))
{
return (p64_ringbuf_result_t){ .index = 0, .actual = 0, .mask = 0 };
}
return (p64_ringbuf_result_t){ .index = tail, .actual = actual, .mask = mask };
}
//MT-safe multi producer/consumer code
static inline p64_ringbuf_result_t
acquire_slots_mtsafe(struct endpoint *rb,
int n,
ringidx_t capacity)
{
ringidx_t tail;
int actual;
tail = __atomic_load_n(&rb->tail, __ATOMIC_RELAXED);
ringidx_t head = __atomic_load_n(&rb->head.cur, __ATOMIC_ACQUIRE);
do
{
actual = MIN(n, (int)(capacity + head - tail));
if (UNLIKELY(actual <= 0))
{
return (p64_ringbuf_result_t){ .index = 0, .actual = 0, .mask = 0 };
}
}
while (!__atomic_compare_exchange_n(&rb->tail,
&tail,//Updated on failure
tail + actual,
/*weak=*/true,
__ATOMIC_RELAXED,
__ATOMIC_RELAXED));
return (p64_ringbuf_result_t){ .index = tail,
.actual = actual,
.mask = rb->mask };
}
#define LOWER(x) (uint32_t)(x)
#define TOLOWER(x) (uint32_t)(x)
#define UPPER(x) (uint32_t)((x) >> 32)
#define TOUPPER(x) ((uint64_t)(x) << 32)
static inline void
release_slots(struct idxpair *loc,
ringidx_t idx,
uint32_t n,
bool loads_only,
uint32_t flags)
{
if (flags & FLAG_BLK)
{
//Wait for our turn to signal consumers (producers)
wait_until_equal(&loc->cur, idx, __ATOMIC_RELAXED);
}
if (!(flags & FLAG_NONBLK))
{
//Release elements to consumers (producers)
//Also enable other producers (consumers) to proceed
if (loads_only)
{
smp_fence(LoadStore);//Order loads only
__atomic_store_n(&loc->cur, idx + n, __ATOMIC_RELAXED);
}
else
{
__atomic_store_n(&loc->cur, idx + n, __ATOMIC_RELEASE);
}
return;
}
//Else non-blocking (FLAG_NONBLK)
uint64_t old, neu;
restart:
//Attempt in-order release
old = TOLOWER(idx);
neu = TOLOWER(idx + n);
if (UNLIKELY(__atomic_compare_exchange_n((uint64_t *)loc,
&old,
neu,
0,
__ATOMIC_RELEASE,
__ATOMIC_RELAXED)))
{
return;
}
//Else failure, 'old' updated with current value
uint32_t delta = LOWER(neu) - LOWER(old);
//Check if released slots fit within pending window
if (UNLIKELY(delta >= PENDMAX))
{
//End index outside of pending mask
//Cannot perform update, must block:-(
goto restart;
}
do
{
if (UNLIKELY(n >= PENDMAX && idx == LOWER(old)))
{
//We can only do large releases in-order
neu = TOLOWER(idx + n);
}
else
{
uint32_t offset = idx - LOWER(old);//Offset into pending mask
uint32_t mask = (1UL << n) - 1;//Mask of slots to release
uint32_t newmask = UPPER(old) | (mask << offset);//Update pending mask
uint64_t longmask = newmask;
//Find number of in-order slots (count trailing ones of newmask)
// ~longmask can never be zero!
uint32_t inorder = __builtin_ctzl(~longmask); //0..PENDMAX-1
longmask >>= inorder;
neu = TOLOWER(old + inorder) | TOUPPER(longmask);
}
}
while (!__atomic_compare_exchange_n((uint64_t *)loc,
&old,//Updated on failure
neu,
/*weak=*/0,
__ATOMIC_RELEASE,
__ATOMIC_RELAXED));
}
inline p64_ringbuf_result_t
p64_ringbuf_acquire_(void *ptr,
uint32_t num,
bool enqueue)
{
p64_ringbuf_t *rb = container_of(ptr, p64_ringbuf_t, ring);
p64_ringbuf_result_t r;
if (enqueue)
{
uint32_t mask = rb->prod.mask;
uint32_t prod_flags = rb->prod.flags;
if (!(prod_flags & (FLAG_BLK | FLAG_NONBLK)))
{
//MT-unsafe single producer code
//Consumer metadata is swapped: cons.tail<->cons.head
r = acquire_slots(&rb->prod.head.cur,
&rb->cons.head/*tail*/.cur,
mask, num, rb->prod.capacity);
}
else
{
//MT-safe multi producer code
r = acquire_slots_mtsafe(&rb->prod, num, rb->prod.capacity);
}
}
else //dequeue
{
uint32_t mask = rb->cons.mask;
uint32_t cons_flags = rb->cons.flags;
if (rb->cons.flags & FLAG_LOCKFREE)
{
//Use prod.head instead of cons.head (which is not used at all)
int actual;
//Speculative acquisition of slots
ringidx_t head = __atomic_load_n(&rb->prod.head.cur,
__ATOMIC_RELAXED);
//Consumer metadata is swapped: cons.tail<->cons.head
ringidx_t tail = __atomic_load_n(&rb->cons.head/*tail*/.cur,
__ATOMIC_ACQUIRE);
actual = MIN((int)num, (int)(tail - head));
if (UNLIKELY(actual <= 0))
{
return (p64_ringbuf_result_t){ .index = 0,
.actual = 0,
.mask = 0 };
}
return (p64_ringbuf_result_t){ .index = head,
.actual = actual,
.mask = mask };
}
if (!(cons_flags & (FLAG_BLK | FLAG_NONBLK)))
{
//MT-unsafe single consumer code
//Consumer metadata is swapped: cons.tail<->cons.head
r = acquire_slots(&rb->cons.head/*tail*/.cur,
&rb->prod.head.cur,
mask, num, 0);
}
else
{
//MT-safe multi consumer code
r = acquire_slots_mtsafe(&rb->cons, num, 0);
}
}
return r;
}
inline bool
p64_ringbuf_release_(void *ptr,
p64_ringbuf_result_t r,
bool enqueue)
{
p64_ringbuf_t *rb = container_of(ptr, p64_ringbuf_t, ring);
if (enqueue)
{
//Consumer metadata is swapped: cons.tail<->cons.head
release_slots(&rb->cons.head/*tail*/, r.index, r.actual,
/*loads_only=*/false, rb->prod.flags);
return true;//Success
}
else //dequeue
{
if (rb->cons.flags & FLAG_LOCKFREE)
{
bool success = __atomic_compare_exchange_n(&rb->prod.head.cur,
&r.index,
r.index + r.actual,
/*weak=*/true,
__ATOMIC_RELEASE,
__ATOMIC_RELAXED);
return success;
}
release_slots(&rb->prod.head, r.index, r.actual,
/*loads_only=*/true, rb->cons.flags);
return true;//Success
}
}
UNROLL_LOOPS
static inline void
copy_voidptr(void **restrict dst, void *const *restrict src, uint32_t num)
{
for (uint32_t i = 0; i < num; i++)
{
dst[i] = src[i];
}
}
static inline void
write_slots(void **restrict rbring,
void *const *restrict ev,
const p64_ringbuf_result_t r)
{
void **restrict ring0 = &rbring[r.index & r.mask];
if (LIKELY(r.actual <= 1))
{
ring0[0] = ev[0];
return;
}
uint32_t seg0 = r.mask + 1 - (r.index & r.mask);
if (LIKELY(r.actual <= seg0))
{
/* One contiguous range */
assert((r.index & r.mask) + seg0 <= r.mask + 1);
copy_voidptr(ring0, ev, r.actual);
}
else
{
/* Range wraps around end of ring => two subranges */
assert(seg0 < r.actual);
copy_voidptr(ring0, ev, seg0);
copy_voidptr(rbring, ev + seg0, r.actual - seg0);
}
}
//Enqueue elements at tail
uint32_t
p64_ringbuf_enqueue(p64_ringbuf_t *rb,
void *const *restrict ev,
uint32_t num)
{
PREFETCH_FOR_WRITE(&rb->prod);
//Step 1: acquire slots
uint32_t mask = rb->prod.mask;
uint32_t prod_flags = rb->prod.flags;
p64_ringbuf_result_t r;
if (!(prod_flags & (FLAG_BLK | FLAG_NONBLK)))//SPENQ
{
//MT-unsafe single producer code
//Consumer metadata is swapped: cons.tail<->cons.head
r = acquire_slots(&rb->prod.head.cur,
&rb->cons.head/*tail*/.cur,
mask, num, rb->prod.capacity);
}
else//MPENQ or NBENQ
{
//MT-safe multi producer code
r = acquire_slots_mtsafe(&rb->prod, num, rb->prod.capacity);
}
if (UNLIKELY(r.actual == 0))
{
return 0;
}
//Step 2: write slots
if (prod_flags & FLAG_NONBLK)//NBENQ
{
for (uint32_t i = 1; i < r.actual; i++)
{
__atomic_store_n(&rb->ring[(r.index + i) & mask],
ev[i], __ATOMIC_RELAXED);
}
__atomic_store_n(&rb->ring[(r.index + 0) & mask],
ev[0], __ATOMIC_RELEASE);
}
else//SPENQ or MPENQ
{
write_slots(rb->ring, ev, r);
}
//Step 3: release slots to consumer
//Consumer metadata is swapped: cons.tail<->cons.head
release_slots(&rb->cons.head/*tail*/, r.index, r.actual,
/*loads_only=*/false, prod_flags);
return r.actual;
}
static inline void
read_slots(void *const *restrict rbring,
void **restrict ev,
const p64_ringbuf_result_t r)
{
void *const *restrict ring0 = &rbring[r.index & r.mask];
if (LIKELY(r.actual <= 1))
{
ev[0] = ring0[0];
return;
}
uint32_t seg0 = r.mask + 1 - (r.index & r.mask);
if (LIKELY(r.actual <= seg0))
{
/* One contiguous range */
assert((r.index & r.mask) + seg0 <= r.mask + 1);
copy_voidptr(ev, ring0, r.actual);
}
else
{
/* Range wraps around end of ring => two subranges */
assert(seg0 < r.actual);
copy_voidptr(ev, ring0, seg0);
copy_voidptr(ev + seg0, rbring, r.actual - seg0);
}
}
//Dequeue elements from head
UNROLL_LOOPS
uint32_t
p64_ringbuf_dequeue(p64_ringbuf_t *rb,
void **restrict ev,
uint32_t num,
uint32_t *index)
{
uint32_t mask = rb->cons.mask;
uint32_t cons_flags = rb->cons.flags;
if (cons_flags & FLAG_LOCKFREE)
{
//Use prod.head instead of cons.head (which is not used at all)
int actual;
//Step 1: speculative acquisition of slots
PREFETCH_FOR_WRITE(&rb->prod.head);
ringidx_t head = __atomic_load_n(&rb->prod.head.cur, __ATOMIC_RELAXED);
//Consumer metadata is swapped: cons.tail<->cons.head
ringidx_t tail = __atomic_load_n(&rb->cons.head/*tail*/.cur,
__ATOMIC_ACQUIRE);
do
{
actual = MIN((int)num, (int)(tail - head));
if (UNLIKELY(actual <= 0))
{
return 0;
}
//Step 2: read slots in advance (fortunately non-destructive)
p64_ringbuf_result_t r = { .index = head,
.actual = actual,
.mask = mask };
read_slots(rb->ring, ev, r);
//Step 3: commit acquisition, release slots to producer
}
while (!__atomic_compare_exchange_n(&rb->prod.head.cur,
&head,//Updated on failure
head + actual,
/*weak=*/true,
__ATOMIC_RELEASE,
__ATOMIC_RELAXED));
*index = head;
return actual;
}
//Step 1: acquire slots
p64_ringbuf_result_t r;
if (!(cons_flags & (FLAG_BLK | FLAG_NONBLK)))//SCDEQ
{
//MT-unsafe single consumer code
//Consumer metadata is swapped: cons.tail<->cons.head
r = acquire_slots(&rb->cons.head/*tail*/.cur,
&rb->prod.head.cur,
mask, num, 0);
}
else//MCDEQ or NBDEQ
{
//MT-safe multi consumer code
r = acquire_slots_mtsafe(&rb->cons, num, 0);
}
if (UNLIKELY(r.actual == 0))
{
return 0;
}
//Step 2: read slots
read_slots(rb->ring, ev, r);
//Step 3: release slots to producer
release_slots(&rb->prod.head, r.index, r.actual,
/*loads_only=*/true, cons_flags);
*index = r.index;
return r.actual;
}