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 "kernel_types.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 
C++ compatibility of default C11 atomics types.
Types used by the kernel.
uint16_t refcount
Number of locks owned by the thread owner.
Definition: rmutex.h:56
void rmutex_unlock(rmutex_t *rmutex)
Unlocks the recursive mutex.
struct rmutex_t rmutex_t
Mutex structure.
Mutex structure.
Definition: rmutex.h:43
void rmutex_lock(rmutex_t *rmutex)
Locks a recursive mutex, blocking.
mutex_t mutex
The mutex used for locking.
Definition: rmutex.h:50
static void rmutex_init(rmutex_t *rmutex)
Initializes a recursive mutex object.
Definition: rmutex.h:80
Type with the same alignment and size as atomic_int_least16_t
atomic_int_least16_t owner
Owner thread of the mutex.
Definition: rmutex.h:65
int rmutex_trylock(rmutex_t *rmutex)
Tries to get a recursive mutex, non-blocking.
Mutex for thread synchronization.
#define RMUTEX_INIT
Static initializer for rmutex_t.
Definition: rmutex.h:72
Mutex structure.
Definition: mutex.h:37