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.cpp@0:b435eadf76b4, 2022-03-28 (annotated)
- Committer:
- flo__
- Date:
- Mon Mar 28 15:54:19 2022 +0000
- Revision:
- 0:b435eadf76b4
28/03/2022
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
flo__ | 0:b435eadf76b4 | 1 | /* mbed Microcontroller Library |
flo__ | 0:b435eadf76b4 | 2 | * Copyright (c) 2006-2012 ARM Limited |
flo__ | 0:b435eadf76b4 | 3 | * |
flo__ | 0:b435eadf76b4 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
flo__ | 0:b435eadf76b4 | 5 | * of this software and associated documentation files (the "Software"), to deal |
flo__ | 0:b435eadf76b4 | 6 | * in the Software without restriction, including without limitation the rights |
flo__ | 0:b435eadf76b4 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
flo__ | 0:b435eadf76b4 | 8 | * copies of the Software, and to permit persons to whom the Software is |
flo__ | 0:b435eadf76b4 | 9 | * furnished to do so, subject to the following conditions: |
flo__ | 0:b435eadf76b4 | 10 | * |
flo__ | 0:b435eadf76b4 | 11 | * The above copyright notice and this permission notice shall be included in |
flo__ | 0:b435eadf76b4 | 12 | * all copies or substantial portions of the Software. |
flo__ | 0:b435eadf76b4 | 13 | * |
flo__ | 0:b435eadf76b4 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
flo__ | 0:b435eadf76b4 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
flo__ | 0:b435eadf76b4 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
flo__ | 0:b435eadf76b4 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
flo__ | 0:b435eadf76b4 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
flo__ | 0:b435eadf76b4 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
flo__ | 0:b435eadf76b4 | 20 | * SOFTWARE. |
flo__ | 0:b435eadf76b4 | 21 | */ |
flo__ | 0:b435eadf76b4 | 22 | #include "rtos/Mutex.h" |
flo__ | 0:b435eadf76b4 | 23 | |
flo__ | 0:b435eadf76b4 | 24 | #include <string.h> |
flo__ | 0:b435eadf76b4 | 25 | #include "platform/mbed_error.h" |
flo__ | 0:b435eadf76b4 | 26 | |
flo__ | 0:b435eadf76b4 | 27 | namespace rtos { |
flo__ | 0:b435eadf76b4 | 28 | |
flo__ | 0:b435eadf76b4 | 29 | Mutex::Mutex() { |
flo__ | 0:b435eadf76b4 | 30 | #ifdef CMSIS_OS_RTX |
flo__ | 0:b435eadf76b4 | 31 | memset(_mutex_data, 0, sizeof(_mutex_data)); |
flo__ | 0:b435eadf76b4 | 32 | _osMutexDef.mutex = _mutex_data; |
flo__ | 0:b435eadf76b4 | 33 | #endif |
flo__ | 0:b435eadf76b4 | 34 | _osMutexId = osMutexCreate(&_osMutexDef); |
flo__ | 0:b435eadf76b4 | 35 | if (_osMutexId == NULL) { |
flo__ | 0:b435eadf76b4 | 36 | error("Error initializing the mutex object\n"); |
flo__ | 0:b435eadf76b4 | 37 | } |
flo__ | 0:b435eadf76b4 | 38 | } |
flo__ | 0:b435eadf76b4 | 39 | |
flo__ | 0:b435eadf76b4 | 40 | osStatus Mutex::lock(uint32_t millisec) { |
flo__ | 0:b435eadf76b4 | 41 | return osMutexWait(_osMutexId, millisec); |
flo__ | 0:b435eadf76b4 | 42 | } |
flo__ | 0:b435eadf76b4 | 43 | |
flo__ | 0:b435eadf76b4 | 44 | bool Mutex::trylock() { |
flo__ | 0:b435eadf76b4 | 45 | return (osMutexWait(_osMutexId, 0) == osOK); |
flo__ | 0:b435eadf76b4 | 46 | } |
flo__ | 0:b435eadf76b4 | 47 | |
flo__ | 0:b435eadf76b4 | 48 | osStatus Mutex::unlock() { |
flo__ | 0:b435eadf76b4 | 49 | return osMutexRelease(_osMutexId); |
flo__ | 0:b435eadf76b4 | 50 | } |
flo__ | 0:b435eadf76b4 | 51 | |
flo__ | 0:b435eadf76b4 | 52 | Mutex::~Mutex() { |
flo__ | 0:b435eadf76b4 | 53 | osMutexDelete(_osMutexId); |
flo__ | 0:b435eadf76b4 | 54 | } |
flo__ | 0:b435eadf76b4 | 55 | |
flo__ | 0:b435eadf76b4 | 56 | } |