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