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.
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 #include "rtos/Kernel.h" 00024 00025 #include <string.h> 00026 #include "mbed_error.h" 00027 #include "mbed_assert.h" 00028 00029 namespace rtos { 00030 00031 Mutex::Mutex(): _count(0) 00032 { 00033 constructor(); 00034 } 00035 00036 Mutex::Mutex(const char *name) 00037 { 00038 constructor(name); 00039 } 00040 00041 void Mutex::constructor(const char *name) 00042 { 00043 memset(&_obj_mem, 0, sizeof(_obj_mem)); 00044 osMutexAttr_t attr = 00045 { 0 }; 00046 attr.name = name ? name : "aplication_unnamed_mutex"; 00047 attr.cb_mem = &_obj_mem; 00048 attr.cb_size = sizeof(_obj_mem); 00049 attr.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust; 00050 _id = osMutexNew(&attr); 00051 MBED_ASSERT(_id); 00052 } 00053 00054 osStatus Mutex::lock(void) 00055 { 00056 osStatus status = osMutexAcquire(_id, osWaitForever); 00057 if (osOK == status) { 00058 _count++; 00059 } 00060 00061 if (status != osOK) { 00062 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_LOCK_FAILED), "Mutex lock failed", status); 00063 } 00064 00065 return osOK; 00066 } 00067 00068 osStatus Mutex::lock(uint32_t millisec) 00069 { 00070 osStatus status = osMutexAcquire(_id, millisec); 00071 if (osOK == status) { 00072 _count++; 00073 } 00074 00075 bool success = (status == osOK || 00076 (status == osErrorResource && millisec == 0) || 00077 (status == osErrorTimeout && millisec != osWaitForever)); 00078 00079 if (!success) { 00080 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_LOCK_FAILED), "Mutex lock failed", status); 00081 } 00082 00083 return status; 00084 } 00085 00086 bool Mutex::trylock() 00087 { 00088 return trylock_for(0); 00089 } 00090 00091 bool Mutex::trylock_for(uint32_t millisec) 00092 { 00093 osStatus status = osMutexAcquire(_id, millisec); 00094 if (status == osOK) { 00095 return true; 00096 } 00097 00098 bool success = (status == osOK || 00099 (status == osErrorResource && millisec == 0) || 00100 (status == osErrorTimeout && millisec != osWaitForever)); 00101 00102 if (!success) { 00103 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_LOCK_FAILED), "Mutex lock failed", status); 00104 } 00105 00106 return false; 00107 } 00108 00109 bool Mutex::trylock_until(uint64_t millisec) 00110 { 00111 uint64_t now = Kernel::get_ms_count(); 00112 00113 if (now >= millisec) { 00114 return trylock(); 00115 } else if (millisec - now >= osWaitForever) { 00116 // API permits early return 00117 return trylock_for(osWaitForever - 1); 00118 } else { 00119 return trylock_for(millisec - now); 00120 } 00121 } 00122 00123 osStatus Mutex::unlock() 00124 { 00125 _count--; 00126 00127 osStatus status = osMutexRelease(_id); 00128 00129 if (status != osOK) { 00130 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_UNLOCK_FAILED), "Mutex unlock failed", status); 00131 } 00132 00133 return osOK; 00134 } 00135 00136 osThreadId Mutex::get_owner() 00137 { 00138 return osMutexGetOwner(_id); 00139 } 00140 00141 Mutex::~Mutex() 00142 { 00143 osMutexDelete(_id); 00144 } 00145 00146 }
Generated on Tue Aug 9 2022 00:37:16 by
