Skip to content

Commit dcd3ee7

Browse files
maribumguetschow
andcommitted
core/cib: Add a prominent warning about thread safety
Users of CIB may easily be mislead to believing that it is generally thread safe, or at least thread safe as long as there is a single consumer and producer. Neither is the case, which now is clearly communicated in the API doc. Co-authored-by: mguetschow <mikolai.guetschow@tu-dresden.de>
1 parent be88208 commit dcd3ee7

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

core/lib/include/cib.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
* and combined with an memory array forms a circular buffer.
1818
*
1919
* @author Kaspar Schleiser <kaspar@schleiser.de>
20+
*
21+
* @warning This API is NOT thread-safe.
22+
*
23+
* @note It may appear that the functions would be thread safe if the
24+
* management data structure is only ever accessed by a single
25+
* consumer and a singler producer on platforms where loads and
26+
* stores to `unsigned int` is atomic. But even this is not the
27+
* case, as this would require careful use of memory barriers to
28+
* ensure correct order of memory accesses. So: Just make sure
29+
* to use either a mutex or to disable interrupts to ensure
30+
* exclusive access as indicated in the API doc.
2031
*/
2132

2233
#ifndef CIB_H
@@ -84,6 +95,9 @@ static inline unsigned int cib_size(const cib_t *cib)
8495
* @param[in] cib the cib_t to check.
8596
* Must not be NULL.
8697
* @return How often cib_get() can be called before @p cib is empty.
98+
*
99+
* @warning This function is not thread safe. (The caller needs to ensure
100+
* exclusive access to the `cib_t`.)
87101
*/
88102
static inline unsigned int cib_avail(const cib_t *cib)
89103
{
@@ -96,6 +110,9 @@ static inline unsigned int cib_avail(const cib_t *cib)
96110
* @param[in] cib the cib_t to check.
97111
* Must not be NULL.
98112
* @return 1 if cib_put() would return "-1", 0 otherwise
113+
*
114+
* @warning This function is not thread safe. (The caller needs to ensure
115+
* exclusive access to the `cib_t`.)
99116
*/
100117
static inline unsigned int cib_full(const cib_t *cib)
101118
{
@@ -109,6 +126,9 @@ static inline unsigned int cib_full(const cib_t *cib)
109126
* Must not be NULL.
110127
* @return index of next item
111128
* @retval -1 if the buffer is empty
129+
*
130+
* @warning This function is not thread safe. (The caller needs to ensure
131+
* exclusive access to the `cib_t`.)
112132
*/
113133
static inline int cib_get(cib_t *__restrict cib)
114134
{
@@ -134,6 +154,10 @@ static inline int cib_get(cib_t *__restrict cib)
134154
*
135155
* @return index of item
136156
* @retval -1 if no item at @p offset exists in the buffer
157+
*
158+
* @warning This function is not thread safe. (The caller needs to ensure
159+
* exclusive access to the `cib_t` and must not release that exclusive
160+
* access between the call to @ref cib_avail and this function.)
137161
*/
138162
static inline int cib_peek_at_unsafe(const cib_t *__restrict cib, unsigned offset)
139163
{
@@ -152,6 +176,9 @@ static inline int cib_peek_at_unsafe(const cib_t *__restrict cib, unsigned offse
152176
*
153177
* @return index of item
154178
* @retval -1 if no item at @p offset exists in the buffer
179+
*
180+
* @warning This function is not thread safe. (The caller needs to ensure
181+
* exclusive access to the `cib_t`.)
155182
*/
156183
static inline int cib_peek_at(const cib_t *__restrict cib, unsigned offset)
157184
{
@@ -171,6 +198,10 @@ static inline int cib_peek_at(const cib_t *__restrict cib, unsigned offset)
171198
* Must not be NULL.
172199
* @return index of next item
173200
* @retval -1 if the buffer is empty
201+
*
202+
* @warning This function is not thread safe. (The caller needs to ensure
203+
* exclusive access to the `cib_t` and must not release that exclusive
204+
* access between the call to @ref cib_avail and this function.)
174205
*/
175206
static inline int cib_peek_unsafe(const cib_t *__restrict cib)
176207
{
@@ -184,6 +215,9 @@ static inline int cib_peek_unsafe(const cib_t *__restrict cib)
184215
* Must not be NULL.
185216
* @return index of next item
186217
* @retval -1 if the buffer is empty
218+
*
219+
* @warning This function is not thread safe. (The caller needs to ensure
220+
* exclusive access to the `cib_t`.)
187221
*/
188222
static inline int cib_peek(const cib_t *__restrict cib)
189223
{
@@ -198,6 +232,9 @@ static inline int cib_peek(const cib_t *__restrict cib)
198232
* @param[in,out] cib corresponding *cib* to buffer.
199233
* Must not be NULL.
200234
* @return index of next item
235+
*
236+
* @warning This function is not thread safe. (The caller needs to ensure
237+
* exclusive access to the `cib_t`.)
201238
*/
202239
static inline int cib_get_unsafe(cib_t *cib)
203240
{
@@ -211,6 +248,9 @@ static inline int cib_get_unsafe(cib_t *cib)
211248
* Must not be NULL.
212249
* @return index of item to put to
213250
* @retval -1 if the buffer is full
251+
*
252+
* @warning This function is not thread safe. (The caller needs to ensure
253+
* exclusive access to the `cib_t`.)
214254
*/
215255
static inline int cib_put(cib_t *__restrict cib)
216256
{
@@ -232,6 +272,9 @@ static inline int cib_put(cib_t *__restrict cib)
232272
* @param[in,out] cib corresponding *cib* to buffer.
233273
* Must not be NULL.
234274
* @return index of item to put to
275+
*
276+
* @warning This function is not thread safe. (The caller needs to ensure
277+
* exclusive access to the `cib_t`.)
235278
*/
236279
static inline int cib_put_unsafe(cib_t *cib)
237280
{

0 commit comments

Comments
 (0)