2 * Copyright 2009-2015 Samy Al Bahra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <ck_stdbool.h>
34 #include <ck_string.h>
37 * Concurrent ring buffer.
42 char pad[CK_MD_CACHELINE - sizeof(unsigned int)];
45 char _pad[CK_MD_CACHELINE - sizeof(unsigned int) * 2];
49 typedef struct ck_ring ck_ring_t;
51 struct ck_ring_buffer {
54 typedef struct ck_ring_buffer ck_ring_buffer_t;
56 CK_CC_INLINE static unsigned int
57 ck_ring_size(const struct ck_ring *ring)
61 c = ck_pr_load_uint(&ring->c_head);
62 p = ck_pr_load_uint(&ring->p_tail);
63 return (p - c) & ring->mask;
66 CK_CC_INLINE static unsigned int
67 ck_ring_capacity(const struct ck_ring *ring)
72 CK_CC_INLINE static void
73 ck_ring_init(struct ck_ring *ring, unsigned int size)
77 ring->mask = size - 1;
85 * The _ck_ring_* namespace is internal only and must not used externally.
87 CK_CC_FORCE_INLINE static bool
88 _ck_ring_enqueue_sp(struct ck_ring *ring,
89 void *CK_CC_RESTRICT buffer,
90 const void *CK_CC_RESTRICT entry,
94 const unsigned int mask = ring->mask;
95 unsigned int consumer, producer, delta;
97 consumer = ck_pr_load_uint(&ring->c_head);
98 producer = ring->p_tail;
101 *size = (producer - consumer) & mask;
103 if (CK_CC_UNLIKELY((delta & mask) == (consumer & mask)))
106 buffer = (char *)buffer + ts * (producer & mask);
107 memcpy(buffer, entry, ts);
110 * Make sure to update slot value before indicating
111 * that the slot is available for consumption.
114 ck_pr_store_uint(&ring->p_tail, delta);
118 CK_CC_FORCE_INLINE static bool
119 _ck_ring_enqueue_sp_size(struct ck_ring *ring,
120 void *CK_CC_RESTRICT buffer,
121 const void *CK_CC_RESTRICT entry,
128 r = _ck_ring_enqueue_sp(ring, buffer, entry, ts, &sz);
133 CK_CC_FORCE_INLINE static bool
134 _ck_ring_dequeue_sc(struct ck_ring *ring,
135 const void *CK_CC_RESTRICT buffer,
136 void *CK_CC_RESTRICT target,
139 const unsigned int mask = ring->mask;
140 unsigned int consumer, producer;
142 consumer = ring->c_head;
143 producer = ck_pr_load_uint(&ring->p_tail);
145 if (CK_CC_UNLIKELY(consumer == producer))
149 * Make sure to serialize with respect to our snapshot
150 * of the producer counter.
154 buffer = (const char *)buffer + size * (consumer & mask);
155 memcpy(target, buffer, size);
158 * Make sure copy is completed with respect to consumer
162 ck_pr_store_uint(&ring->c_head, consumer + 1);
166 CK_CC_FORCE_INLINE static bool
167 _ck_ring_enqueue_mp(struct ck_ring *ring,
173 const unsigned int mask = ring->mask;
174 unsigned int producer, consumer, delta;
177 producer = ck_pr_load_uint(&ring->p_head);
181 * The snapshot of producer must be up to date with respect to
185 consumer = ck_pr_load_uint(&ring->c_head);
187 delta = producer + 1;
190 * Only try to CAS if the producer is not clearly stale (not
191 * less than consumer) and the buffer is definitely not full.
193 if (CK_CC_LIKELY((producer - consumer) < mask)) {
194 if (ck_pr_cas_uint_value(&ring->p_head,
195 producer, delta, &producer) == true) {
199 unsigned int new_producer;
202 * Slow path. Either the buffer is full or we have a
203 * stale snapshot of p_head. Execute a second read of
204 * p_read that must be ordered wrt the snapshot of
208 new_producer = ck_pr_load_uint(&ring->p_head);
211 * Only fail if we haven't made forward progress in
212 * production: the buffer must have been full when we
213 * read new_producer (or we wrapped around UINT_MAX
214 * during this iteration).
216 if (producer == new_producer) {
222 * p_head advanced during this iteration. Try again.
224 producer = new_producer;
228 buffer = (char *)buffer + ts * (producer & mask);
229 memcpy(buffer, entry, ts);
232 * Wait until all concurrent producers have completed writing
233 * their data into the ring buffer.
235 while (ck_pr_load_uint(&ring->p_tail) != producer)
239 * Ensure that copy is completed before updating shared producer
243 ck_pr_store_uint(&ring->p_tail, delta);
247 *size = (producer - consumer) & mask;
252 CK_CC_FORCE_INLINE static bool
253 _ck_ring_enqueue_mp_size(struct ck_ring *ring,
262 r = _ck_ring_enqueue_mp(ring, buffer, entry, ts, &sz);
267 CK_CC_FORCE_INLINE static bool
268 _ck_ring_trydequeue_mc(struct ck_ring *ring,
273 const unsigned int mask = ring->mask;
274 unsigned int consumer, producer;
276 consumer = ck_pr_load_uint(&ring->c_head);
278 producer = ck_pr_load_uint(&ring->p_tail);
280 if (CK_CC_UNLIKELY(consumer == producer))
285 buffer = (const char *)buffer + size * (consumer & mask);
286 memcpy(data, buffer, size);
288 ck_pr_fence_store_atomic();
289 return ck_pr_cas_uint(&ring->c_head, consumer, consumer + 1);
292 CK_CC_FORCE_INLINE static bool
293 _ck_ring_dequeue_mc(struct ck_ring *ring,
298 const unsigned int mask = ring->mask;
299 unsigned int consumer, producer;
301 consumer = ck_pr_load_uint(&ring->c_head);
307 * Producer counter must represent state relative to
308 * our latest consumer snapshot.
311 producer = ck_pr_load_uint(&ring->p_tail);
313 if (CK_CC_UNLIKELY(consumer == producer))
318 target = (const char *)buffer + ts * (consumer & mask);
319 memcpy(data, target, ts);
321 /* Serialize load with respect to head update. */
322 ck_pr_fence_store_atomic();
323 } while (ck_pr_cas_uint_value(&ring->c_head,
326 &consumer) == false);
332 * The ck_ring_*_spsc namespace is the public interface for interacting with a
333 * ring buffer containing pointers. Correctness is only provided if there is up
334 * to one concurrent consumer and up to one concurrent producer.
336 CK_CC_INLINE static bool
337 ck_ring_enqueue_spsc_size(struct ck_ring *ring,
338 struct ck_ring_buffer *buffer,
343 return _ck_ring_enqueue_sp_size(ring, buffer, &entry,
344 sizeof(entry), size);
347 CK_CC_INLINE static bool
348 ck_ring_enqueue_spsc(struct ck_ring *ring,
349 struct ck_ring_buffer *buffer,
353 return _ck_ring_enqueue_sp(ring, buffer,
354 &entry, sizeof(entry), NULL);
357 CK_CC_INLINE static bool
358 ck_ring_dequeue_spsc(struct ck_ring *ring,
359 const struct ck_ring_buffer *buffer,
363 return _ck_ring_dequeue_sc(ring, buffer,
364 (void **)data, sizeof(void *));
368 * The ck_ring_*_mpmc namespace is the public interface for interacting with a
369 * ring buffer containing pointers. Correctness is provided for any number of
370 * producers and consumers.
372 CK_CC_INLINE static bool
373 ck_ring_enqueue_mpmc(struct ck_ring *ring,
374 struct ck_ring_buffer *buffer,
378 return _ck_ring_enqueue_mp(ring, buffer, &entry,
379 sizeof(entry), NULL);
382 CK_CC_INLINE static bool
383 ck_ring_enqueue_mpmc_size(struct ck_ring *ring,
384 struct ck_ring_buffer *buffer,
389 return _ck_ring_enqueue_mp_size(ring, buffer, &entry,
390 sizeof(entry), size);
393 CK_CC_INLINE static bool
394 ck_ring_trydequeue_mpmc(struct ck_ring *ring,
395 const struct ck_ring_buffer *buffer,
399 return _ck_ring_trydequeue_mc(ring,
400 buffer, (void **)data, sizeof(void *));
403 CK_CC_INLINE static bool
404 ck_ring_dequeue_mpmc(struct ck_ring *ring,
405 const struct ck_ring_buffer *buffer,
409 return _ck_ring_dequeue_mc(ring, buffer, (void **)data,
414 * The ck_ring_*_spmc namespace is the public interface for interacting with a
415 * ring buffer containing pointers. Correctness is provided for any number of
416 * consumers with up to one concurrent producer.
418 CK_CC_INLINE static bool
419 ck_ring_enqueue_spmc_size(struct ck_ring *ring,
420 struct ck_ring_buffer *buffer,
425 return _ck_ring_enqueue_sp_size(ring, buffer, &entry,
426 sizeof(entry), size);
429 CK_CC_INLINE static bool
430 ck_ring_enqueue_spmc(struct ck_ring *ring,
431 struct ck_ring_buffer *buffer,
435 return _ck_ring_enqueue_sp(ring, buffer, &entry,
436 sizeof(entry), NULL);
439 CK_CC_INLINE static bool
440 ck_ring_trydequeue_spmc(struct ck_ring *ring,
441 const struct ck_ring_buffer *buffer,
445 return _ck_ring_trydequeue_mc(ring, buffer, (void **)data, sizeof(void *));
448 CK_CC_INLINE static bool
449 ck_ring_dequeue_spmc(struct ck_ring *ring,
450 const struct ck_ring_buffer *buffer,
454 return _ck_ring_dequeue_mc(ring, buffer, (void **)data, sizeof(void *));
458 * The ck_ring_*_mpsc namespace is the public interface for interacting with a
459 * ring buffer containing pointers. Correctness is provided for any number of
460 * producers with up to one concurrent consumers.
462 CK_CC_INLINE static bool
463 ck_ring_enqueue_mpsc(struct ck_ring *ring,
464 struct ck_ring_buffer *buffer,
468 return _ck_ring_enqueue_mp(ring, buffer, &entry,
469 sizeof(entry), NULL);
472 CK_CC_INLINE static bool
473 ck_ring_enqueue_mpsc_size(struct ck_ring *ring,
474 struct ck_ring_buffer *buffer,
479 return _ck_ring_enqueue_mp_size(ring, buffer, &entry,
480 sizeof(entry), size);
483 CK_CC_INLINE static bool
484 ck_ring_dequeue_mpsc(struct ck_ring *ring,
485 const struct ck_ring_buffer *buffer,
489 return _ck_ring_dequeue_sc(ring, buffer, (void **)data,
494 * CK_RING_PROTOTYPE is used to define a type-safe interface for inlining
495 * values of a particular type in the ring the buffer.
497 #define CK_RING_PROTOTYPE(name, type) \
498 CK_CC_INLINE static bool \
499 ck_ring_enqueue_spsc_size_##name(struct ck_ring *a, \
505 return _ck_ring_enqueue_sp_size(a, b, c, \
506 sizeof(struct type), d); \
509 CK_CC_INLINE static bool \
510 ck_ring_enqueue_spsc_##name(struct ck_ring *a, \
515 return _ck_ring_enqueue_sp(a, b, c, \
516 sizeof(struct type), NULL); \
519 CK_CC_INLINE static bool \
520 ck_ring_dequeue_spsc_##name(struct ck_ring *a, \
525 return _ck_ring_dequeue_sc(a, b, c, \
526 sizeof(struct type)); \
529 CK_CC_INLINE static bool \
530 ck_ring_enqueue_spmc_size_##name(struct ck_ring *a, \
536 return _ck_ring_enqueue_sp_size(a, b, c, \
537 sizeof(struct type), d); \
540 CK_CC_INLINE static bool \
541 ck_ring_enqueue_spmc_##name(struct ck_ring *a, \
546 return _ck_ring_enqueue_sp(a, b, c, \
547 sizeof(struct type), NULL); \
550 CK_CC_INLINE static bool \
551 ck_ring_trydequeue_spmc_##name(struct ck_ring *a, \
556 return _ck_ring_trydequeue_mc(a, \
557 b, c, sizeof(struct type)); \
560 CK_CC_INLINE static bool \
561 ck_ring_dequeue_spmc_##name(struct ck_ring *a, \
566 return _ck_ring_dequeue_mc(a, b, c, \
567 sizeof(struct type)); \
570 CK_CC_INLINE static bool \
571 ck_ring_enqueue_mpsc_##name(struct ck_ring *a, \
576 return _ck_ring_enqueue_mp(a, b, c, \
577 sizeof(struct type), NULL); \
580 CK_CC_INLINE static bool \
581 ck_ring_enqueue_mpsc_size_##name(struct ck_ring *a, \
587 return _ck_ring_enqueue_mp_size(a, b, c, \
588 sizeof(struct type), d); \
591 CK_CC_INLINE static bool \
592 ck_ring_dequeue_mpsc_##name(struct ck_ring *a, \
597 return _ck_ring_dequeue_sc(a, b, c, \
598 sizeof(struct type)); \
601 CK_CC_INLINE static bool \
602 ck_ring_enqueue_mpmc_size_##name(struct ck_ring *a, \
608 return _ck_ring_enqueue_mp_size(a, b, c, \
609 sizeof(struct type), d); \
612 CK_CC_INLINE static bool \
613 ck_ring_enqueue_mpmc_##name(struct ck_ring *a, \
618 return _ck_ring_enqueue_mp(a, b, c, \
619 sizeof(struct type), NULL); \
622 CK_CC_INLINE static bool \
623 ck_ring_trydequeue_mpmc_##name(struct ck_ring *a, \
628 return _ck_ring_trydequeue_mc(a, \
629 b, c, sizeof(struct type)); \
632 CK_CC_INLINE static bool \
633 ck_ring_dequeue_mpmc_##name(struct ck_ring *a, \
638 return _ck_ring_dequeue_mc(a, b, c, \
639 sizeof(struct type)); \
643 * A single producer with one concurrent consumer.
645 #define CK_RING_ENQUEUE_SPSC(name, a, b, c) \
646 ck_ring_enqueue_spsc_##name(a, b, c)
647 #define CK_RING_ENQUEUE_SPSC_SIZE(name, a, b, c, d) \
648 ck_ring_enqueue_spsc_size_##name(a, b, c, d)
649 #define CK_RING_DEQUEUE_SPSC(name, a, b, c) \
650 ck_ring_dequeue_spsc_##name(a, b, c)
653 * A single producer with any number of concurrent consumers.
655 #define CK_RING_ENQUEUE_SPMC(name, a, b, c) \
656 ck_ring_enqueue_spmc_##name(a, b, c)
657 #define CK_RING_ENQUEUE_SPMC_SIZE(name, a, b, c, d) \
658 ck_ring_enqueue_spmc_size_##name(a, b, c, d)
659 #define CK_RING_TRYDEQUEUE_SPMC(name, a, b, c) \
660 ck_ring_trydequeue_spmc_##name(a, b, c)
661 #define CK_RING_DEQUEUE_SPMC(name, a, b, c) \
662 ck_ring_dequeue_spmc_##name(a, b, c)
665 * Any number of concurrent producers with up to one
666 * concurrent consumer.
668 #define CK_RING_ENQUEUE_MPSC(name, a, b, c) \
669 ck_ring_enqueue_mpsc_##name(a, b, c)
670 #define CK_RING_ENQUEUE_MPSC_SIZE(name, a, b, c, d) \
671 ck_ring_enqueue_mpsc_size_##name(a, b, c, d)
672 #define CK_RING_DEQUEUE_MPSC(name, a, b, c) \
673 ck_ring_dequeue_mpsc_##name(a, b, c)
676 * Any number of concurrent producers and consumers.
678 #define CK_RING_ENQUEUE_MPMC(name, a, b, c) \
679 ck_ring_enqueue_mpmc_##name(a, b, c)
680 #define CK_RING_ENQUEUE_MPMC_SIZE(name, a, b, c, d) \
681 ck_ring_enqueue_mpmc_size_##name(a, b, c, d)
682 #define CK_RING_TRYDEQUEUE_MPMC(name, a, b, c) \
683 ck_ring_trydequeue_mpmc_##name(a, b, c)
684 #define CK_RING_DEQUEUE_MPMC(name, a, b, c) \
685 ck_ring_dequeue_mpmc_##name(a, b, c)
687 #endif /* CK_RING_H */