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