Mistake on this page?
Report an issue in GitHub or email us
Mutex.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef MUTEX_H
23 #define MUTEX_H
24 
25 #include <stdint.h>
26 #include "rtos/mbed_rtos_types.h"
27 #include "rtos/mbed_rtos1_types.h"
28 #include "rtos/mbed_rtos_storage.h"
29 
30 #include "platform/NonCopyable.h"
31 #include "platform/ScopedLock.h"
32 #include "platform/mbed_toolchain.h"
33 
34 namespace rtos {
35 /** \addtogroup rtos-public-api */
36 /** @{*/
37 
38 class Mutex;
39 /** Typedef for the mutex lock
40  *
41  * Usage:
42  * @code
43  * void foo(Mutex &m) {
44  * ScopedMutexLock lock(m);
45  * // Mutex lock protects code in this block
46  * }
47  * @endcode
48  */
50 
51 /**
52  * \defgroup rtos_Mutex Mutex class
53  * @{
54  */
55 
56 /** The Mutex class is used to synchronize the execution of threads.
57  This is, for example, used to protect access to a shared resource.
58 
59  In bare-metal builds, the Mutex class is a dummy, so lock() and unlock() are no-ops.
60 
61  @note You cannot use member functions of this class in ISR context. If you require Mutex functionality within
62  ISR handler, consider using @a Semaphore.
63 
64  @note
65  Memory considerations: The mutex control structures are created on the current thread's stack, both for the Mbed OS
66  and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
67 */
68 class Mutex : private mbed::NonCopyable<Mutex> {
69 public:
70  /** Create and Initialize a Mutex object
71  *
72  * @note You cannot call this function from ISR context.
73  */
74  Mutex();
75 
76  /** Create and Initialize a Mutex object
77 
78  @param name name to be used for this mutex. It has to stay allocated for the lifetime of the thread.
79  @note You cannot call this function from ISR context.
80  */
81  Mutex(const char *name);
82 
83  /**
84  Wait until a Mutex becomes available.
85 
86  @return status code that indicates the execution status of the function:
87  @a osOK the mutex has been obtained.
88 
89  @note You cannot call this function from ISR context.
90  @note This function treats RTOS errors as fatal system errors, so it can only return osOK.
91  Use of the return value is deprecated, as the return is expected to become void in the future.
92  */
93 #if MBED_CONF_RTOS_PRESENT
94  osStatus lock();
95 #else
96  void lock(); // Value return backwards compatibility not required for non-RTOS
97 #endif
98 
99  /**
100  Wait until a Mutex becomes available.
101 
102  @deprecated Do not use this function. This function has been replaced with lock(), trylock() and trylock_for() functions.
103 
104  @param millisec timeout value.
105  @return status code that indicates the execution status of the function:
106  @a osOK the mutex has been obtained.
107  @a osErrorTimeout the mutex could not be obtained in the given time.
108  @a osErrorResource the mutex could not be obtained when no timeout was specified.
109 
110  @note You cannot call this function from ISR context.
111  @note This function treats RTOS errors as fatal system errors, so it can only return osOK or
112  osErrorResource in case when millisec is 0 or osErrorTimeout if millisec is not osWaitForever.
113  */
114  MBED_DEPRECATED_SINCE("mbed-os-5.10.0", "Replaced with lock(), trylock() and trylock_for() functions")
115  osStatus lock(uint32_t millisec);
116 
117  /** Try to lock the mutex, and return immediately
118  @return true if the mutex was acquired, false otherwise.
119  @note equivalent to trylock_for(0)
120 
121  @note You cannot call this function from ISR context.
122  */
123  bool trylock();
124 
125  /** Try to lock the mutex for a specified time
126  @param millisec timeout value.
127  @return true if the mutex was acquired, false otherwise.
128  @note the underlying RTOS may have a limit to the maximum wait time
129  due to internal 32-bit computations, but this is guaranteed to work if the
130  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
131  the lock attempt will time out earlier than specified.
132 
133  @note You cannot call this function from ISR context.
134  */
135  bool trylock_for(uint32_t millisec);
136 
137  /** Try to lock the mutex until specified time
138  @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
139  @return true if the mutex was acquired, false otherwise.
140  @note the underlying RTOS may have a limit to the maximum wait time
141  due to internal 32-bit computations, but this is guaranteed to work if the
142  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
143  the lock attempt will time out earlier than specified.
144 
145  @note You cannot call this function from ISR context.
146  */
147  bool trylock_until(uint64_t millisec);
148 
149  /**
150  Unlock the mutex that has previously been locked by the same thread
151 
152  @return status code that indicates the execution status of the function:
153  @a osOK the mutex has been released.
154 
155  @note You cannot call this function from ISR context.
156  @note This function treats RTOS errors as fatal system errors, so it can only return osOK.
157  Use of the return value is deprecated, as the return is expected to become void in the future.
158  */
159 #if MBED_CONF_RTOS_PRESENT
160  osStatus unlock();
161 #else
162  void unlock(); // Value return backwards compatibility not required for non-RTOS
163 #endif
164 
165  /** Get the owner the this mutex
166  @return the current owner of this mutex.
167 
168  @note You cannot call this function from ISR context.
169  */
170  osThreadId_t get_owner();
171 
172  /** Mutex destructor
173  *
174  * @note You cannot call this function from ISR context.
175  */
176  ~Mutex();
177 
178 private:
179 #if MBED_CONF_RTOS_PRESENT
180  void constructor(const char *name = nullptr);
181  friend class ConditionVariable;
182 
183  osMutexId_t _id;
184  mbed_rtos_storage_mutex_t _obj_mem;
185  uint32_t _count;
186 #endif
187 };
188 
189 #if !MBED_CONF_RTOS_PRESENT
190 inline Mutex::Mutex()
191 {
192 }
193 
194 inline Mutex::Mutex(const char *)
195 {
196 }
197 
199 {
200 }
201 
202 inline void Mutex::lock()
203 {
204 }
205 
206 inline bool Mutex::trylock()
207 {
208  return true;
209 }
210 
211 inline bool Mutex::trylock_for(uint32_t)
212 {
213  return true;
214 }
215 
216 inline bool Mutex::trylock_until(uint64_t)
217 {
218  return true;
219 }
220 
221 inline void Mutex::unlock()
222 {
223 }
224 #endif
225 
226 /** @}*/
227 /** @}*/
228 }
229 #endif
RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block...
Definition: ScopedLock.h:63
bool trylock()
Try to lock the mutex, and return immediately.
Definition: Mutex.h:206
~Mutex()
Mutex destructor.
Definition: Mutex.h:198
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:169
Mutex()
Create and Initialize a Mutex object.
Definition: Mutex.h:190
void unlock()
Unlock the mutex that has previously been locked by the same thread.
Definition: Mutex.h:221
osThreadId_t get_owner()
Get the owner the this mutex.
bool trylock_for(uint32_t millisec)
Try to lock the mutex for a specified time.
Definition: Mutex.h:211
bool trylock_until(uint64_t millisec)
Try to lock the mutex until specified time.
Definition: Mutex.h:216
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:68
Mutex Control Block.
Definition: rtx_os.h:180
The ConditionVariable class is a synchronization primitive that allows threads to wait until a partic...
void lock()
Wait until a Mutex becomes available.
Definition: Mutex.h:202
mbed::ScopedLock< Mutex > ScopedMutexLock
Typedef for the mutex lock.
Definition: Mutex.h:38
Definition: TaskBase.h:25
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.