rmutex.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Theobroma Systems Design & 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 
23 #ifndef RMUTEX_H
24 #define RMUTEX_H
25 
26 #include <stdint.h>
27 #ifdef __cplusplus
28 #include "c11_atomics_compat.hpp"
29 #else
30 #include <stdatomic.h>
31 #endif
32 
33 #include "mutex.h"
34 #include "sched.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
43 typedef struct rmutex_t {
44  /* fields are managed by mutex functions, don't touch */
51 
56  uint16_t refcount;
57 
66 } rmutex_t;
67 
72 #define RMUTEX_INIT { MUTEX_INIT, 0, ATOMIC_VAR_INIT(KERNEL_PID_UNDEF) }
73 
80 static inline void rmutex_init(rmutex_t *rmutex)
81 {
82  rmutex_t empty_rmutex = RMUTEX_INIT;
83 
84  *rmutex = empty_rmutex;
85 }
86 
96 int rmutex_trylock(rmutex_t *rmutex);
97 
104 void rmutex_lock(rmutex_t *rmutex);
105 
111 void rmutex_unlock(rmutex_t *rmutex);
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif /* RMUTEX_H */
118 
rmutex_unlock
void rmutex_unlock(rmutex_t *rmutex)
Unlocks the recursive mutex.
RMUTEX_INIT
#define RMUTEX_INIT
Static initializer for rmutex_t.
Definition: rmutex.h:72
rmutex_trylock
int rmutex_trylock(rmutex_t *rmutex)
Tries to get a recursive mutex, non-blocking.
rmutex_t
struct rmutex_t rmutex_t
Mutex structure.
rmutex_t::mutex
mutex_t mutex
The mutex used for locking.
Definition: rmutex.h:50
sched.h
Scheduler API definition.
rmutex_lock
void rmutex_lock(rmutex_t *rmutex)
Locks a recursive mutex, blocking.
rmutex_t
Mutex structure.
Definition: rmutex.h:43
c11_atomics_compat.hpp
C++ compatibility of default C11 atomics types.
mutex.h
Mutex for thread synchronization.
atomic_int_least16_t
Type with the same alignment and size as atomic_int_least16_t
Definition: c11_atomics_compat.hpp:214
rmutex_t::refcount
uint16_t refcount
Number of locks owned by the thread owner.
Definition: rmutex.h:56
rmutex_init
static void rmutex_init(rmutex_t *rmutex)
Initializes a recursive mutex object.
Definition: rmutex.h:80
rmutex_t::owner
atomic_int_least16_t owner
Owner thread of the mutex.
Definition: rmutex.h:65
mutex_t
Mutex structure.
Definition: mutex.h:120