sema.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 TriaGnoSys GmbH
3  * 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
10 #pragma once
11 
27 #include <stdint.h>
28 
29 #include "mutex.h"
30 #include "ztimer.h"
31 
32 #if IS_USED(MODULE_SEMA_DEPRECATED)
33 #include "ztimer64.h"
34 #endif
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
48 #define SEMA_CREATE(value) { (value), SEMA_OK, MUTEX_INIT }
49 
54 #define SEMA_CREATE_LOCKED() { (0), SEMA_OK, MUTEX_INIT_LOCKED }
55 
59 typedef enum {
60  SEMA_OK = 0,
61  SEMA_DESTROY,
62 } sema_state_t;
63 
67 typedef struct {
68  unsigned int value;
71 } sema_t;
72 
85 void sema_create(sema_t *sema, unsigned int value);
86 
102 void sema_destroy(sema_t *sema);
103 
111 static inline unsigned sema_get_value(const sema_t *sema)
112 {
113  return sema->value;
114 }
134 int _sema_wait_ztimer(sema_t *sema, int block,
135  ztimer_clock_t *clock, uint32_t timeout);
136 
137 #if IS_USED(MODULE_SEMA_DEPRECATED)
158 int _sema_wait_ztimer64(sema_t *sema, int block,
159  ztimer64_clock_t *clock, uint64_t timeout);
160 #endif
161 
172 static inline int sema_wait(sema_t *sema)
173 {
174  return _sema_wait_ztimer(sema, 1, NULL, 0);
175 }
176 
188 static inline int sema_try_wait(sema_t *sema)
189 {
190  return _sema_wait_ztimer(sema, 0, NULL, 0);
191 }
192 
193 #if IS_USED(MODULE_SEMA_DEPRECATED) || defined(DOXYGEN)
210 static inline int sema_wait_timed(sema_t *sema, uint64_t timeout)
211 {
212  return _sema_wait_ztimer64(sema, (timeout != 0), ZTIMER64_USEC, timeout);
213 }
214 #endif
215 
232 static inline int sema_wait_timed_ztimer(sema_t *sema,
233  ztimer_clock_t *clock,
234  uint32_t timeout)
235 {
236  return _sema_wait_ztimer(sema, (timeout != 0), clock, timeout);
237 }
238 
249 int sema_post(sema_t *sema);
250 
251 #ifdef __cplusplus
252 }
253 #endif
254 
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:172
void sema_destroy(sema_t *sema)
Destroys a semaphore.
sema_state_t
A Semaphore states.
Definition: sema.h:59
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:232
static unsigned sema_get_value(const sema_t *sema)
Get a semaphore's current value.
Definition: sema.h:111
static int sema_wait_timed(sema_t *sema, uint64_t timeout)
Wait for a semaphore being posted with a 64bit timeout.
Definition: sema.h:210
static int sema_try_wait(sema_t *sema)
Test if the semaphore is posted.
Definition: sema.h:188
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:39
A Semaphore.
Definition: sema.h:67
mutex_t mutex
mutex of the semaphore
Definition: sema.h:70
sema_state_t state
state of the semaphore
Definition: sema.h:69
unsigned int value
value of the semaphore
Definition: sema.h:68
ztimer64 clock structure
Definition: ztimer64.h:109
ztimer device structure
Definition: ztimer.h:370
ztimer 64bit API
ztimer API