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