1

Committer:
valeyev
Date:
Tue Mar 13 07:17:50 2018 +0000
Revision:
0:e056ac8fecf8
looking for...

Who changed what in which revision?

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