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.
ThisThread.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 00023 #define __STDC_LIMIT_MACROS 00024 #include "rtos/ThisThread.h" 00025 00026 #include "mbed.h" 00027 #include "rtos/rtos_idle.h" 00028 #include "mbed_assert.h" 00029 00030 namespace rtos { 00031 00032 uint32_t ThisThread::flags_clear(uint32_t flags) 00033 { 00034 flags = osThreadFlagsClear(flags); 00035 MBED_ASSERT(!(flags & osFlagsError)); 00036 return flags; 00037 } 00038 00039 uint32_t ThisThread::flags_get() 00040 { 00041 return osThreadFlagsGet(); 00042 } 00043 00044 static uint32_t flags_wait_for(uint32_t flags, uint32_t millisec, bool clear, uint32_t options) 00045 { 00046 if (!clear) { 00047 options |= osFlagsNoClear; 00048 } 00049 flags = osThreadFlagsWait(flags, options, millisec); 00050 if (flags & osFlagsError) { 00051 MBED_ASSERT((flags == osFlagsErrorTimeout && millisec != osWaitForever) || 00052 (flags == osFlagsErrorResource && millisec == 0)); 00053 flags = ThisThread::flags_get(); 00054 } 00055 00056 return flags; 00057 } 00058 00059 static uint32_t flags_wait_until(uint32_t flags, uint64_t millisec, bool clear, uint32_t options) 00060 { 00061 uint64_t now = Kernel::get_ms_count(); 00062 00063 uint32_t delay; 00064 if (now >= millisec) { 00065 delay = 0; 00066 } else if (millisec - now >= osWaitForever) { 00067 // Documentation permits early return for big offsets 00068 delay = osWaitForever - 1; 00069 } else { 00070 delay = millisec - now; 00071 } 00072 return flags_wait_for(flags, delay, clear, options); 00073 } 00074 00075 uint32_t ThisThread::flags_wait_all(uint32_t flags, bool clear) 00076 { 00077 return flags_wait_for(flags, osWaitForever, clear, osFlagsWaitAll); 00078 } 00079 00080 uint32_t ThisThread::flags_wait_all_for(uint32_t flags, uint32_t millisec, bool clear) 00081 { 00082 return flags_wait_for(flags, millisec, clear, osFlagsWaitAll); 00083 } 00084 00085 uint32_t ThisThread::flags_wait_all_until(uint32_t flags, uint64_t millisec, bool clear) 00086 { 00087 return flags_wait_until(flags, millisec, clear, osFlagsWaitAll); 00088 } 00089 00090 uint32_t ThisThread::flags_wait_any(uint32_t flags, bool clear) 00091 { 00092 return flags_wait_for(flags, osWaitForever, clear, osFlagsWaitAny); 00093 } 00094 00095 uint32_t ThisThread::flags_wait_any_for(uint32_t flags, uint32_t millisec, bool clear) 00096 { 00097 return flags_wait_for(flags, millisec, clear, osFlagsWaitAny); 00098 } 00099 00100 uint32_t ThisThread::flags_wait_any_until(uint32_t flags, uint64_t millisec, bool clear) 00101 { 00102 return flags_wait_until(flags, millisec, clear, osFlagsWaitAny); 00103 } 00104 00105 void ThisThread::sleep_for(uint32_t millisec) 00106 { 00107 osStatus_t status = osDelay(millisec); 00108 MBED_ASSERT(status == osOK); 00109 } 00110 00111 void ThisThread::sleep_until(uint64_t millisec) 00112 { 00113 // CMSIS-RTOS 2.1.0 had 64-bit time and osDelayUntil, but that's been revoked. 00114 // Limit ourselves to manual implementation assuming a >=32-bit osDelay. 00115 00116 // 64-bit time doesn't wrap (for half a billion years, at last) 00117 // make the effort to loop for unlimited sleep, as it doesn't cost much 00118 uint64_t now; 00119 00120 while ((now = Kernel::get_ms_count()) < millisec) { 00121 if (millisec - now > UINT32_MAX) { 00122 sleep_for(UINT32_MAX); 00123 continue; 00124 } else { 00125 sleep_for(millisec - now); 00126 break; 00127 } 00128 } 00129 } 00130 00131 void ThisThread::yield() 00132 { 00133 osThreadYield(); 00134 } 00135 00136 osThreadId_t ThisThread::get_id() 00137 { 00138 return osThreadGetId(); 00139 } 00140 00141 }
Generated on Tue Aug 9 2022 00:37:22 by
