sema_inv.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 ML!PA Consulting GmbH
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
42 #ifndef SEMA_INV_H
43 #define SEMA_INV_H
44 
45 #include "atomic_utils.h"
46 #include "mutex.h"
47 
48 #ifdef MODULE_XTIMER
49 #include "xtimer.h"
50 #endif
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
59 typedef struct {
60  uint32_t value;
62 } sema_inv_t;
63 
77 
91 bool sema_inv_post_mask(sema_inv_t *s, uint32_t mask);
92 
99 static inline void sema_inv_init(sema_inv_t *s, uint32_t value)
100 {
101  const mutex_t locked = MUTEX_INIT_LOCKED;
102  s->lock = locked;
103  s->value = value;
104 }
105 
111 static inline void sema_inv_wait(sema_inv_t *s)
112 {
113  mutex_lock(&s->lock);
114 }
115 
124 static inline int sema_inv_try_wait(sema_inv_t *s)
125 {
126  return mutex_trylock(&s->lock);
127 }
128 
129 #if defined(MODULE_XTIMER) || DOXYGEN
140 static inline int sema_inv_wait_timeout(sema_inv_t *s, uint32_t us)
141 {
142  return xtimer_mutex_lock_timeout(&s->lock, us);
143 }
144 #endif
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif /* SEMA_INV_H */
API of the utility functions for atomic accesses.
#define MUTEX_INIT_LOCKED
Static initializer for mutex_t with a locked mutex.
Definition: mutex.h:229
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
Definition: mutex.h:312
static int mutex_trylock(mutex_t *mutex)
Tries to get a mutex, non-blocking.
Definition: mutex.h:296
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:140
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:99
static void sema_inv_wait(sema_inv_t *s)
Wait for the inverse semaphore value to reach zero.
Definition: sema_inv.h:111
static int sema_inv_try_wait(sema_inv_t *s)
Check if the inverse semaphore value has reached zero.
Definition: sema_inv.h:124
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:146
An Inverse Semaphore.
Definition: sema_inv.h:59
uint32_t value
value of the semaphore
Definition: sema_inv.h:60
mutex_t lock
mutex of the semaphore
Definition: sema_inv.h:61
xtimer interface definitions