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.
Fork of mbed-rtos by
Thread.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 "Thread.h" 00023 00024 #include "mbed_error.h" 00025 00026 namespace rtos { 00027 00028 Thread::Thread(void (*task)(void const *argument), void *argument, 00029 osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { 00030 #ifdef CMSIS_OS_RTX 00031 _thread_def.pthread = task; 00032 _thread_def.tpriority = priority; 00033 _thread_def.stacksize = stack_size; 00034 if (stack_pointer != NULL) { 00035 _thread_def.stack_pointer = (uint32_t*)stack_pointer; 00036 _dynamic_stack = false; 00037 } else { 00038 _thread_def.stack_pointer = new uint32_t[stack_size/sizeof(uint32_t)]; 00039 if (_thread_def.stack_pointer == NULL) 00040 error("Error allocating the stack memory\n"); 00041 _dynamic_stack = true; 00042 } 00043 00044 //Fill the stack with a magic word for maximum usage checking 00045 for (uint32_t i = 0; i < (stack_size / sizeof(uint32_t)); i++) { 00046 _thread_def.stack_pointer[i] = 0xE25A2EA5; 00047 } 00048 #endif 00049 _tid = osThreadCreate(&_thread_def, argument); 00050 } 00051 00052 osStatus Thread::terminate() { 00053 return osThreadTerminate(_tid); 00054 } 00055 00056 osStatus Thread::set_priority(osPriority priority) { 00057 return osThreadSetPriority(_tid, priority); 00058 } 00059 00060 osPriority Thread::get_priority() { 00061 return osThreadGetPriority(_tid); 00062 } 00063 00064 int32_t Thread::signal_set(int32_t signals) { 00065 return osSignalSet(_tid, signals); 00066 } 00067 00068 int32_t Thread::signal_clr(int32_t signals) { 00069 return osSignalClear(_tid, signals); 00070 } 00071 00072 Thread::State Thread::get_state() { 00073 #ifndef __MBED_CMSIS_RTOS_CA9 00074 return ((State)_thread_def.tcb.state); 00075 #else 00076 uint8_t status; 00077 status = osThreadGetState(_tid); 00078 return ((State)status); 00079 #endif 00080 } 00081 00082 uint32_t Thread::stack_size() { 00083 #ifndef __MBED_CMSIS_RTOS_CA9 00084 return _thread_def.tcb.priv_stack; 00085 #else 00086 return 0; 00087 #endif 00088 } 00089 00090 uint32_t Thread::free_stack() { 00091 #ifndef __MBED_CMSIS_RTOS_CA9 00092 uint32_t bottom = (uint32_t)_thread_def.tcb.stack; 00093 return _thread_def.tcb.tsk_stack - bottom; 00094 #else 00095 return 0; 00096 #endif 00097 } 00098 00099 uint32_t Thread::used_stack() { 00100 #ifndef __MBED_CMSIS_RTOS_CA9 00101 uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack; 00102 return top - _thread_def.tcb.tsk_stack; 00103 #else 00104 return 0; 00105 #endif 00106 } 00107 00108 uint32_t Thread::max_stack() { 00109 #ifndef __MBED_CMSIS_RTOS_CA9 00110 uint32_t high_mark = 0; 00111 while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5) 00112 high_mark++; 00113 return _thread_def.tcb.priv_stack - (high_mark * 4); 00114 #else 00115 return 0; 00116 #endif 00117 } 00118 00119 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) { 00120 return osSignalWait(signals, millisec); 00121 } 00122 00123 osStatus Thread::wait(uint32_t millisec) { 00124 return osDelay(millisec); 00125 } 00126 00127 osStatus Thread::yield() { 00128 return osThreadYield(); 00129 } 00130 00131 osThreadId Thread::gettid() { 00132 return osThreadGetId(); 00133 } 00134 00135 Thread::~Thread() { 00136 terminate(); 00137 if (_dynamic_stack) { 00138 delete[] (_thread_def.stack_pointer); 00139 } 00140 } 00141 00142 }
Generated on Tue Jul 19 2022 01:16:56 by
