ChibiOS/RT  5.1.0
chmtx.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 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; either version 3 of the License, or
9  (at your option) any later version.
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 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  threads_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) {_THREADS_QUEUE_DATA(name.queue), NULL, NULL, 0}
82 #else
83 #define _MUTEX_DATA(name) {_THREADS_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  * @deprecated
127  * @sclass
128  */
129 static inline bool chMtxQueueNotEmptyS(mutex_t *mp) {
130 
132 
133  return queue_notempty(&mp->queue);
134 }
135 
136 /**
137  * @brief Returns the next mutex in the mutexes stack of the current thread.
138  *
139  * @return A pointer to the next mutex in the stack.
140  * @retval NULL if the stack is empty.
141  *
142  * @sclass
143  */
144 static inline mutex_t *chMtxGetNextMutexS(void) {
145 
146  return chThdGetSelfX()->mtxlist;
147 }
148 
149 #endif /* CH_CFG_USE_MUTEXES == TRUE */
150 
151 #endif /* CHMTX_H */
152 
153 /** @} */
bool chMtxTryLock(mutex_t *mp)
Tries to lock a mutex.
Definition: chmtx.c:255
void chDbgCheckClassS(void)
S-class functions context check.
Definition: chdebug.c:250
threads_queue_t queue
Queue of the threads sleeping on this mutex.
Definition: chmtx.h:58
void chMtxUnlockS(mutex_t *mp)
Unlocks the specified mutex.
Definition: chmtx.c:410
struct ch_mutex * mtxlist
List of the mutexes owned by this thread.
Definition: chschd.h:294
Generic threads bidirectional linked list header and element.
Definition: chschd.h:142
void chMtxUnlockAllS(void)
Unlocks all mutexes owned by the invoking thread.
Definition: chmtx.c:487
Mutex structure.
Definition: chmtx.h:57
thread_t * owner
Owner thread_t pointer or NULL.
Definition: chmtx.h:60
static mutex_t * chMtxGetNextMutexS(void)
Returns the next mutex in the mutexes stack of the current thread.
Definition: chmtx.h:144
mutex_t * next
Next mutex_t into an owner-list or NULL.
Definition: chmtx.h:62
void chMtxLock(mutex_t *mp)
Locks the specified mutex.
Definition: chmtx.c:123
bool chMtxTryLockS(mutex_t *mp)
Tries to lock a mutex.
Definition: chmtx.c:282
static bool chMtxQueueNotEmptyS(mutex_t *mp)
Returns true if the mutex queue contains at least a waiting thread.
Definition: chmtx.h:129
static thread_t * chThdGetSelfX(void)
Returns a pointer to the current thread_t.
Definition: chthreads.h:272
static bool queue_notempty(const threads_queue_t *tqp)
Evaluates to true if the specified threads queue is not empty.
Definition: chschd.h:575
void chMtxObjectInit(mutex_t *mp)
Initializes s mutex_t structure.
Definition: chmtx.c:103
cnt_t cnt
Mutex recursion counter.
Definition: chmtx.h:65
void chMtxUnlock(mutex_t *mp)
Unlocks the specified mutex.
Definition: chmtx.c:323
void chMtxUnlockAll(void)
Unlocks all mutexes owned by the invoking thread.
Definition: chmtx.c:524
void chMtxLockS(mutex_t *mp)
Locks the specified mutex.
Definition: chmtx.c:139
Structure representing a thread.
Definition: chschd.h:153