ChibiOS  21.6.0
chmtx.h
Go to the documentation of this file.
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  */
52 typedef struct ch_mutex mutex_t;
53 
54 /**
55  * @brief Mutex structure.
56  */
57 struct 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
100 extern "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  */
128 static 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  */
144 static 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  */
159 static 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 /** @} */
ch_mutex::cnt
cnt_t cnt
Mutex recursion counter.
Definition: chmtx.h:65
chMtxGetOwnerI
static thread_t * chMtxGetOwnerI(mutex_t *mp)
Returns the mutex owner thread.
Definition: chmtx.h:144
ch_queue_notempty
static bool ch_queue_notempty(const ch_queue_t *qp)
Evaluates to true if the specified queue is not empty.
Definition: chlists.h:249
chMtxLock
void chMtxLock(mutex_t *mp)
Locks the specified mutex.
Definition: chmtx.c:123
chMtxUnlockAll
void chMtxUnlockAll(void)
Unlocks all mutexes owned by the invoking thread.
Definition: chmtx.c:531
chMtxTryLock
bool chMtxTryLock(mutex_t *mp)
Tries to lock a mutex.
Definition: chmtx.c:257
chMtxTryLockS
bool chMtxTryLockS(mutex_t *mp)
Tries to lock a mutex.
Definition: chmtx.c:284
ch_thread
Structure representing a thread.
Definition: chobjects.h:156
cnt_t
int32_t cnt_t
Definition: chearly.h:92
chMtxGetNextMutexX
static mutex_t * chMtxGetNextMutexX(void)
Returns the next mutex in the mutexes stack of the current thread.
Definition: chmtx.h:159
chMtxObjectInit
void chMtxObjectInit(mutex_t *mp)
Initializes s mutex_t structure.
Definition: chmtx.c:103
ch_queue
Structure representing a generic bidirectional linked list header and element.
Definition: chlists.h:69
ch_mutex::owner
thread_t * owner
Owner thread_t pointer or NULL.
Definition: chmtx.h:60
chMtxUnlockAllS
void chMtxUnlockAllS(void)
Unlocks all mutexes owned by the invoking thread.
Definition: chmtx.c:490
chMtxUnlock
void chMtxUnlock(mutex_t *mp)
Unlocks the specified mutex.
Definition: chmtx.c:326
ch_mutex::next
mutex_t * next
Next mutex_t into an owner-list or NULL.
Definition: chmtx.h:62
ch_mutex
Mutex structure.
Definition: chmtx.h:57
chMtxUnlockS
void chMtxUnlockS(mutex_t *mp)
Unlocks the specified mutex.
Definition: chmtx.c:413
chMtxLockS
void chMtxLockS(mutex_t *mp)
Locks the specified mutex.
Definition: chmtx.c:139
ch_mutex::queue
ch_queue_t queue
Queue of the threads sleeping on this mutex.
Definition: chmtx.h:58
chMtxQueueNotEmptyS
static bool chMtxQueueNotEmptyS(mutex_t *mp)
Returns true if the mutex queue contains at least a waiting thread.
Definition: chmtx.h:128
chThdGetSelfX
#define chThdGetSelfX()
Returns a pointer to the current thread_t.
Definition: nil/include/ch.h:1132