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.
Thread.h
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 #ifndef THREAD_H 00023 #define THREAD_H 00024 00025 #include <stdint.h> 00026 #include "cmsis_os.h" 00027 #include "platform/Callback.h" 00028 #include "platform/toolchain.h" 00029 #include "rtos/Semaphore.h" 00030 #include "rtos/Mutex.h" 00031 00032 namespace rtos { 00033 /** \addtogroup rtos */ 00034 /** @{*/ 00035 00036 /** The Thread class allow defining, creating, and controlling thread functions in the system. 00037 * 00038 * Example: 00039 * @code 00040 * #include "mbed.h" 00041 * #include "rtos.h" 00042 * 00043 * Thread thread; 00044 * DigitalOut led1(LED1); 00045 * volatile bool running = true; 00046 * 00047 * // Blink function toggles the led in a long running loop 00048 * void blink(DigitalOut *led) { 00049 * while (running) { 00050 * *led = !*led; 00051 * Thread::wait(1000); 00052 * } 00053 * } 00054 * 00055 * // Spawns a thread to run blink for 5 seconds 00056 * int main() { 00057 * thread.start(led1, blink); 00058 * Thread::wait(5000); 00059 * running = false; 00060 * thread.join(); 00061 * } 00062 * @endcode 00063 */ 00064 class Thread { 00065 public: 00066 /** Allocate a new thread without starting execution 00067 @param priority initial priority of the thread function. (default: osPriorityNormal). 00068 @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). 00069 @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). 00070 */ 00071 Thread(osPriority priority=osPriorityNormal, 00072 uint32_t stack_size=DEFAULT_STACK_SIZE, 00073 unsigned char *stack_pointer=NULL) { 00074 constructor(priority, stack_size, stack_pointer); 00075 } 00076 00077 /** Create a new thread, and start it executing the specified function. 00078 @param task function to be executed by this thread. 00079 @param argument pointer that is passed to the thread function as start argument. (default: NULL). 00080 @param priority initial priority of the thread function. (default: osPriorityNormal). 00081 @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). 00082 @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). 00083 @deprecated 00084 Thread-spawning constructors hide errors. Replaced by thread.start(task). 00085 00086 @code 00087 Thread thread(priority, stack_size, stack_pointer); 00088 00089 osStatus status = thread.start(task); 00090 if (status != osOK) { 00091 error("oh no!"); 00092 } 00093 @endcode 00094 */ 00095 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00096 "Thread-spawning constructors hide errors. " 00097 "Replaced by thread.start(task).") 00098 Thread(mbed::Callback<void()> task, 00099 osPriority priority=osPriorityNormal, 00100 uint32_t stack_size=DEFAULT_STACK_SIZE, 00101 unsigned char *stack_pointer=NULL) { 00102 constructor(task, priority, stack_size, stack_pointer); 00103 } 00104 00105 /** Create a new thread, and start it executing the specified function. 00106 @param obj argument to task. 00107 @param method function to be executed by this thread. 00108 @param argument pointer that is passed to the thread function as start argument. (default: NULL). 00109 @param priority initial priority of the thread function. (default: osPriorityNormal). 00110 @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). 00111 @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). 00112 @deprecated 00113 Thread-spawning constructors hide errors. Replaced by thread.start(callback(task, argument)). 00114 00115 @code 00116 Thread thread(priority, stack_size, stack_pointer); 00117 00118 osStatus status = thread.start(callback(task, argument)); 00119 if (status != osOK) { 00120 error("oh no!"); 00121 } 00122 @endcode 00123 */ 00124 template <typename T> 00125 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00126 "Thread-spawning constructors hide errors. " 00127 "Replaced by thread.start(callback(task, argument)).") 00128 Thread(T *argument, void (T::*task)(), 00129 osPriority priority=osPriorityNormal, 00130 uint32_t stack_size=DEFAULT_STACK_SIZE, 00131 unsigned char *stack_pointer=NULL) { 00132 constructor(mbed::callback(task, argument), 00133 priority, stack_size, stack_pointer); 00134 } 00135 00136 /** Create a new thread, and start it executing the specified function. 00137 @param obj argument to task. 00138 @param method function to be executed by this thread. 00139 @param argument pointer that is passed to the thread function as start argument. (default: NULL). 00140 @param priority initial priority of the thread function. (default: osPriorityNormal). 00141 @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). 00142 @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). 00143 @deprecated 00144 Thread-spawning constructors hide errors. Replaced by thread.start(callback(task, argument)). 00145 00146 @code 00147 Thread thread(priority, stack_size, stack_pointer); 00148 00149 osStatus status = thread.start(callback(task, argument)); 00150 if (status != osOK) { 00151 error("oh no!"); 00152 } 00153 @endcode 00154 */ 00155 template <typename T> 00156 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00157 "Thread-spawning constructors hide errors. " 00158 "Replaced by thread.start(callback(task, argument)).") 00159 Thread(T *argument, void (*task)(T *), 00160 osPriority priority=osPriorityNormal, 00161 uint32_t stack_size=DEFAULT_STACK_SIZE, 00162 unsigned char *stack_pointer=NULL) { 00163 constructor(mbed::callback(task, argument), 00164 priority, stack_size, stack_pointer); 00165 } 00166 00167 /** Create a new thread, and start it executing the specified function. 00168 Provided for backwards compatibility 00169 @param task function to be executed by this thread. 00170 @param argument pointer that is passed to the thread function as start argument. (default: NULL). 00171 @param priority initial priority of the thread function. (default: osPriorityNormal). 00172 @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). 00173 @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). 00174 @deprecated 00175 Thread-spawning constructors hide errors. Replaced by thread.start(callback(task, argument)). 00176 00177 @code 00178 Thread thread(priority, stack_size, stack_pointer); 00179 00180 osStatus status = thread.start(callback(task, argument)); 00181 if (status != osOK) { 00182 error("oh no!"); 00183 } 00184 @endcode 00185 */ 00186 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00187 "Thread-spawning constructors hide errors. " 00188 "Replaced by thread.start(callback(task, argument)).") 00189 Thread(void (*task)(void const *argument), void *argument=NULL, 00190 osPriority priority=osPriorityNormal, 00191 uint32_t stack_size=DEFAULT_STACK_SIZE, 00192 unsigned char *stack_pointer=NULL) { 00193 constructor(mbed::callback((void (*)(void *))task, argument), 00194 priority, stack_size, stack_pointer); 00195 } 00196 00197 /** Starts a thread executing the specified function. 00198 @param task function to be executed by this thread. 00199 @return status code that indicates the execution status of the function. 00200 */ 00201 osStatus start(mbed::Callback<void()> task); 00202 00203 /** Starts a thread executing the specified function. 00204 @param obj argument to task 00205 @param method function to be executed by this thread. 00206 @return status code that indicates the execution status of the function. 00207 @deprecated 00208 The start function does not support cv-qualifiers. Replaced by start(callback(obj, method)). 00209 */ 00210 template <typename T, typename M> 00211 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00212 "The start function does not support cv-qualifiers. " 00213 "Replaced by thread.start(callback(obj, method)).") 00214 osStatus start(T *obj, M method) { 00215 return start(mbed::callback(obj, method)); 00216 } 00217 00218 /** Wait for thread to terminate 00219 @return status code that indicates the execution status of the function. 00220 @note not callable from interrupt 00221 */ 00222 osStatus join(); 00223 00224 /** Terminate execution of a thread and remove it from Active Threads 00225 @return status code that indicates the execution status of the function. 00226 */ 00227 osStatus terminate(); 00228 00229 /** Set priority of an active thread 00230 @param priority new priority value for the thread function. 00231 @return status code that indicates the execution status of the function. 00232 */ 00233 osStatus set_priority(osPriority priority); 00234 00235 /** Get priority of an active thread 00236 @return current priority value of the thread function. 00237 */ 00238 osPriority get_priority(); 00239 00240 /** Set the specified Signal Flags of an active thread. 00241 @param signals specifies the signal flags of the thread that should be set. 00242 @return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters. 00243 */ 00244 int32_t signal_set(int32_t signals); 00245 00246 /** Clears the specified Signal Flags of an active thread. 00247 @param signals specifies the signal flags of the thread that should be cleared. 00248 @return resultant signal flags of the specified thread or 0x80000000 in case of incorrect parameters. 00249 */ 00250 int32_t signal_clr(int32_t signals); 00251 00252 /** State of the Thread */ 00253 enum State { 00254 Inactive, /**< Not created or terminated */ 00255 Ready, /**< Ready to run */ 00256 Running, /**< Running */ 00257 WaitingDelay, /**< Waiting for a delay to occur */ 00258 WaitingInterval, /**< Waiting for an interval to occur */ 00259 WaitingOr, /**< Waiting for one event in a set to occur */ 00260 WaitingAnd, /**< Waiting for multiple events in a set to occur */ 00261 WaitingSemaphore, /**< Waiting for a semaphore event to occur */ 00262 WaitingMailbox, /**< Waiting for a mailbox event to occur */ 00263 WaitingMutex, /**< Waiting for a mutex event to occur */ 00264 00265 /* Not in sync with RTX below here */ 00266 Deleted, /**< The task has been deleted */ 00267 }; 00268 00269 /** State of this Thread 00270 @return the State of this Thread 00271 */ 00272 State get_state(); 00273 00274 /** Get the total stack memory size for this Thread 00275 @return the total stack memory size in bytes 00276 */ 00277 uint32_t stack_size(); 00278 00279 /** Get the currently unused stack memory for this Thread 00280 @return the currently unused stack memory in bytes 00281 */ 00282 uint32_t free_stack(); 00283 00284 /** Get the currently used stack memory for this Thread 00285 @return the currently used stack memory in bytes 00286 */ 00287 uint32_t used_stack(); 00288 00289 /** Get the maximum stack memory usage to date for this Thread 00290 @return the maximum stack memory usage to date in bytes 00291 */ 00292 uint32_t max_stack(); 00293 00294 /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread. 00295 @param signals wait until all specified signal flags set or 0 for any single signal flag. 00296 @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). 00297 @return event flag information or error code. 00298 @note not callable from interrupt 00299 */ 00300 static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever); 00301 00302 /** Wait for a specified time period in millisec: 00303 @param millisec time delay value 00304 @return status code that indicates the execution status of the function. 00305 @note not callable from interrupt 00306 */ 00307 static osStatus wait(uint32_t millisec); 00308 00309 /** Pass control to next thread that is in state READY. 00310 @return status code that indicates the execution status of the function. 00311 @note not callable from interrupt 00312 */ 00313 static osStatus yield(); 00314 00315 /** Get the thread id of the current running thread. 00316 @return thread ID for reference by other functions or NULL in case of error. 00317 */ 00318 static osThreadId gettid(); 00319 00320 /** Attach a function to be called by the RTOS idle task 00321 @param fptr pointer to the function to be called 00322 */ 00323 static void attach_idle_hook(void (*fptr)(void)); 00324 00325 /** Attach a function to be called when a task is killed 00326 @param fptr pointer to the function to be called 00327 */ 00328 static void attach_terminate_hook(void (*fptr)(osThreadId id)); 00329 00330 virtual ~Thread(); 00331 00332 private: 00333 // Required to share definitions without 00334 // delegated constructors 00335 void constructor(osPriority priority=osPriorityNormal, 00336 uint32_t stack_size=DEFAULT_STACK_SIZE, 00337 unsigned char *stack_pointer=NULL); 00338 void constructor(mbed::Callback<void()> task, 00339 osPriority priority=osPriorityNormal, 00340 uint32_t stack_size=DEFAULT_STACK_SIZE, 00341 unsigned char *stack_pointer=NULL); 00342 static void _thunk(const void * thread_ptr); 00343 00344 mbed::Callback<void()> _task; 00345 osThreadId _tid; 00346 osThreadDef_t _thread_def; 00347 bool _dynamic_stack; 00348 Semaphore _join_sem; 00349 Mutex _mutex; 00350 }; 00351 00352 } 00353 #endif 00354 00355 /** @}*/
Generated on Tue Jul 12 2022 17:34:58 by
