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