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