Loading...
Searching...
No Matches
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
25#ifndef SEMA_H
26#define SEMA_H
27
28#include <stdint.h>
29
30#include "mutex.h"
31#include "ztimer.h"
32
33#if IS_USED(MODULE_SEMA_DEPRECATED)
34#include "ztimer64.h"
35#endif
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
49#define SEMA_CREATE(value) { (value), SEMA_OK, MUTEX_INIT }
50
55#define SEMA_CREATE_LOCKED() { (0), SEMA_OK, MUTEX_INIT_LOCKED }
56
60typedef enum {
61 SEMA_OK = 0,
62 SEMA_DESTROY,
64
68typedef struct {
69 unsigned int value;
72} sema_t;
73
86void sema_create(sema_t *sema, unsigned int value);
87
104
112static inline unsigned sema_get_value(const sema_t *sema)
113{
114 return sema->value;
115}
135int _sema_wait_ztimer(sema_t *sema, int block,
136 ztimer_clock_t *clock, uint32_t timeout);
137
138#if IS_USED(MODULE_SEMA_DEPRECATED)
159int _sema_wait_ztimer64(sema_t *sema, int block,
160 ztimer64_clock_t *clock, uint64_t timeout);
161#endif
162
173static inline int sema_wait(sema_t *sema)
174{
175 return _sema_wait_ztimer(sema, 1, NULL, 0);
176}
177
189static inline int sema_try_wait(sema_t *sema)
190{
191 return _sema_wait_ztimer(sema, 0, NULL, 0);
192}
193
194#if IS_USED(MODULE_SEMA_DEPRECATED) || defined(DOXYGEN)
211static inline int sema_wait_timed(sema_t *sema, uint64_t timeout)
212{
213 return _sema_wait_ztimer64(sema, (timeout != 0), ZTIMER64_USEC, timeout);
214}
215#endif
216
233static inline int sema_wait_timed_ztimer(sema_t *sema,
234 ztimer_clock_t *clock,
235 uint32_t timeout)
236{
237 return _sema_wait_ztimer(sema, (timeout != 0), clock, timeout);
238}
239
250int sema_post(sema_t *sema);
251
252#ifdef __cplusplus
253}
254#endif
255
256#endif /* SEMA_H */
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:173
void sema_destroy(sema_t *sema)
Destroys a semaphore.
sema_state_t
A Semaphore states.
Definition sema.h:60
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:233
static unsigned sema_get_value(const sema_t *sema)
Get a semaphore's current value.
Definition sema.h:112
static int sema_wait_timed(sema_t *sema, uint64_t timeout)
Wait for a semaphore being posted with a 64bit timeout.
Definition sema.h:211
static int sema_try_wait(sema_t *sema)
Test if the semaphore is posted.
Definition sema.h:189
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:146
A Semaphore.
Definition sema.h:68
mutex_t mutex
mutex of the semaphore
Definition sema.h:71
sema_state_t state
state of the semaphore
Definition sema.h:70
unsigned int value
value of the semaphore
Definition sema.h:69
ztimer64 clock structure
Definition ztimer64.h:110
ztimer device structure
Definition ztimer.h:369
ztimer 64bit API
ztimer API