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