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.
Semaphore.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/Semaphore.h" 00023 #include "rtos/Kernel.h" 00024 #include "platform/mbed_assert.h" 00025 00026 #include <string.h> 00027 00028 namespace rtos { 00029 00030 Semaphore::Semaphore(int32_t count) { 00031 constructor(count, 0xffff); 00032 } 00033 00034 Semaphore::Semaphore(int32_t count, uint16_t max_count) { 00035 constructor(count, max_count); 00036 } 00037 00038 void Semaphore::constructor(int32_t count, uint16_t max_count) { 00039 memset(&_obj_mem, 0, sizeof(_obj_mem)); 00040 osSemaphoreAttr_t attr = { 0 }; 00041 attr.cb_mem = &_obj_mem; 00042 attr.cb_size = sizeof(_obj_mem); 00043 _id = osSemaphoreNew(max_count, count, &attr); 00044 MBED_ASSERT(_id != NULL); 00045 } 00046 00047 int32_t Semaphore::wait(uint32_t millisec) { 00048 osStatus_t stat = osSemaphoreAcquire(_id, millisec); 00049 switch (stat) { 00050 case osOK: 00051 return osSemaphoreGetCount(_id) + 1; 00052 case osErrorTimeout: 00053 case osErrorResource: 00054 return 0; 00055 case osErrorParameter: 00056 default: 00057 return -1; 00058 } 00059 } 00060 00061 int32_t Semaphore::wait_until(uint64_t millisec) { 00062 uint64_t now = Kernel::get_ms_count(); 00063 00064 if (now >= millisec) { 00065 return wait(0); 00066 } else if (millisec - now >= osWaitForever) { 00067 // API permits early return 00068 return wait(osWaitForever - 1); 00069 } else { 00070 return wait(millisec - now); 00071 } 00072 } 00073 00074 osStatus Semaphore::release(void) { 00075 return osSemaphoreRelease(_id); 00076 } 00077 00078 Semaphore::~Semaphore() { 00079 osSemaphoreDelete(_id); 00080 } 00081 00082 }
Generated on Tue Jul 12 2022 14:24:34 by
