Lely core libraries 2.3.4
spscring.h
Go to the documentation of this file.
1
35#ifndef LELY_UTIL_SPSCRING_H_
36#define LELY_UTIL_SPSCRING_H_
37
38#include <lely/features.h>
39
40#if !LELY_NO_ATOMICS
41#ifdef __cplusplus
42#include <atomic>
43#else
44#include <lely/libc/stdatomic.h>
45#endif
46#endif // !LELY_NO_ATOMICS
47
48#include <stddef.h>
49
50#if LELY_NO_ATOMICS
51typedef size_t spscring_atomic_t;
52#elif defined(__cplusplus)
53using spscring_atomic_t = ::std::atomic_size_t;
54#else // C11
55typedef atomic_size_t spscring_atomic_t;
56#endif
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
63struct spscring {
64 struct {
65 _Alignas(LEVEL1_DCACHE_LINESIZE) struct spscring_ctx {
66 size_t size;
67 size_t base;
68 size_t pos;
69 size_t end;
70 } ctx;
71 char _pad1[LEVEL1_DCACHE_LINESIZE
72 - sizeof(struct spscring_ctx)];
73 _Alignas(LEVEL1_DCACHE_LINESIZE) spscring_atomic_t pos;
74 char _pad2[LEVEL1_DCACHE_LINESIZE - sizeof(spscring_atomic_t)];
75 _Alignas(LEVEL1_DCACHE_LINESIZE) struct spscring_sig {
76 spscring_atomic_t size;
77 void (*func)(struct spscring *ring, void *arg);
78 void *arg;
79 } sig;
80 char _pad3[LEVEL1_DCACHE_LINESIZE
81 - sizeof(struct spscring_sig)];
82 } p, c;
83};
84
86#if LELY_NO_ATOMICS
87#define SPSCRING_INIT(size) \
88 { \
89 { { (size), 0, 0, 0 }, { 0 }, 0, { 0 }, { 0, NULL, NULL }, \
90 { 0 } }, \
91 { \
92 { (size), 0, 0, 0 }, { 0 }, 0, { 0 }, \
93 { 0, NULL, NULL }, { 0 }, \
94 } \
95 }
96#else
97#define SPSCRING_INIT(size) \
98 { \
99 { \
100 { (size), 0, 0, 0 }, \
101 { 0 }, \
102 ATOMIC_VAR_INIT(0), \
103 { 0 }, \
104 { ATOMIC_VAR_INIT(0), NULL, NULL }, \
105 { 0 }, \
106 }, \
107 { \
108 { (size), 0, 0, 0 }, { 0 }, ATOMIC_VAR_INIT(0), { 0 }, \
109 { ATOMIC_VAR_INIT(0), NULL, NULL }, \
110 { 0 }, \
111 } \
112 }
113#endif
114
119void spscring_init(struct spscring *ring, size_t size);
120
122size_t spscring_size(const struct spscring *ring);
123
135size_t spscring_p_capacity(struct spscring *ring);
136
148size_t spscring_p_capacity_no_wrap(struct spscring *ring);
149
167size_t spscring_p_alloc(struct spscring *ring, size_t *psize);
168
186size_t spscring_p_alloc_no_wrap(struct spscring *ring, size_t *psize);
187
203size_t spscring_p_commit(struct spscring *ring, size_t size);
204
227int spscring_p_submit_wait(struct spscring *ring, size_t size,
228 void (*func)(struct spscring *ring, void *arg), void *arg);
229
240int spscring_p_abort_wait(struct spscring *ring);
241
253size_t spscring_c_capacity(struct spscring *ring);
254
266size_t spscring_c_capacity_no_wrap(struct spscring *ring);
267
285size_t spscring_c_alloc(struct spscring *ring, size_t *psize);
286
304size_t spscring_c_alloc_no_wrap(struct spscring *ring, size_t *psize);
305
321size_t spscring_c_commit(struct spscring *ring, size_t size);
322
345int spscring_c_submit_wait(struct spscring *ring, size_t size,
346 void (*func)(struct spscring *ring, void *arg), void *arg);
347
358int spscring_c_abort_wait(struct spscring *ring);
359
360#ifdef __cplusplus
361}
362#endif
363
364#endif // !LELY_UTIL_SPSCRING_H_
This header file is part of the Lely libraries; it contains the compiler feature definitions.
#define LEVEL1_DCACHE_LINESIZE
The presumed size (in bytes) of a line in the L1 data cache.
Definition features.h:302
int spscring_p_abort_wait(struct spscring *ring)
Aborts a wait operation previously registered with spscring_p_submit_wait().
Definition spscring.c:204
size_t spscring_c_alloc(struct spscring *ring, size_t *psize)
Allocates a consecutive range of indices, including wrapping, in a single-producer,...
Definition spscring.c:262
int spscring_c_submit_wait(struct spscring *ring, size_t size, void(*func)(struct spscring *ring, void *arg), void *arg)
Checks if the requested range of indices, including wrapping, in a single-producer,...
Definition spscring.c:327
size_t spscring_p_capacity(struct spscring *ring)
Returns the total capacity available for a producer in a single-producer single-consumer ring buffer,...
Definition spscring.c:65
size_t spscring_p_commit(struct spscring *ring, size_t size)
Makes the specified number of indices available to a consumer and, if this satisfies a wait operation...
Definition spscring.c:138
int spscring_p_submit_wait(struct spscring *ring, size_t size, void(*func)(struct spscring *ring, void *arg), void *arg)
Checks if the requested range of indices, including wrapping, in a single-producer,...
Definition spscring.c:163
size_t spscring_c_capacity_no_wrap(struct spscring *ring)
Returns the total capacity available for a consumer in a single-producer single-consumer ring buffer,...
Definition spscring.c:247
size_t spscring_c_commit(struct spscring *ring, size_t size)
Makes the specified number of indices available to a producer and, if this satisfies a wait operation...
Definition spscring.c:302
size_t spscring_c_capacity(struct spscring *ring)
Returns the total capacity available for a consumer in a single-producer single-consumer ring buffer,...
Definition spscring.c:229
int spscring_c_abort_wait(struct spscring *ring)
Aborts a wait operation previously registered with spscring_c_submit_wait().
Definition spscring.c:368
size_t spscring_p_capacity_no_wrap(struct spscring *ring)
Returns the total capacity available for a producer in a single-producer single-consumer ring buffer,...
Definition spscring.c:83
size_t spscring_c_alloc_no_wrap(struct spscring *ring, size_t *psize)
Allocates a consecutive range of indices, without wrapping, in a single-producer, single-consumer rin...
Definition spscring.c:282
void spscring_init(struct spscring *ring, size_t size)
Initializes a single-producer, single-consumer ring buffer with the specified size.
Definition spscring.c:47
size_t spscring_p_alloc_no_wrap(struct spscring *ring, size_t *psize)
Allocates a consecutive range of indices, without wrapping, in a single-producer, single-consumer rin...
Definition spscring.c:118
size_t spscring_size(const struct spscring *ring)
Returns the size of a single-producer, single-consumer ring buffer.
Definition spscring.c:56
size_t spscring_p_alloc(struct spscring *ring, size_t *psize)
Allocates a consecutive range of indices, including wrapping, in a single-producer,...
Definition spscring.c:98
This header file is part of the C11 and POSIX compatibility library; it includes <stdatomic....
This header file is part of the C11 and POSIX compatibility library; it includes <stddef....
A single-producer, single-consumer ring buffer.
Definition spscring.h:63