mbed-os
Dependents: cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more
rtos/Mutex.h@0:b74591d5ab33, 2017-12-11 (annotated)
- Committer:
- be_bryan
- Date:
- Mon Dec 11 17:54:04 2017 +0000
- Revision:
- 0:b74591d5ab33
motor ++
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
be_bryan | 0:b74591d5ab33 | 1 | /* mbed Microcontroller Library |
be_bryan | 0:b74591d5ab33 | 2 | * Copyright (c) 2006-2012 ARM Limited |
be_bryan | 0:b74591d5ab33 | 3 | * |
be_bryan | 0:b74591d5ab33 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
be_bryan | 0:b74591d5ab33 | 5 | * of this software and associated documentation files (the "Software"), to deal |
be_bryan | 0:b74591d5ab33 | 6 | * in the Software without restriction, including without limitation the rights |
be_bryan | 0:b74591d5ab33 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
be_bryan | 0:b74591d5ab33 | 8 | * copies of the Software, and to permit persons to whom the Software is |
be_bryan | 0:b74591d5ab33 | 9 | * furnished to do so, subject to the following conditions: |
be_bryan | 0:b74591d5ab33 | 10 | * |
be_bryan | 0:b74591d5ab33 | 11 | * The above copyright notice and this permission notice shall be included in |
be_bryan | 0:b74591d5ab33 | 12 | * all copies or substantial portions of the Software. |
be_bryan | 0:b74591d5ab33 | 13 | * |
be_bryan | 0:b74591d5ab33 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
be_bryan | 0:b74591d5ab33 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
be_bryan | 0:b74591d5ab33 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
be_bryan | 0:b74591d5ab33 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
be_bryan | 0:b74591d5ab33 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
be_bryan | 0:b74591d5ab33 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
be_bryan | 0:b74591d5ab33 | 20 | * SOFTWARE. |
be_bryan | 0:b74591d5ab33 | 21 | */ |
be_bryan | 0:b74591d5ab33 | 22 | #ifndef MUTEX_H |
be_bryan | 0:b74591d5ab33 | 23 | #define MUTEX_H |
be_bryan | 0:b74591d5ab33 | 24 | |
be_bryan | 0:b74591d5ab33 | 25 | #include <stdint.h> |
be_bryan | 0:b74591d5ab33 | 26 | #include "cmsis_os2.h" |
be_bryan | 0:b74591d5ab33 | 27 | #include "mbed_rtos1_types.h" |
be_bryan | 0:b74591d5ab33 | 28 | #include "mbed_rtos_storage.h" |
be_bryan | 0:b74591d5ab33 | 29 | |
be_bryan | 0:b74591d5ab33 | 30 | #include "platform/NonCopyable.h" |
be_bryan | 0:b74591d5ab33 | 31 | |
be_bryan | 0:b74591d5ab33 | 32 | namespace rtos { |
be_bryan | 0:b74591d5ab33 | 33 | /** \addtogroup rtos */ |
be_bryan | 0:b74591d5ab33 | 34 | /** @{*/ |
be_bryan | 0:b74591d5ab33 | 35 | /** |
be_bryan | 0:b74591d5ab33 | 36 | * \defgroup rtos_Mutex Mutex class |
be_bryan | 0:b74591d5ab33 | 37 | * @{ |
be_bryan | 0:b74591d5ab33 | 38 | */ |
be_bryan | 0:b74591d5ab33 | 39 | |
be_bryan | 0:b74591d5ab33 | 40 | /** The Mutex class is used to synchronize the execution of threads. |
be_bryan | 0:b74591d5ab33 | 41 | This is for example used to protect access to a shared resource. |
be_bryan | 0:b74591d5ab33 | 42 | |
be_bryan | 0:b74591d5ab33 | 43 | @note |
be_bryan | 0:b74591d5ab33 | 44 | Memory considerations: The mutex control structures will be created on current thread's stack, both for the mbed OS |
be_bryan | 0:b74591d5ab33 | 45 | and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). |
be_bryan | 0:b74591d5ab33 | 46 | */ |
be_bryan | 0:b74591d5ab33 | 47 | class Mutex : private mbed::NonCopyable<Mutex> { |
be_bryan | 0:b74591d5ab33 | 48 | public: |
be_bryan | 0:b74591d5ab33 | 49 | /** Create and Initialize a Mutex object */ |
be_bryan | 0:b74591d5ab33 | 50 | Mutex(); |
be_bryan | 0:b74591d5ab33 | 51 | |
be_bryan | 0:b74591d5ab33 | 52 | /** Create and Initialize a Mutex object |
be_bryan | 0:b74591d5ab33 | 53 | |
be_bryan | 0:b74591d5ab33 | 54 | @param name name to be used for this mutex. It has to stay allocated for the lifetime of the thread. |
be_bryan | 0:b74591d5ab33 | 55 | */ |
be_bryan | 0:b74591d5ab33 | 56 | Mutex(const char *name); |
be_bryan | 0:b74591d5ab33 | 57 | |
be_bryan | 0:b74591d5ab33 | 58 | /** Wait until a Mutex becomes available. |
be_bryan | 0:b74591d5ab33 | 59 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever) |
be_bryan | 0:b74591d5ab33 | 60 | @return status code that indicates the execution status of the function: |
be_bryan | 0:b74591d5ab33 | 61 | @a osOK the mutex has been obtained. |
be_bryan | 0:b74591d5ab33 | 62 | @a osErrorTimeout the mutex could not be obtained in the given time. |
be_bryan | 0:b74591d5ab33 | 63 | @a osErrorParameter internal error. |
be_bryan | 0:b74591d5ab33 | 64 | @a osErrorResource the mutex could not be obtained when no timeout was specified. |
be_bryan | 0:b74591d5ab33 | 65 | @a osErrorISR this function cannot be called from the interrupt service routine. |
be_bryan | 0:b74591d5ab33 | 66 | */ |
be_bryan | 0:b74591d5ab33 | 67 | osStatus lock(uint32_t millisec=osWaitForever); |
be_bryan | 0:b74591d5ab33 | 68 | |
be_bryan | 0:b74591d5ab33 | 69 | /** Try to lock the mutex, and return immediately |
be_bryan | 0:b74591d5ab33 | 70 | @return true if the mutex was acquired, false otherwise. |
be_bryan | 0:b74591d5ab33 | 71 | */ |
be_bryan | 0:b74591d5ab33 | 72 | bool trylock(); |
be_bryan | 0:b74591d5ab33 | 73 | |
be_bryan | 0:b74591d5ab33 | 74 | /** Unlock the mutex that has previously been locked by the same thread |
be_bryan | 0:b74591d5ab33 | 75 | @return status code that indicates the execution status of the function: |
be_bryan | 0:b74591d5ab33 | 76 | @a osOK the mutex has been released. |
be_bryan | 0:b74591d5ab33 | 77 | @a osErrorParameter internal error. |
be_bryan | 0:b74591d5ab33 | 78 | @a osErrorResource the mutex was not locked or the current thread wasn't the owner. |
be_bryan | 0:b74591d5ab33 | 79 | @a osErrorISR this function cannot be called from the interrupt service routine. |
be_bryan | 0:b74591d5ab33 | 80 | */ |
be_bryan | 0:b74591d5ab33 | 81 | osStatus unlock(); |
be_bryan | 0:b74591d5ab33 | 82 | |
be_bryan | 0:b74591d5ab33 | 83 | /** Get the owner the this mutex |
be_bryan | 0:b74591d5ab33 | 84 | @return the current owner of this mutex. |
be_bryan | 0:b74591d5ab33 | 85 | */ |
be_bryan | 0:b74591d5ab33 | 86 | osThreadId get_owner(); |
be_bryan | 0:b74591d5ab33 | 87 | |
be_bryan | 0:b74591d5ab33 | 88 | ~Mutex(); |
be_bryan | 0:b74591d5ab33 | 89 | |
be_bryan | 0:b74591d5ab33 | 90 | private: |
be_bryan | 0:b74591d5ab33 | 91 | void constructor(const char *name = NULL); |
be_bryan | 0:b74591d5ab33 | 92 | friend class ConditionVariable; |
be_bryan | 0:b74591d5ab33 | 93 | |
be_bryan | 0:b74591d5ab33 | 94 | osMutexId_t _id; |
be_bryan | 0:b74591d5ab33 | 95 | mbed_rtos_storage_mutex_t _obj_mem; |
be_bryan | 0:b74591d5ab33 | 96 | uint32_t _count; |
be_bryan | 0:b74591d5ab33 | 97 | }; |
be_bryan | 0:b74591d5ab33 | 98 | /** @}*/ |
be_bryan | 0:b74591d5ab33 | 99 | /** @}*/ |
be_bryan | 0:b74591d5ab33 | 100 | } |
be_bryan | 0:b74591d5ab33 | 101 | #endif |
be_bryan | 0:b74591d5ab33 | 102 | |
be_bryan | 0:b74591d5ab33 | 103 |