mbed-os
Fork of mbed-os by
rtos/Mutex.h@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2006-2012 ARM Limited |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
elessair | 0:f269e3021894 | 5 | * of this software and associated documentation files (the "Software"), to deal |
elessair | 0:f269e3021894 | 6 | * in the Software without restriction, including without limitation the rights |
elessair | 0:f269e3021894 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
elessair | 0:f269e3021894 | 8 | * copies of the Software, and to permit persons to whom the Software is |
elessair | 0:f269e3021894 | 9 | * furnished to do so, subject to the following conditions: |
elessair | 0:f269e3021894 | 10 | * |
elessair | 0:f269e3021894 | 11 | * The above copyright notice and this permission notice shall be included in |
elessair | 0:f269e3021894 | 12 | * all copies or substantial portions of the Software. |
elessair | 0:f269e3021894 | 13 | * |
elessair | 0:f269e3021894 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
elessair | 0:f269e3021894 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
elessair | 0:f269e3021894 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
elessair | 0:f269e3021894 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
elessair | 0:f269e3021894 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
elessair | 0:f269e3021894 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
elessair | 0:f269e3021894 | 20 | * SOFTWARE. |
elessair | 0:f269e3021894 | 21 | */ |
elessair | 0:f269e3021894 | 22 | #ifndef MUTEX_H |
elessair | 0:f269e3021894 | 23 | #define MUTEX_H |
elessair | 0:f269e3021894 | 24 | |
elessair | 0:f269e3021894 | 25 | #include <stdint.h> |
elessair | 0:f269e3021894 | 26 | #include "cmsis_os.h" |
elessair | 0:f269e3021894 | 27 | |
elessair | 0:f269e3021894 | 28 | namespace rtos { |
elessair | 0:f269e3021894 | 29 | /** \addtogroup rtos */ |
elessair | 0:f269e3021894 | 30 | /** @{*/ |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | /** The Mutex class is used to synchronise the execution of threads. |
elessair | 0:f269e3021894 | 33 | This is for example used to protect access to a shared resource. |
elessair | 0:f269e3021894 | 34 | */ |
elessair | 0:f269e3021894 | 35 | class Mutex { |
elessair | 0:f269e3021894 | 36 | public: |
elessair | 0:f269e3021894 | 37 | /** Create and Initialize a Mutex object */ |
elessair | 0:f269e3021894 | 38 | Mutex(); |
elessair | 0:f269e3021894 | 39 | |
elessair | 0:f269e3021894 | 40 | /** Wait until a Mutex becomes available. |
elessair | 0:f269e3021894 | 41 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever) |
elessair | 0:f269e3021894 | 42 | @return status code that indicates the execution status of the function. |
elessair | 0:f269e3021894 | 43 | */ |
elessair | 0:f269e3021894 | 44 | osStatus lock(uint32_t millisec=osWaitForever); |
elessair | 0:f269e3021894 | 45 | |
elessair | 0:f269e3021894 | 46 | /** Try to lock the mutex, and return immediately |
elessair | 0:f269e3021894 | 47 | @return true if the mutex was acquired, false otherwise. |
elessair | 0:f269e3021894 | 48 | */ |
elessair | 0:f269e3021894 | 49 | bool trylock(); |
elessair | 0:f269e3021894 | 50 | |
elessair | 0:f269e3021894 | 51 | /** Unlock the mutex that has previously been locked by the same thread |
elessair | 0:f269e3021894 | 52 | @return status code that indicates the execution status of the function. |
elessair | 0:f269e3021894 | 53 | */ |
elessair | 0:f269e3021894 | 54 | osStatus unlock(); |
elessair | 0:f269e3021894 | 55 | |
elessair | 0:f269e3021894 | 56 | ~Mutex(); |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | private: |
elessair | 0:f269e3021894 | 59 | osMutexId _osMutexId; |
elessair | 0:f269e3021894 | 60 | osMutexDef_t _osMutexDef; |
elessair | 0:f269e3021894 | 61 | #ifdef CMSIS_OS_RTX |
elessair | 0:f269e3021894 | 62 | #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM) |
elessair | 0:f269e3021894 | 63 | int32_t _mutex_data[4]; |
elessair | 0:f269e3021894 | 64 | #else |
elessair | 0:f269e3021894 | 65 | int32_t _mutex_data[3]; |
elessair | 0:f269e3021894 | 66 | #endif |
elessair | 0:f269e3021894 | 67 | #endif |
elessair | 0:f269e3021894 | 68 | }; |
elessair | 0:f269e3021894 | 69 | |
elessair | 0:f269e3021894 | 70 | } |
elessair | 0:f269e3021894 | 71 | #endif |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | /** @}*/ |