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