Loading...
Searching...
No Matches
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
23
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
34extern "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
56typedef enum {
57 SEMA_OK = 0,
58 SEMA_DESTROY,
60
64typedef struct {
65 unsigned int value;
68} sema_t;
69
82void sema_create(sema_t *sema, unsigned int value);
83
99void sema_destroy(sema_t *sema);
100
108static inline unsigned sema_get_value(const sema_t *sema)
109{
110 return sema->value;
111}
112
131int _sema_wait_ztimer(sema_t *sema, int block,
132 ztimer_clock_t *clock, uint32_t timeout);
133
134#if IS_USED(MODULE_SEMA_DEPRECATED)
155int _sema_wait_ztimer64(sema_t *sema, int block,
156 ztimer64_clock_t *clock, uint64_t timeout);
157#endif
158
169static inline int sema_wait(sema_t *sema)
170{
171 return _sema_wait_ztimer(sema, 1, NULL, 0);
172}
173
185static 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)
207static 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
229static 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
246int 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.
struct ztimer64_clock ztimer64_clock_t
ztimer64_clock_t forward declaration
Definition ztimer64.h:81
ztimer64_clock_t *const ZTIMER64_USEC
Default ztimer microsecond clock.
struct ztimer_clock ztimer_clock_t
ztimer_clock_t forward declaration
Definition ztimer.h:287
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
ztimer 64bit API
ztimer API