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 
9 #pragma once
10 
44 #include "atomic_utils.h"
45 #include "mutex.h"
46 
47 #ifdef MODULE_XTIMER
48 #include "xtimer.h"
49 #endif
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
58 typedef struct {
59  uint32_t value;
61 } sema_inv_t;
62 
76 
90 bool sema_inv_post_mask(sema_inv_t *s, uint32_t mask);
91 
98 static inline void sema_inv_init(sema_inv_t *s, uint32_t value)
99 {
100  const mutex_t locked = MUTEX_INIT_LOCKED;
101  s->lock = locked;
102  s->value = value;
103 }
104 
110 static inline void sema_inv_wait(sema_inv_t *s)
111 {
112  mutex_lock(&s->lock);
113 }
114 
123 static inline int sema_inv_try_wait(sema_inv_t *s)
124 {
125  return mutex_trylock(&s->lock);
126 }
127 
128 #if defined(MODULE_XTIMER) || DOXYGEN
139 static inline int sema_inv_wait_timeout(sema_inv_t *s, uint32_t us)
140 {
141  return xtimer_mutex_lock_timeout(&s->lock, us);
142 }
143 #endif
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
API of the utility functions for atomic accesses.
#define MUTEX_INIT_LOCKED
Static initializer for mutex_t with a locked mutex.
Definition: mutex.h:122
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
Definition: mutex.h:205
static int mutex_trylock(mutex_t *mutex)
Tries to get a mutex, non-blocking.
Definition: mutex.h:189
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:139
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:98
static void sema_inv_wait(sema_inv_t *s)
Wait for the inverse semaphore value to reach zero.
Definition: sema_inv.h:110
static int sema_inv_try_wait(sema_inv_t *s)
Check if the inverse semaphore value has reached zero.
Definition: sema_inv.h:123
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:39
An Inverse Semaphore.
Definition: sema_inv.h:58
uint32_t value
value of the semaphore
Definition: sema_inv.h:59
mutex_t lock
mutex of the semaphore
Definition: sema_inv.h:60
xtimer interface definitions