sema_inv.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2021 ML!PA Consulting GmbH
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
41 #include "atomic_utils.h"
42 #include "mutex.h"
43 
44 #ifdef MODULE_XTIMER
45 #include "xtimer.h"
46 #endif
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
55 typedef struct {
56  uint32_t value;
58 } sema_inv_t;
59 
73 
87 bool sema_inv_post_mask(sema_inv_t *s, uint32_t mask);
88 
95 static inline void sema_inv_init(sema_inv_t *s, uint32_t value)
96 {
97  const mutex_t locked = MUTEX_INIT_LOCKED;
98  s->lock = locked;
99  s->value = value;
100 }
101 
107 static inline void sema_inv_wait(sema_inv_t *s)
108 {
109  mutex_lock(&s->lock);
110 }
111 
120 static inline int sema_inv_try_wait(sema_inv_t *s)
121 {
122  return mutex_trylock(&s->lock);
123 }
124 
125 #if defined(MODULE_XTIMER) || DOXYGEN
136 static inline int sema_inv_wait_timeout(sema_inv_t *s, uint32_t us)
137 {
138  return xtimer_mutex_lock_timeout(&s->lock, us);
139 }
140 #endif
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
API of the utility functions for atomic accesses.
#define MUTEX_INIT_LOCKED
Static initializer for mutex_t with a locked mutex.
Definition: mutex.h:119
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
Definition: mutex.h:202
static int mutex_trylock(mutex_t *mutex)
Tries to get a mutex, non-blocking.
Definition: mutex.h:186
static int sema_inv_wait_timeout(sema_inv_t *s, uint32_t us)
Wait for the inverse semaphore value to reach zero or a timeout being reached.
Definition: sema_inv.h:136
bool sema_inv_post_mask(sema_inv_t *s, uint32_t mask)
Signal semaphore (mask mode).
bool sema_inv_post(sema_inv_t *s)
Signal semaphore (counter mode).
static void sema_inv_init(sema_inv_t *s, uint32_t value)
Initialize an inverse semaphore.
Definition: sema_inv.h:95
static void sema_inv_wait(sema_inv_t *s)
Wait for the inverse semaphore value to reach zero.
Definition: sema_inv.h:107
static int sema_inv_try_wait(sema_inv_t *s)
Check if the inverse semaphore value has reached zero.
Definition: sema_inv.h:120
int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us)
lock a mutex but with timeout
Mutex for thread synchronization.
Mutex structure.
Definition: mutex.h:36
An Inverse Semaphore.
Definition: sema_inv.h:55
uint32_t value
value of the semaphore
Definition: sema_inv.h:56
mutex_t lock
mutex of the semaphore
Definition: sema_inv.h:57
xtimer interface definitions