sema.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016 TriaGnoSys GmbH
3  * SPDX-FileCopyrightText: 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
24 #include <stdint.h>
25 
26 #include "mutex.h"
27 #include "ztimer.h"
28 
29 #if IS_USED(MODULE_SEMA_DEPRECATED)
30 #include "ztimer64.h"
31 #endif
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
45 #define SEMA_CREATE(value) { (value), SEMA_OK, MUTEX_INIT }
46 
51 #define SEMA_CREATE_LOCKED() { (0), SEMA_OK, MUTEX_INIT_LOCKED }
52 
56 typedef enum {
57  SEMA_OK = 0,
58  SEMA_DESTROY,
59 } sema_state_t;
60 
64 typedef struct {
65  unsigned int value;
68 } sema_t;
69 
82 void sema_create(sema_t *sema, unsigned int value);
83 
99 void sema_destroy(sema_t *sema);
100 
108 static inline unsigned sema_get_value(const sema_t *sema)
109 {
110  return sema->value;
111 }
131 int _sema_wait_ztimer(sema_t *sema, int block,
132  ztimer_clock_t *clock, uint32_t timeout);
133 
134 #if IS_USED(MODULE_SEMA_DEPRECATED)
155 int _sema_wait_ztimer64(sema_t *sema, int block,
156  ztimer64_clock_t *clock, uint64_t timeout);
157 #endif
158 
169 static inline int sema_wait(sema_t *sema)
170 {
171  return _sema_wait_ztimer(sema, 1, NULL, 0);
172 }
173 
185 static inline int sema_try_wait(sema_t *sema)
186 {
187  return _sema_wait_ztimer(sema, 0, NULL, 0);
188 }
189 
190 #if IS_USED(MODULE_SEMA_DEPRECATED) || defined(DOXYGEN)
207 static inline int sema_wait_timed(sema_t *sema, uint64_t timeout)
208 {
209  return _sema_wait_ztimer64(sema, (timeout != 0), ZTIMER64_USEC, timeout);
210 }
211 #endif
212 
229 static inline int sema_wait_timed_ztimer(sema_t *sema,
230  ztimer_clock_t *clock,
231  uint32_t timeout)
232 {
233  return _sema_wait_ztimer(sema, (timeout != 0), clock, timeout);
234 }
235 
246 int sema_post(sema_t *sema);
247 
248 #ifdef __cplusplus
249 }
250 #endif
251 
void sema_create(sema_t *sema, unsigned int value)
Creates semaphore dynamically.
static int sema_wait(sema_t *sema)
Wait for a semaphore being posted (without timeout).
Definition: sema.h:169
void sema_destroy(sema_t *sema)
Destroys a semaphore.
sema_state_t
A Semaphore states.
Definition: sema.h:56
static int sema_wait_timed_ztimer(sema_t *sema, ztimer_clock_t *clock, uint32_t timeout)
Wait for a semaphore being posted, using ztimer as backend.
Definition: sema.h:229
static unsigned sema_get_value(const sema_t *sema)
Get a semaphore's current value.
Definition: sema.h:108
static int sema_wait_timed(sema_t *sema, uint64_t timeout)
Wait for a semaphore being posted with a 64bit timeout.
Definition: sema.h:207
static int sema_try_wait(sema_t *sema)
Test if the semaphore is posted.
Definition: sema.h:185
int sema_post(sema_t *sema)
Signal semaphore.
int _sema_wait_ztimer(sema_t *sema, int block, ztimer_clock_t *clock, uint32_t timeout)
Wait for a semaphore, blocking or non-blocking.
ztimer64_clock_t *const ZTIMER64_USEC
Default ztimer microsecond clock.
Mutex for thread synchronization.
Mutex structure.
Definition: mutex.h:36
A Semaphore.
Definition: sema.h:64
mutex_t mutex
mutex of the semaphore
Definition: sema.h:67
sema_state_t state
state of the semaphore
Definition: sema.h:66
unsigned int value
value of the semaphore
Definition: sema.h:65
ztimer64 clock structure
Definition: ztimer64.h:106
ztimer device structure
Definition: ztimer.h:367
ztimer 64bit API
ztimer API