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