Mbed OS Reference
Loading...
Searching...
No Matches
Mutex.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2019 ARM Limited
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23#ifndef MUTEX_H
24#define MUTEX_H
25
26#include <stdint.h>
27#include "rtos/mbed_rtos_types.h"
28#include "rtos/internal/mbed_rtos1_types.h"
29#include "rtos/internal/mbed_rtos_storage.h"
30#include "rtos/Kernel.h"
31
32#include "platform/NonCopyable.h"
33#include "platform/ScopedLock.h"
34#include "platform/mbed_toolchain.h"
35
36namespace rtos {
37/** \addtogroup rtos-public-api */
38/** @{*/
39
40class Mutex;
41/** Typedef for the mutex lock
42 *
43 * Usage:
44 * @code
45 * void foo(Mutex &m) {
46 * ScopedMutexLock lock(m);
47 * // Mutex lock protects code in this block
48 * }
49 * @endcode
50 */
52
53/**
54 * \defgroup rtos_Mutex Mutex class
55 * @{
56 */
57
58/**
59 * @brief The Mutex class is used to synchronize the execution of threads.
60 *
61 * \par
62 * This is, for example, used to protect access to a shared resource.
63 *
64 * \par
65 * In bare-metal builds, the Mutex class is a dummy, so lock() and unlock() are no-ops.
66 *
67 * \par
68 * Mbed Mutexes are recursive. So, if you call the \c lock() function multiple times,
69 * you must call \c unlock() the same number of times to unlock the mutex. This means that it's okay to lock
70 * a mutex, then call another function that also locks and unlocks the mutex, and the mutex won't actually
71 * get unlocked until your function unlocks it.
72 *
73 * @note You cannot use member functions of this class in ISR context. If you require Mutex functionality within ISR handler, consider using @a Semaphore.
74 *
75 * @note
76 * Memory considerations: The mutex control structures are created on the current thread's stack, both for the Mbed OS
77 * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
78*/
79class Mutex : private mbed::NonCopyable<Mutex> {
80public:
81 /** Create and Initialize a Mutex object
82 *
83 * @note You cannot call this function from ISR context.
84 */
86
87 /** Create and Initialize a Mutex object
88
89 @param name name to be used for this mutex. It has to stay allocated for the lifetime of the thread.
90 @note You cannot call this function from ISR context.
91 */
92 Mutex(const char *name);
93
94 /**
95 Wait until a Mutex becomes available.
96
97 @note You cannot call this function from ISR context.
98 */
99 void lock();
100
101 /** Try to lock the mutex, and return immediately
102 @return true if the mutex was acquired, false otherwise.
103 @note equivalent to trylock_for(0)
104
105 @note You cannot call this function from ISR context.
106 */
107 bool trylock();
108
109 /** Try to lock the mutex for a specified time
110 @param millisec timeout value.
111 @return true if the mutex was acquired, false otherwise.
112 @note the underlying RTOS may have a limit to the maximum wait time
113 due to internal 32-bit computations, but this is guaranteed to work if the
114 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
115 the lock attempt will time out earlier than specified.
116
117 @note You cannot call this function from ISR context.
118 @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
119 */
120 MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
121 bool trylock_for(uint32_t millisec);
122
123 /** Try to lock the mutex for a specified time
124 @param rel_time timeout value.
125 @return true if the mutex was acquired, false otherwise.
126 @note the underlying RTOS may have a limit to the maximum wait time
127 due to internal 32-bit computations, but this is guaranteed to work if the
128 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
129 the lock attempt will time out earlier than specified.
130
131 @note You cannot call this function from ISR context.
132 */
133 bool trylock_for(Kernel::Clock::duration_u32 rel_time);
134
135 /** Try to lock the mutex until specified time
136 @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
137 @return true if the mutex was acquired, false otherwise.
138 @note the underlying RTOS may have a limit to the maximum wait time
139 due to internal 32-bit computations, but this is guaranteed to work if the
140 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
141 the lock attempt will time out earlier than specified.
142
143 @note You cannot call this function from ISR context.
144 @deprecated Pass a chrono time_point, not an integer millisecond count. For example use
145 `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.
146 */
147 MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.")
148 bool trylock_until(uint64_t millisec);
149
150 /** Try to lock the mutex until specified time
151 @param abs_time absolute timeout time, referenced to Kernel::Clock
152 @return true if the mutex was acquired, false otherwise.
153 @note the underlying RTOS may have a limit to the maximum wait time
154 due to internal 32-bit computations, but this is guaranteed to work if the
155 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
156 the lock attempt will time out earlier than specified.
157
158 @note You cannot call this function from ISR context.
159 */
160 bool trylock_until(Kernel::Clock::time_point abs_time);
161
162 /**
163 Unlock the mutex that has previously been locked by the same thread
164
165 @note You cannot call this function from ISR context.
166 */
167 void unlock();
168
169 /** Get the owner the this mutex
170 @return the current owner of this mutex.
171
172 @note You cannot call this function from ISR context.
173 */
174 osThreadId_t get_owner();
175
176 /** Mutex destructor
177 *
178 * @note You cannot call this function from ISR context.
179 */
181
182private:
183 void constructor(const char *name = nullptr);
184 friend class ConditionVariable;
185
186 osMutexId_t _id;
187 mbed_rtos_storage_mutex_t _obj_mem;
188 uint32_t _count;
189};
190
191/** @}*/
192/** @}*/
193}
194#endif
Prevents generation of copy constructor and copy assignment operator in derived classes.
RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block.
Definition ScopedLock.h:63
The ConditionVariable class is a synchronization primitive that allows threads to wait until a partic...
The Mutex class is used to synchronize the execution of threads.
Definition Mutex.h:79
bool trylock_until(uint64_t millisec)
Try to lock the mutex until specified time.
Mutex()
Create and Initialize a Mutex object.
bool trylock_for(uint32_t millisec)
Try to lock the mutex for a specified time.
Mutex(const char *name)
Create and Initialize a Mutex object.
bool trylock()
Try to lock the mutex, and return immediately.
void unlock()
Unlock the mutex that has previously been locked by the same thread.
osThreadId_t get_owner()
Get the owner the this mutex.
void lock()
Wait until a Mutex becomes available.
uint64_t get_ms_count(void)
Generic thread functions.
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
mbed::ScopedLock< Mutex > ScopedMutexLock
Typedef for the mutex lock.
Definition Mutex.h:51