Mistake on this page?
Report an issue in GitHub or email us
Mutex.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2012 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 "cmsis_os2.h"
27 #include "mbed_rtos1_types.h"
28 #include "mbed_rtos_storage.h"
29 
30 #include "platform/NonCopyable.h"
31 #include "platform/ScopedLock.h"
32 
33 namespace rtos {
34 /** \addtogroup rtos */
35 /** @{*/
36 
37 class Mutex;
38 /** Typedef for the mutex lock
39  *
40  * Usage:
41  * @code
42  * void foo(Mutex &m) {
43  * ScopedMutexLock lock(m);
44  * // Mutex lock protects code in this block
45  * }
46  * @endcode
47  */
49 
50 /**
51  * \defgroup rtos_Mutex Mutex class
52  * @{
53  */
54 
55 /** The Mutex class is used to synchronize the execution of threads.
56  This is for example used to protect access to a shared resource.
57 
58  @note You cannot use member functions of this class in ISR context. If you require Mutex functionality within
59  ISR handler, consider using @a Semaphore.
60 
61  @note
62  Memory considerations: The mutex control structures will be created on current thread's stack, both for the Mbed OS
63  and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
64 */
65 class Mutex : private mbed::NonCopyable<Mutex> {
66 public:
67  /** Create and Initialize a Mutex object
68  *
69  * @note You cannot call this function from ISR context.
70  */
71  Mutex();
72 
73  /** Create and Initialize a Mutex object
74 
75  @param name name to be used for this mutex. It has to stay allocated for the lifetime of the thread.
76 
77  @note You cannot call this function from ISR context.
78  */
79  Mutex(const char *name);
80 
81  /** Wait until a Mutex becomes available.
82  @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever)
83  @return status code that indicates the execution status of the function:
84  @a osOK the mutex has been obtained.
85  @a osErrorTimeout the mutex could not be obtained in the given time.
86  @a osErrorParameter internal error.
87  @a osErrorResource the mutex could not be obtained when no timeout was specified.
88  @a osErrorISR this function cannot be called from the interrupt service routine.
89 
90  @note You cannot call this function from ISR context.
91  */
92  osStatus lock(uint32_t millisec=osWaitForever);
93 
94  /** Try to lock the mutex, and return immediately
95  @return true if the mutex was acquired, false otherwise.
96  @note equivalent to trylock_for(0)
97 
98  @note You cannot call this function from ISR context.
99  */
100  bool trylock();
101 
102  /** Try to lock the mutex for a specified time
103  @param millisec timeout value or 0 in case of no time-out.
104  @return true if the mutex was acquired, false otherwise.
105  @note the underlying RTOS may have a limit to the maximum wait time
106  due to internal 32-bit computations, but this is guaranteed to work if the
107  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
108  the lock attempt will time out earlier than specified.
109 
110  @note You cannot call this function from ISR context.
111  */
112  bool trylock_for(uint32_t millisec);
113 
114  /** Try to lock the mutex until specified time
115  @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
116  @return true if the mutex was acquired, false otherwise.
117  @note the underlying RTOS may have a limit to the maximum wait time
118  due to internal 32-bit computations, but this is guaranteed to work if the
119  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
120  the lock attempt will time out earlier than specified.
121 
122  @note You cannot call this function from ISR context.
123  */
124  bool trylock_until(uint64_t millisec);
125 
126  /** Unlock the mutex that has previously been locked by the same thread
127  @return status code that indicates the execution status of the function:
128  @a osOK the mutex has been released.
129  @a osErrorParameter internal error.
130  @a osErrorResource the mutex was not locked or the current thread wasn't the owner.
131  @a osErrorISR this function cannot be called from the interrupt service routine.
132 
133  @note You cannot call this function from ISR context.
134  */
135  osStatus unlock();
136 
137  /** Get the owner the this mutex
138  @return the current owner of this mutex.
139 
140  @note You cannot call this function from ISR context.
141  */
142  osThreadId get_owner();
143 
144  /** Mutex destructor
145  *
146  * @note You cannot call this function from ISR context.
147  */
148  ~Mutex();
149 
150 private:
151  void constructor(const char *name = NULL);
152  friend class ConditionVariable;
153 
154  osMutexId_t _id;
155  mbed_rtos_storage_mutex_t _obj_mem;
156  uint32_t _count;
157 };
158 /** @}*/
159 /** @}*/
160 }
161 #endif
162 
163 
osStatus unlock()
Definition: Mutex.cpp:89
osStatus lock(uint32_t millisec=osWaitForever)
Definition: Mutex.cpp:53
Definition: ScopedLock.h:61
~Mutex()
Definition: Mutex.cpp:98
Definition: NonCopyable.h:150
osThreadId get_owner()
Definition: Mutex.cpp:94
mbed::ScopedLock< Mutex > ScopedMutexLock
Definition: Mutex.h:37
bool trylock_for(uint32_t millisec)
Definition: Mutex.cpp:65
bool trylock_until(uint64_t millisec)
Definition: Mutex.cpp:76
Definition: Mutex.h:65
os_mutex_t mbed_rtos_storage_mutex_t
RTOS primitives storage types for RTX.
Definition: mbed_rtos_storage.h:46
Definition: ConditionVariable.h:119
Mutex()
Definition: Mutex.cpp:31
Definition: Kernel.cpp:19
bool trylock()
Definition: Mutex.cpp:61
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.