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