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.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
ThisThread.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2019 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 #ifndef THIS_THREAD_H 00023 #define THIS_THREAD_H 00024 00025 #include <stdint.h> 00026 #include "rtos/mbed_rtos_types.h" 00027 00028 namespace rtos { 00029 /** \addtogroup rtos-public-api */ 00030 /** @{*/ 00031 00032 /** 00033 * \defgroup rtos_ThisThread ThisThread namespace 00034 * @{ 00035 */ 00036 00037 /** The ThisThread namespace allows controlling the current thread. 00038 * 00039 * Example: 00040 * @code 00041 * #include "mbed.h" 00042 * #include "rtos.h" 00043 * 00044 * Thread thread; 00045 * DigitalOut led1(LED1); 00046 * 00047 * #define STOP_FLAG 1 00048 * 00049 * // Blink function toggles the led in a long running loop 00050 * void blink(DigitalOut *led) { 00051 * while (!ThisThread::flags_wait_any_for(STOP_FLAG, 1000)) { 00052 * *led = !*led; 00053 * } 00054 * } 00055 * 00056 * // Spawns a thread to run blink for 5 seconds 00057 * int main() { 00058 * thread.start(callback(blink, &led1)); 00059 * ThisThread::sleep_for(5000); 00060 * thread.signal_set(STOP_FLAG); 00061 * thread.join(); 00062 * } 00063 * @endcode 00064 * 00065 */ 00066 namespace ThisThread { 00067 /** Clears the specified Thread Flags of the currently running thread. 00068 @param flags specifies the flags of the thread that should be cleared. 00069 @return thread flags before clearing. 00070 @note You cannot call this function from ISR context. 00071 @see Thread::flags_set 00072 */ 00073 uint32_t flags_clear(uint32_t flags); 00074 00075 /** Returns the Thread Flags currently set for the currently running thread. 00076 @return current thread flags or 0 if not in a valid thread. 00077 @note You cannot call this function from ISR context. 00078 @see Thread::flags_set 00079 */ 00080 uint32_t flags_get(); 00081 00082 /** Wait for all of the specified Thread Flags to become signaled for the current thread. 00083 @param flags specifies the flags to wait for 00084 @param clear whether to clear the specified flags after waiting for them. (default: true) 00085 @return actual thread flags before clearing, which will satisfy the wait 00086 @note You cannot call this function from ISR context. 00087 @see Thread::flags_set 00088 */ 00089 uint32_t flags_wait_all(uint32_t flags, bool clear = true); 00090 00091 /** Wait for any of the specified Thread Flags to become signaled for the current thread. 00092 @param flags specifies the flags to wait for 00093 @param clear whether to clear the specified flags after waiting for them. (default: true) 00094 @return actual thread flags before clearing, which will satisfy the wait 00095 @note You cannot call this function from ISR context. 00096 @see Thread::flags_set 00097 */ 00098 uint32_t flags_wait_any(uint32_t flags, bool clear = true); 00099 00100 /** Wait for all of the specified Thread Flags to become signaled for the current thread. 00101 @param flags specifies the flags to wait for 00102 @param millisec timeout value. 00103 @param clear whether to clear the specified flags after waiting for them. (default: true) 00104 @return actual thread flags before clearing, which may not satisfy the wait 00105 @note You cannot call this function from ISR context. 00106 @see Thread::flags_set 00107 */ 00108 uint32_t flags_wait_all_for(uint32_t flags, uint32_t millisec, bool clear = true); 00109 00110 /** Wait for all of the specified Thread Flags to become signaled for the current thread. 00111 @param flags specifies the flags to wait for 00112 @param millisec absolute timeout time, referenced to Kernel::get_ms_count() 00113 @param clear whether to clear the specified flags after waiting for them. (default: true) 00114 @return actual thread flags before clearing, which may not satisfy the wait 00115 @note You cannot call this function from ISR context. 00116 @note the underlying RTOS may have a limit to the maximum wait time 00117 due to internal 32-bit computations, but this is guaranteed to work if the 00118 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, 00119 the wait will time out earlier than specified. 00120 @see Thread::flags_set 00121 */ 00122 uint32_t flags_wait_all_until(uint32_t flags, uint64_t millisec, bool clear = true); 00123 00124 /** Wait for any of the specified Thread Flags to become signaled for the current thread. 00125 @param flags specifies the flags to wait for 00126 @param millisec timeout value. 00127 @param clear whether to clear the specified flags after waiting for them. (default: true) 00128 @return actual thread flags before clearing, which may not satisfy the wait 00129 @note You cannot call this function from ISR context. 00130 @see Thread::flags_set 00131 */ 00132 uint32_t flags_wait_any_for(uint32_t flags, uint32_t millisec, bool clear = true); 00133 00134 /** Wait for any of the specified Thread Flags to become signaled for the current thread. 00135 @param flags specifies the flags to wait for 00136 @param millisec absolute timeout time, referenced to Kernel::get_ms_count() 00137 @param clear whether to clear the specified flags after waiting for them. (default: true) 00138 @return actual thread flags before clearing, which may not satisfy the wait 00139 @note You cannot call this function from ISR context. 00140 @note the underlying RTOS may have a limit to the maximum wait time 00141 due to internal 32-bit computations, but this is guaranteed to work if the 00142 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, 00143 the wait will time out earlier than specified. 00144 @see Thread::flags_set 00145 */ 00146 uint32_t flags_wait_any_until(uint32_t flags, uint64_t millisec, bool clear = true); 00147 00148 /** Sleep for a specified time period in millisec: 00149 @param millisec time delay value 00150 @note You cannot call this function from ISR context. 00151 @note The equivalent functionality is accessible in C via thread_sleep_for. 00152 */ 00153 void sleep_for(uint32_t millisec); 00154 00155 /** Sleep until a specified time in millisec 00156 The specified time is according to Kernel::get_ms_count(). 00157 @param millisec absolute time in millisec 00158 @note You cannot call this function from ISR context. 00159 @note if millisec is equal to or lower than the current tick count, this 00160 returns immediately. 00161 @note The equivalent functionality is accessible in C via thread_sleep_until. 00162 */ 00163 void sleep_until(uint64_t millisec); 00164 00165 /** Pass control to next equal-priority thread that is in state READY. 00166 (Higher-priority READY threads would prevent us from running; this 00167 will not enable lower-priority threads to run, as we remain READY). 00168 @note You cannot call this function from ISR context. 00169 */ 00170 void yield(); 00171 00172 /** Get the thread id of the current running thread. 00173 @return thread ID for reference by other functions or nullptr in case of error or in ISR context. 00174 @note You may call this function from ISR context. 00175 */ 00176 osThreadId_t get_id(); 00177 00178 /** Get the thread name of the current running thread. 00179 @return thread name pointer or nullptr if thread has no name or in case of error. 00180 @note You cannot call this function from ISR context. 00181 */ 00182 const char *get_name(); 00183 00184 }; 00185 /** @}*/ 00186 /** @}*/ 00187 00188 00189 namespace internal { 00190 /** \addtogroup rtos-internal-api */ 00191 /** @{*/ 00192 00193 struct flags_check_capture { 00194 uint32_t *flags; 00195 uint32_t options; 00196 uint32_t flags_wanted; 00197 uint32_t result; 00198 bool match; 00199 }; 00200 00201 bool non_rtos_check_flags(void *handle); 00202 00203 } 00204 /** @}*/ 00205 } 00206 #endif
Generated on Tue Jul 12 2022 13:54:57 by
