takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/rtos/Mutex.h@3:f3764f852aa8, 2018-10-11 (annotated)
- Committer:
- kadonotakashi
- Date:
- Thu Oct 11 02:27:46 2018 +0000
- Revision:
- 3:f3764f852aa8
- Parent:
- 0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2006-2012 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
kadonotakashi | 0:8fdf9a60065b | 5 | * of this software and associated documentation files (the "Software"), to deal |
kadonotakashi | 0:8fdf9a60065b | 6 | * in the Software without restriction, including without limitation the rights |
kadonotakashi | 0:8fdf9a60065b | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kadonotakashi | 0:8fdf9a60065b | 8 | * copies of the Software, and to permit persons to whom the Software is |
kadonotakashi | 0:8fdf9a60065b | 9 | * furnished to do so, subject to the following conditions: |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * The above copyright notice and this permission notice shall be included in |
kadonotakashi | 0:8fdf9a60065b | 12 | * all copies or substantial portions of the Software. |
kadonotakashi | 0:8fdf9a60065b | 13 | * |
kadonotakashi | 0:8fdf9a60065b | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kadonotakashi | 0:8fdf9a60065b | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kadonotakashi | 0:8fdf9a60065b | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kadonotakashi | 0:8fdf9a60065b | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kadonotakashi | 0:8fdf9a60065b | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kadonotakashi | 0:8fdf9a60065b | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
kadonotakashi | 0:8fdf9a60065b | 20 | * SOFTWARE. |
kadonotakashi | 0:8fdf9a60065b | 21 | */ |
kadonotakashi | 0:8fdf9a60065b | 22 | #ifndef MUTEX_H |
kadonotakashi | 0:8fdf9a60065b | 23 | #define MUTEX_H |
kadonotakashi | 0:8fdf9a60065b | 24 | |
kadonotakashi | 0:8fdf9a60065b | 25 | #include <stdint.h> |
kadonotakashi | 0:8fdf9a60065b | 26 | #include "cmsis_os2.h" |
kadonotakashi | 0:8fdf9a60065b | 27 | #include "mbed_rtos1_types.h" |
kadonotakashi | 0:8fdf9a60065b | 28 | #include "mbed_rtos_storage.h" |
kadonotakashi | 0:8fdf9a60065b | 29 | |
kadonotakashi | 0:8fdf9a60065b | 30 | #include "platform/NonCopyable.h" |
kadonotakashi | 0:8fdf9a60065b | 31 | #include "platform/ScopedLock.h" |
kadonotakashi | 0:8fdf9a60065b | 32 | #include "platform/mbed_toolchain.h" |
kadonotakashi | 0:8fdf9a60065b | 33 | |
kadonotakashi | 0:8fdf9a60065b | 34 | namespace rtos { |
kadonotakashi | 0:8fdf9a60065b | 35 | /** \addtogroup rtos */ |
kadonotakashi | 0:8fdf9a60065b | 36 | /** @{*/ |
kadonotakashi | 0:8fdf9a60065b | 37 | |
kadonotakashi | 0:8fdf9a60065b | 38 | class Mutex; |
kadonotakashi | 0:8fdf9a60065b | 39 | /** Typedef for the mutex lock |
kadonotakashi | 0:8fdf9a60065b | 40 | * |
kadonotakashi | 0:8fdf9a60065b | 41 | * Usage: |
kadonotakashi | 0:8fdf9a60065b | 42 | * @code |
kadonotakashi | 0:8fdf9a60065b | 43 | * void foo(Mutex &m) { |
kadonotakashi | 0:8fdf9a60065b | 44 | * ScopedMutexLock lock(m); |
kadonotakashi | 0:8fdf9a60065b | 45 | * // Mutex lock protects code in this block |
kadonotakashi | 0:8fdf9a60065b | 46 | * } |
kadonotakashi | 0:8fdf9a60065b | 47 | * @endcode |
kadonotakashi | 0:8fdf9a60065b | 48 | */ |
kadonotakashi | 0:8fdf9a60065b | 49 | typedef mbed::ScopedLock<Mutex> ScopedMutexLock; |
kadonotakashi | 0:8fdf9a60065b | 50 | |
kadonotakashi | 0:8fdf9a60065b | 51 | /** |
kadonotakashi | 0:8fdf9a60065b | 52 | * \defgroup rtos_Mutex Mutex class |
kadonotakashi | 0:8fdf9a60065b | 53 | * @{ |
kadonotakashi | 0:8fdf9a60065b | 54 | */ |
kadonotakashi | 0:8fdf9a60065b | 55 | |
kadonotakashi | 0:8fdf9a60065b | 56 | /** The Mutex class is used to synchronize the execution of threads. |
kadonotakashi | 0:8fdf9a60065b | 57 | This is for example used to protect access to a shared resource. |
kadonotakashi | 0:8fdf9a60065b | 58 | |
kadonotakashi | 0:8fdf9a60065b | 59 | @note You cannot use member functions of this class in ISR context. If you require Mutex functionality within |
kadonotakashi | 0:8fdf9a60065b | 60 | ISR handler, consider using @a Semaphore. |
kadonotakashi | 0:8fdf9a60065b | 61 | |
kadonotakashi | 0:8fdf9a60065b | 62 | @note |
kadonotakashi | 0:8fdf9a60065b | 63 | Memory considerations: The mutex control structures will be created on current thread's stack, both for the Mbed OS |
kadonotakashi | 0:8fdf9a60065b | 64 | and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). |
kadonotakashi | 0:8fdf9a60065b | 65 | */ |
kadonotakashi | 0:8fdf9a60065b | 66 | class Mutex : private mbed::NonCopyable<Mutex> { |
kadonotakashi | 0:8fdf9a60065b | 67 | public: |
kadonotakashi | 0:8fdf9a60065b | 68 | /** Create and Initialize a Mutex object |
kadonotakashi | 0:8fdf9a60065b | 69 | * |
kadonotakashi | 0:8fdf9a60065b | 70 | * @note You cannot call this function from ISR context. |
kadonotakashi | 0:8fdf9a60065b | 71 | */ |
kadonotakashi | 0:8fdf9a60065b | 72 | Mutex(); |
kadonotakashi | 0:8fdf9a60065b | 73 | |
kadonotakashi | 0:8fdf9a60065b | 74 | /** Create and Initialize a Mutex object |
kadonotakashi | 0:8fdf9a60065b | 75 | |
kadonotakashi | 0:8fdf9a60065b | 76 | @param name name to be used for this mutex. It has to stay allocated for the lifetime of the thread. |
kadonotakashi | 0:8fdf9a60065b | 77 | @note You cannot call this function from ISR context. |
kadonotakashi | 0:8fdf9a60065b | 78 | */ |
kadonotakashi | 0:8fdf9a60065b | 79 | Mutex(const char *name); |
kadonotakashi | 0:8fdf9a60065b | 80 | |
kadonotakashi | 0:8fdf9a60065b | 81 | /** |
kadonotakashi | 0:8fdf9a60065b | 82 | Wait until a Mutex becomes available. |
kadonotakashi | 0:8fdf9a60065b | 83 | |
kadonotakashi | 0:8fdf9a60065b | 84 | @return status code that indicates the execution status of the function: |
kadonotakashi | 0:8fdf9a60065b | 85 | @a osOK the mutex has been obtained. |
kadonotakashi | 0:8fdf9a60065b | 86 | |
kadonotakashi | 0:8fdf9a60065b | 87 | @note You cannot call this function from ISR context. |
kadonotakashi | 0:8fdf9a60065b | 88 | @note This function treats RTOS errors as fatal system errors, so can only return osOK. |
kadonotakashi | 0:8fdf9a60065b | 89 | Use of the return value is deprecated, as the return is expected to become void in the future. |
kadonotakashi | 0:8fdf9a60065b | 90 | */ |
kadonotakashi | 0:8fdf9a60065b | 91 | osStatus lock(void); |
kadonotakashi | 0:8fdf9a60065b | 92 | |
kadonotakashi | 0:8fdf9a60065b | 93 | /** |
kadonotakashi | 0:8fdf9a60065b | 94 | For backwards compatibility. |
kadonotakashi | 0:8fdf9a60065b | 95 | @deprecated Do not use this function. This function has been replaced with lock(), trylock() and trylock_for() functions. |
kadonotakashi | 0:8fdf9a60065b | 96 | |
kadonotakashi | 0:8fdf9a60065b | 97 | Wait until a Mutex becomes available. |
kadonotakashi | 0:8fdf9a60065b | 98 | @param millisec timeout value or 0 in case of no time-out. |
kadonotakashi | 0:8fdf9a60065b | 99 | @return status code that indicates the execution status of the function: |
kadonotakashi | 0:8fdf9a60065b | 100 | @a osOK the mutex has been obtained. |
kadonotakashi | 0:8fdf9a60065b | 101 | @a osErrorTimeout the mutex could not be obtained in the given time. |
kadonotakashi | 0:8fdf9a60065b | 102 | @a osErrorResource the mutex could not be obtained when no timeout was specified. |
kadonotakashi | 0:8fdf9a60065b | 103 | |
kadonotakashi | 0:8fdf9a60065b | 104 | @note You cannot call this function from ISR context. |
kadonotakashi | 0:8fdf9a60065b | 105 | @note This function treats RTOS errors as fatal system errors, so can only return osOK or |
kadonotakashi | 0:8fdf9a60065b | 106 | osErrorResource in case when millisec is 0 or osErrorTimeout if millisec is not osWaitForever. |
kadonotakashi | 0:8fdf9a60065b | 107 | */ |
kadonotakashi | 0:8fdf9a60065b | 108 | MBED_DEPRECATED_SINCE("mbed-os-5.10.0", "Replaced with lock(), trylock() and trylock_for() functions") |
kadonotakashi | 0:8fdf9a60065b | 109 | osStatus lock(uint32_t millisec); |
kadonotakashi | 0:8fdf9a60065b | 110 | |
kadonotakashi | 0:8fdf9a60065b | 111 | /** Try to lock the mutex, and return immediately |
kadonotakashi | 0:8fdf9a60065b | 112 | @return true if the mutex was acquired, false otherwise. |
kadonotakashi | 0:8fdf9a60065b | 113 | @note equivalent to trylock_for(0) |
kadonotakashi | 0:8fdf9a60065b | 114 | |
kadonotakashi | 0:8fdf9a60065b | 115 | @note You cannot call this function from ISR context. |
kadonotakashi | 0:8fdf9a60065b | 116 | */ |
kadonotakashi | 0:8fdf9a60065b | 117 | bool trylock(); |
kadonotakashi | 0:8fdf9a60065b | 118 | |
kadonotakashi | 0:8fdf9a60065b | 119 | /** Try to lock the mutex for a specified time |
kadonotakashi | 0:8fdf9a60065b | 120 | @param millisec timeout value or 0 in case of no time-out. |
kadonotakashi | 0:8fdf9a60065b | 121 | @return true if the mutex was acquired, false otherwise. |
kadonotakashi | 0:8fdf9a60065b | 122 | @note the underlying RTOS may have a limit to the maximum wait time |
kadonotakashi | 0:8fdf9a60065b | 123 | due to internal 32-bit computations, but this is guaranteed to work if the |
kadonotakashi | 0:8fdf9a60065b | 124 | wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, |
kadonotakashi | 0:8fdf9a60065b | 125 | the lock attempt will time out earlier than specified. |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | @note You cannot call this function from ISR context. |
kadonotakashi | 0:8fdf9a60065b | 128 | */ |
kadonotakashi | 0:8fdf9a60065b | 129 | bool trylock_for(uint32_t millisec); |
kadonotakashi | 0:8fdf9a60065b | 130 | |
kadonotakashi | 0:8fdf9a60065b | 131 | /** Try to lock the mutex until specified time |
kadonotakashi | 0:8fdf9a60065b | 132 | @param millisec absolute timeout time, referenced to Kernel::get_ms_count() |
kadonotakashi | 0:8fdf9a60065b | 133 | @return true if the mutex was acquired, false otherwise. |
kadonotakashi | 0:8fdf9a60065b | 134 | @note the underlying RTOS may have a limit to the maximum wait time |
kadonotakashi | 0:8fdf9a60065b | 135 | due to internal 32-bit computations, but this is guaranteed to work if the |
kadonotakashi | 0:8fdf9a60065b | 136 | wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, |
kadonotakashi | 0:8fdf9a60065b | 137 | the lock attempt will time out earlier than specified. |
kadonotakashi | 0:8fdf9a60065b | 138 | |
kadonotakashi | 0:8fdf9a60065b | 139 | @note You cannot call this function from ISR context. |
kadonotakashi | 0:8fdf9a60065b | 140 | */ |
kadonotakashi | 0:8fdf9a60065b | 141 | bool trylock_until(uint64_t millisec); |
kadonotakashi | 0:8fdf9a60065b | 142 | |
kadonotakashi | 0:8fdf9a60065b | 143 | /** |
kadonotakashi | 0:8fdf9a60065b | 144 | Unlock the mutex that has previously been locked by the same thread |
kadonotakashi | 0:8fdf9a60065b | 145 | |
kadonotakashi | 0:8fdf9a60065b | 146 | @return status code that indicates the execution status of the function: |
kadonotakashi | 0:8fdf9a60065b | 147 | @a osOK the mutex has been released. |
kadonotakashi | 0:8fdf9a60065b | 148 | |
kadonotakashi | 0:8fdf9a60065b | 149 | @note You cannot call this function from ISR context. |
kadonotakashi | 0:8fdf9a60065b | 150 | @note This function treats RTOS errors as fatal system errors, so can only return osOK. |
kadonotakashi | 0:8fdf9a60065b | 151 | Use of the return value is deprecated, as the return is expected to become void in the future. |
kadonotakashi | 0:8fdf9a60065b | 152 | */ |
kadonotakashi | 0:8fdf9a60065b | 153 | osStatus unlock(); |
kadonotakashi | 0:8fdf9a60065b | 154 | |
kadonotakashi | 0:8fdf9a60065b | 155 | /** Get the owner the this mutex |
kadonotakashi | 0:8fdf9a60065b | 156 | @return the current owner of this mutex. |
kadonotakashi | 0:8fdf9a60065b | 157 | |
kadonotakashi | 0:8fdf9a60065b | 158 | @note You cannot call this function from ISR context. |
kadonotakashi | 0:8fdf9a60065b | 159 | */ |
kadonotakashi | 0:8fdf9a60065b | 160 | osThreadId get_owner(); |
kadonotakashi | 0:8fdf9a60065b | 161 | |
kadonotakashi | 0:8fdf9a60065b | 162 | /** Mutex destructor |
kadonotakashi | 0:8fdf9a60065b | 163 | * |
kadonotakashi | 0:8fdf9a60065b | 164 | * @note You cannot call this function from ISR context. |
kadonotakashi | 0:8fdf9a60065b | 165 | */ |
kadonotakashi | 0:8fdf9a60065b | 166 | ~Mutex(); |
kadonotakashi | 0:8fdf9a60065b | 167 | |
kadonotakashi | 0:8fdf9a60065b | 168 | private: |
kadonotakashi | 0:8fdf9a60065b | 169 | void constructor(const char *name = NULL); |
kadonotakashi | 0:8fdf9a60065b | 170 | friend class ConditionVariable; |
kadonotakashi | 0:8fdf9a60065b | 171 | |
kadonotakashi | 0:8fdf9a60065b | 172 | osMutexId_t _id; |
kadonotakashi | 0:8fdf9a60065b | 173 | mbed_rtos_storage_mutex_t _obj_mem; |
kadonotakashi | 0:8fdf9a60065b | 174 | uint32_t _count; |
kadonotakashi | 0:8fdf9a60065b | 175 | }; |
kadonotakashi | 0:8fdf9a60065b | 176 | /** @}*/ |
kadonotakashi | 0:8fdf9a60065b | 177 | /** @}*/ |
kadonotakashi | 0:8fdf9a60065b | 178 | } |
kadonotakashi | 0:8fdf9a60065b | 179 | #endif |
kadonotakashi | 0:8fdf9a60065b | 180 | |
kadonotakashi | 0:8fdf9a60065b | 181 |