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