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
51 typedef size_t spscring_atomic_t;
52 #elif defined(__cplusplus)
53 using spscring_atomic_t = ::std::atomic_size_t;
54 #else // C11
55 typedef atomic_size_t spscring_atomic_t;
56 #endif
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
63 struct 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 
119 void spscring_init(struct spscring *ring, size_t size);
120 
122 size_t spscring_size(const struct spscring *ring);
123 
135 size_t spscring_p_capacity(struct spscring *ring);
136 
148 size_t spscring_p_capacity_no_wrap(struct spscring *ring);
149 
167 size_t spscring_p_alloc(struct spscring *ring, size_t *psize);
168 
186 size_t spscring_p_alloc_no_wrap(struct spscring *ring, size_t *psize);
187 
203 size_t spscring_p_commit(struct spscring *ring, size_t size);
204 
227 int spscring_p_submit_wait(struct spscring *ring, size_t size,
228  void (*func)(struct spscring *ring, void *arg), void *arg);
229 
240 int spscring_p_abort_wait(struct spscring *ring);
241 
253 size_t spscring_c_capacity(struct spscring *ring);
254 
266 size_t spscring_c_capacity_no_wrap(struct spscring *ring);
267 
285 size_t spscring_c_alloc(struct spscring *ring, size_t *psize);
286 
304 size_t spscring_c_alloc_no_wrap(struct spscring *ring, size_t *psize);
305 
321 size_t spscring_c_commit(struct spscring *ring, size_t size);
322 
345 int spscring_c_submit_wait(struct spscring *ring, size_t size,
346  void (*func)(struct spscring *ring, void *arg), void *arg);
347 
358 int spscring_c_abort_wait(struct spscring *ring);
359 
360 #ifdef __cplusplus
361 }
362 #endif
363 
364 #endif // !LELY_UTIL_SPSCRING_H_
features.h
spscring_c_capacity
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
spscring_size
size_t spscring_size(const struct spscring *ring)
Returns the size of a single-producer, single-consumer ring buffer.
Definition: spscring.c:56
spscring_c_commit
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
spscring_c_abort_wait
int spscring_c_abort_wait(struct spscring *ring)
Aborts a wait operation previously registered with spscring_c_submit_wait().
Definition: spscring.c:368
spscring_c_alloc
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
spscring_c_capacity_no_wrap
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
spscring_c_submit_wait
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
spscring_p_alloc
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
spscring_c_alloc_no_wrap
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
spscring_init
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
spscring_p_alloc_no_wrap
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
LEVEL1_DCACHE_LINESIZE
#define LEVEL1_DCACHE_LINESIZE
The presumed size (in bytes) of a line in the L1 data cache.
Definition: features.h:302
spscring_p_submit_wait
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
spscring_p_capacity_no_wrap
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
spscring_p_abort_wait
int spscring_p_abort_wait(struct spscring *ring)
Aborts a wait operation previously registered with spscring_p_submit_wait().
Definition: spscring.c:204
stdatomic.h
spscring_p_capacity
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
spscring
A single-producer, single-consumer ring buffer.
Definition: spscring.h:63
spscring_p_commit
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
stddef.h