Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
Mutex.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2012 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 #include "rtos/Mutex.h" 00023 00024 #include <string.h> 00025 #include "mbed_error.h" 00026 #include "mbed_assert.h" 00027 00028 namespace rtos { 00029 00030 Mutex::Mutex(): _count(0) 00031 { 00032 constructor(); 00033 } 00034 00035 Mutex::Mutex(const char *name) 00036 { 00037 constructor(name); 00038 } 00039 00040 void Mutex::constructor(const char *name) 00041 { 00042 memset(&_obj_mem, 0, sizeof(_obj_mem)); 00043 osMutexAttr_t attr = { 0 }; 00044 attr.name = name ? name : "aplication_unnamed_mutex"; 00045 attr.cb_mem = &_obj_mem; 00046 attr.cb_size = sizeof(_obj_mem); 00047 attr.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust; 00048 _id = osMutexNew(&attr); 00049 MBED_ASSERT(_id); 00050 } 00051 00052 osStatus Mutex::lock(uint32_t millisec) { 00053 osStatus status = osMutexAcquire(_id, millisec); 00054 if (osOK == status) { 00055 _count++; 00056 } 00057 return status; 00058 } 00059 00060 bool Mutex::trylock() { 00061 if (osMutexAcquire(_id, 0) == osOK) { 00062 _count++; 00063 return true; 00064 } else { 00065 return false; 00066 } 00067 } 00068 00069 osStatus Mutex::unlock() { 00070 _count--; 00071 return osMutexRelease(_id); 00072 } 00073 00074 osThreadId Mutex::get_owner() { 00075 return osMutexGetOwner(_id); 00076 } 00077 00078 Mutex::~Mutex() { 00079 osMutexDelete(_id); 00080 } 00081 00082 }
Generated on Sun Jul 17 2022 08:25:28 by 1.7.2