aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios/os/rt/include/chmtx.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios/os/rt/include/chmtx.h')
-rw-r--r--lib/chibios/os/rt/include/chmtx.h168
1 files changed, 168 insertions, 0 deletions
diff --git a/lib/chibios/os/rt/include/chmtx.h b/lib/chibios/os/rt/include/chmtx.h
new file mode 100644
index 000000000..ba06f23a8
--- /dev/null
+++ b/lib/chibios/os/rt/include/chmtx.h
@@ -0,0 +1,168 @@
1/*
2 ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
3 2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
4
5 This file is part of ChibiOS.
6
7 ChibiOS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation version 3 of the License.
10
11 ChibiOS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20/**
21 * @file rt/include/chmtx.h
22 * @brief Mutexes macros and structures.
23 *
24 * @addtogroup mutexes
25 * @{
26 */
27
28#ifndef CHMTX_H
29#define CHMTX_H
30
31#if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
32
33/*===========================================================================*/
34/* Module constants. */
35/*===========================================================================*/
36
37/*===========================================================================*/
38/* Module pre-compile time settings. */
39/*===========================================================================*/
40
41/*===========================================================================*/
42/* Derived constants and error checks. */
43/*===========================================================================*/
44
45/*===========================================================================*/
46/* Module data structures and types. */
47/*===========================================================================*/
48
49/**
50 * @brief Type of a mutex structure.
51 */
52typedef struct ch_mutex mutex_t;
53
54/**
55 * @brief Mutex structure.
56 */
57struct ch_mutex {
58 ch_queue_t queue; /**< @brief Queue of the threads sleeping
59 on this mutex. */
60 thread_t *owner; /**< @brief Owner @p thread_t pointer or
61 @p NULL. */
62 mutex_t *next; /**< @brief Next @p mutex_t into an
63 owner-list or @p NULL. */
64#if (CH_CFG_USE_MUTEXES_RECURSIVE == TRUE) || defined(__DOXYGEN__)
65 cnt_t cnt; /**< @brief Mutex recursion counter. */
66#endif
67};
68
69/*===========================================================================*/
70/* Module macros. */
71/*===========================================================================*/
72
73/**
74 * @brief Data part of a static mutex initializer.
75 * @details This macro should be used when statically initializing a mutex
76 * that is part of a bigger structure.
77 *
78 * @param[in] name the name of the mutex variable
79 */
80#if (CH_CFG_USE_MUTEXES_RECURSIVE == TRUE) || defined(__DOXYGEN__)
81#define _MUTEX_DATA(name) {_CH_QUEUE_DATA(name.queue), NULL, NULL, 0}
82#else
83#define _MUTEX_DATA(name) {_CH_QUEUE_DATA(name.queue), NULL, NULL}
84#endif
85
86/**
87 * @brief Static mutex initializer.
88 * @details Statically initialized mutexes require no explicit initialization
89 * using @p chMtxInit().
90 *
91 * @param[in] name the name of the mutex variable
92 */
93#define MUTEX_DECL(name) mutex_t name = _MUTEX_DATA(name)
94
95/*===========================================================================*/
96/* External declarations. */
97/*===========================================================================*/
98
99#ifdef __cplusplus
100extern "C" {
101#endif
102 void chMtxObjectInit(mutex_t *mp);
103 void chMtxLock(mutex_t *mp);
104 void chMtxLockS(mutex_t *mp);
105 bool chMtxTryLock(mutex_t *mp);
106 bool chMtxTryLockS(mutex_t *mp);
107 void chMtxUnlock(mutex_t *mp);
108 void chMtxUnlockS(mutex_t *mp);
109 void chMtxUnlockAll(void);
110 void chMtxUnlockAllS(void);
111#ifdef __cplusplus
112}
113#endif
114
115/*===========================================================================*/
116/* Module inline functions. */
117/*===========================================================================*/
118
119/**
120 * @brief Returns @p true if the mutex queue contains at least a waiting
121 * thread.
122 *
123 * @param[out] mp pointer to a @p mutex_t structure
124 * @return The mutex queue status.
125 *
126 * @sclass
127 */
128static inline bool chMtxQueueNotEmptyS(mutex_t *mp) {
129
130 chDbgCheckClassS();
131
132 return ch_queue_notempty(&mp->queue);
133}
134
135/**
136 * @brief Returns the mutex owner thread.
137 *
138 * @param[out] mp pointer to a @p mutex_t structure
139 * @return The owner thread.
140 * @retval NULL if the mutex is not owned.
141 *
142 * @iclass
143 */
144static inline thread_t *chMtxGetOwnerI(mutex_t *mp) {
145
146 chDbgCheckClassI();
147
148 return mp->owner;
149}
150
151/**
152 * @brief Returns the next mutex in the mutexes stack of the current thread.
153 *
154 * @return A pointer to the next mutex in the stack.
155 * @retval NULL if the stack is empty.
156 *
157 * @xclass
158 */
159static inline mutex_t *chMtxGetNextMutexX(void) {
160
161 return chThdGetSelfX()->mtxlist;
162}
163
164#endif /* CH_CFG_USE_MUTEXES == TRUE */
165
166#endif /* CHMTX_H */
167
168/** @} */