Yes
Dependents: Asservissement_Gyro
rtos/Thread.cpp@0:77205fc699b9, 2022-05-08 (annotated)
- Committer:
- braichi13
- Date:
- Sun May 08 14:39:47 2022 +0000
- Revision:
- 0:77205fc699b9
Programme du Gyropode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
braichi13 | 0:77205fc699b9 | 1 | /* mbed Microcontroller Library |
braichi13 | 0:77205fc699b9 | 2 | * Copyright (c) 2006-2012 ARM Limited |
braichi13 | 0:77205fc699b9 | 3 | * |
braichi13 | 0:77205fc699b9 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
braichi13 | 0:77205fc699b9 | 5 | * of this software and associated documentation files (the "Software"), to deal |
braichi13 | 0:77205fc699b9 | 6 | * in the Software without restriction, including without limitation the rights |
braichi13 | 0:77205fc699b9 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
braichi13 | 0:77205fc699b9 | 8 | * copies of the Software, and to permit persons to whom the Software is |
braichi13 | 0:77205fc699b9 | 9 | * furnished to do so, subject to the following conditions: |
braichi13 | 0:77205fc699b9 | 10 | * |
braichi13 | 0:77205fc699b9 | 11 | * The above copyright notice and this permission notice shall be included in |
braichi13 | 0:77205fc699b9 | 12 | * all copies or substantial portions of the Software. |
braichi13 | 0:77205fc699b9 | 13 | * |
braichi13 | 0:77205fc699b9 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
braichi13 | 0:77205fc699b9 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
braichi13 | 0:77205fc699b9 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
braichi13 | 0:77205fc699b9 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
braichi13 | 0:77205fc699b9 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
braichi13 | 0:77205fc699b9 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
braichi13 | 0:77205fc699b9 | 20 | * SOFTWARE. |
braichi13 | 0:77205fc699b9 | 21 | */ |
braichi13 | 0:77205fc699b9 | 22 | #include "rtos/Thread.h" |
braichi13 | 0:77205fc699b9 | 23 | |
braichi13 | 0:77205fc699b9 | 24 | #include "mbed.h" |
braichi13 | 0:77205fc699b9 | 25 | #include "rtos/rtos_idle.h" |
braichi13 | 0:77205fc699b9 | 26 | |
braichi13 | 0:77205fc699b9 | 27 | // rt_tid2ptcb is an internal function which we exposed to get TCB for thread id |
braichi13 | 0:77205fc699b9 | 28 | #undef NULL //Workaround for conflicting macros in rt_TypeDef.h and stdio.h |
braichi13 | 0:77205fc699b9 | 29 | #include "rt_TypeDef.h" |
braichi13 | 0:77205fc699b9 | 30 | |
braichi13 | 0:77205fc699b9 | 31 | extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id); |
braichi13 | 0:77205fc699b9 | 32 | |
braichi13 | 0:77205fc699b9 | 33 | |
braichi13 | 0:77205fc699b9 | 34 | static void (*terminate_hook)(osThreadId id) = 0; |
braichi13 | 0:77205fc699b9 | 35 | extern "C" void thread_terminate_hook(osThreadId id) |
braichi13 | 0:77205fc699b9 | 36 | { |
braichi13 | 0:77205fc699b9 | 37 | if (terminate_hook != (void (*)(osThreadId))NULL) { |
braichi13 | 0:77205fc699b9 | 38 | terminate_hook(id); |
braichi13 | 0:77205fc699b9 | 39 | } |
braichi13 | 0:77205fc699b9 | 40 | } |
braichi13 | 0:77205fc699b9 | 41 | |
braichi13 | 0:77205fc699b9 | 42 | namespace rtos { |
braichi13 | 0:77205fc699b9 | 43 | |
braichi13 | 0:77205fc699b9 | 44 | void Thread::constructor(osPriority priority, |
braichi13 | 0:77205fc699b9 | 45 | uint32_t stack_size, unsigned char *stack_pointer) { |
braichi13 | 0:77205fc699b9 | 46 | _tid = 0; |
braichi13 | 0:77205fc699b9 | 47 | _dynamic_stack = (stack_pointer == NULL); |
braichi13 | 0:77205fc699b9 | 48 | |
braichi13 | 0:77205fc699b9 | 49 | #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM) |
braichi13 | 0:77205fc699b9 | 50 | _thread_def.tpriority = priority; |
braichi13 | 0:77205fc699b9 | 51 | _thread_def.stacksize = stack_size; |
braichi13 | 0:77205fc699b9 | 52 | _thread_def.stack_pointer = (uint32_t*)stack_pointer; |
braichi13 | 0:77205fc699b9 | 53 | #endif |
braichi13 | 0:77205fc699b9 | 54 | } |
braichi13 | 0:77205fc699b9 | 55 | |
braichi13 | 0:77205fc699b9 | 56 | void Thread::constructor(Callback<void()> task, |
braichi13 | 0:77205fc699b9 | 57 | osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { |
braichi13 | 0:77205fc699b9 | 58 | constructor(priority, stack_size, stack_pointer); |
braichi13 | 0:77205fc699b9 | 59 | |
braichi13 | 0:77205fc699b9 | 60 | switch (start(task)) { |
braichi13 | 0:77205fc699b9 | 61 | case osErrorResource: |
braichi13 | 0:77205fc699b9 | 62 | error("OS ran out of threads!\n"); |
braichi13 | 0:77205fc699b9 | 63 | break; |
braichi13 | 0:77205fc699b9 | 64 | case osErrorParameter: |
braichi13 | 0:77205fc699b9 | 65 | error("Thread already running!\n"); |
braichi13 | 0:77205fc699b9 | 66 | break; |
braichi13 | 0:77205fc699b9 | 67 | case osErrorNoMemory: |
braichi13 | 0:77205fc699b9 | 68 | error("Error allocating the stack memory\n"); |
braichi13 | 0:77205fc699b9 | 69 | default: |
braichi13 | 0:77205fc699b9 | 70 | break; |
braichi13 | 0:77205fc699b9 | 71 | } |
braichi13 | 0:77205fc699b9 | 72 | } |
braichi13 | 0:77205fc699b9 | 73 | |
braichi13 | 0:77205fc699b9 | 74 | osStatus Thread::start(Callback<void()> task) { |
braichi13 | 0:77205fc699b9 | 75 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 76 | |
braichi13 | 0:77205fc699b9 | 77 | if (_tid != 0) { |
braichi13 | 0:77205fc699b9 | 78 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 79 | return osErrorParameter; |
braichi13 | 0:77205fc699b9 | 80 | } |
braichi13 | 0:77205fc699b9 | 81 | |
braichi13 | 0:77205fc699b9 | 82 | #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM) |
braichi13 | 0:77205fc699b9 | 83 | _thread_def.pthread = Thread::_thunk; |
braichi13 | 0:77205fc699b9 | 84 | if (_thread_def.stack_pointer == NULL) { |
braichi13 | 0:77205fc699b9 | 85 | _thread_def.stack_pointer = new uint32_t[_thread_def.stacksize/sizeof(uint32_t)]; |
braichi13 | 0:77205fc699b9 | 86 | MBED_ASSERT(_thread_def.stack_pointer != NULL); |
braichi13 | 0:77205fc699b9 | 87 | } |
braichi13 | 0:77205fc699b9 | 88 | |
braichi13 | 0:77205fc699b9 | 89 | //Fill the stack with a magic word for maximum usage checking |
braichi13 | 0:77205fc699b9 | 90 | for (uint32_t i = 0; i < (_thread_def.stacksize / sizeof(uint32_t)); i++) { |
braichi13 | 0:77205fc699b9 | 91 | _thread_def.stack_pointer[i] = 0xE25A2EA5; |
braichi13 | 0:77205fc699b9 | 92 | } |
braichi13 | 0:77205fc699b9 | 93 | #endif |
braichi13 | 0:77205fc699b9 | 94 | _task = task; |
braichi13 | 0:77205fc699b9 | 95 | _tid = osThreadCreate(&_thread_def, this); |
braichi13 | 0:77205fc699b9 | 96 | if (_tid == NULL) { |
braichi13 | 0:77205fc699b9 | 97 | if (_dynamic_stack) { |
braichi13 | 0:77205fc699b9 | 98 | delete[] (_thread_def.stack_pointer); |
braichi13 | 0:77205fc699b9 | 99 | _thread_def.stack_pointer = (uint32_t*)NULL; |
braichi13 | 0:77205fc699b9 | 100 | } |
braichi13 | 0:77205fc699b9 | 101 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 102 | _join_sem.release(); |
braichi13 | 0:77205fc699b9 | 103 | return osErrorResource; |
braichi13 | 0:77205fc699b9 | 104 | } |
braichi13 | 0:77205fc699b9 | 105 | |
braichi13 | 0:77205fc699b9 | 106 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 107 | return osOK; |
braichi13 | 0:77205fc699b9 | 108 | } |
braichi13 | 0:77205fc699b9 | 109 | |
braichi13 | 0:77205fc699b9 | 110 | osStatus Thread::terminate() { |
braichi13 | 0:77205fc699b9 | 111 | osStatus ret; |
braichi13 | 0:77205fc699b9 | 112 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 113 | |
braichi13 | 0:77205fc699b9 | 114 | // Set the Thread's tid to NULL and |
braichi13 | 0:77205fc699b9 | 115 | // release the semaphore before terminating |
braichi13 | 0:77205fc699b9 | 116 | // since this thread could be terminating itself |
braichi13 | 0:77205fc699b9 | 117 | osThreadId local_id = _tid; |
braichi13 | 0:77205fc699b9 | 118 | _join_sem.release(); |
braichi13 | 0:77205fc699b9 | 119 | _tid = (osThreadId)NULL; |
braichi13 | 0:77205fc699b9 | 120 | |
braichi13 | 0:77205fc699b9 | 121 | ret = osThreadTerminate(local_id); |
braichi13 | 0:77205fc699b9 | 122 | |
braichi13 | 0:77205fc699b9 | 123 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 124 | return ret; |
braichi13 | 0:77205fc699b9 | 125 | } |
braichi13 | 0:77205fc699b9 | 126 | |
braichi13 | 0:77205fc699b9 | 127 | osStatus Thread::join() { |
braichi13 | 0:77205fc699b9 | 128 | int32_t ret = _join_sem.wait(); |
braichi13 | 0:77205fc699b9 | 129 | if (ret < 0) { |
braichi13 | 0:77205fc699b9 | 130 | return osErrorOS; |
braichi13 | 0:77205fc699b9 | 131 | } |
braichi13 | 0:77205fc699b9 | 132 | |
braichi13 | 0:77205fc699b9 | 133 | // The semaphore has been released so this thread is being |
braichi13 | 0:77205fc699b9 | 134 | // terminated or has been terminated. Once the mutex has |
braichi13 | 0:77205fc699b9 | 135 | // been locked it is ensured that the thread is deleted. |
braichi13 | 0:77205fc699b9 | 136 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 137 | MBED_ASSERT(NULL == _tid); |
braichi13 | 0:77205fc699b9 | 138 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 139 | |
braichi13 | 0:77205fc699b9 | 140 | // Release sem so any other threads joining this thread wake up |
braichi13 | 0:77205fc699b9 | 141 | _join_sem.release(); |
braichi13 | 0:77205fc699b9 | 142 | return osOK; |
braichi13 | 0:77205fc699b9 | 143 | } |
braichi13 | 0:77205fc699b9 | 144 | |
braichi13 | 0:77205fc699b9 | 145 | osStatus Thread::set_priority(osPriority priority) { |
braichi13 | 0:77205fc699b9 | 146 | osStatus ret; |
braichi13 | 0:77205fc699b9 | 147 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 148 | |
braichi13 | 0:77205fc699b9 | 149 | ret = osThreadSetPriority(_tid, priority); |
braichi13 | 0:77205fc699b9 | 150 | |
braichi13 | 0:77205fc699b9 | 151 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 152 | return ret; |
braichi13 | 0:77205fc699b9 | 153 | } |
braichi13 | 0:77205fc699b9 | 154 | |
braichi13 | 0:77205fc699b9 | 155 | osPriority Thread::get_priority() { |
braichi13 | 0:77205fc699b9 | 156 | osPriority ret; |
braichi13 | 0:77205fc699b9 | 157 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 158 | |
braichi13 | 0:77205fc699b9 | 159 | ret = osThreadGetPriority(_tid); |
braichi13 | 0:77205fc699b9 | 160 | |
braichi13 | 0:77205fc699b9 | 161 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 162 | return ret; |
braichi13 | 0:77205fc699b9 | 163 | } |
braichi13 | 0:77205fc699b9 | 164 | |
braichi13 | 0:77205fc699b9 | 165 | int32_t Thread::signal_set(int32_t signals) { |
braichi13 | 0:77205fc699b9 | 166 | // osSignalSet is thread safe as long as the underlying |
braichi13 | 0:77205fc699b9 | 167 | // thread does not get terminated or return from main |
braichi13 | 0:77205fc699b9 | 168 | return osSignalSet(_tid, signals); |
braichi13 | 0:77205fc699b9 | 169 | } |
braichi13 | 0:77205fc699b9 | 170 | |
braichi13 | 0:77205fc699b9 | 171 | int32_t Thread::signal_clr(int32_t signals) { |
braichi13 | 0:77205fc699b9 | 172 | // osSignalClear is thread safe as long as the underlying |
braichi13 | 0:77205fc699b9 | 173 | // thread does not get terminated or return from main |
braichi13 | 0:77205fc699b9 | 174 | return osSignalClear(_tid, signals); |
braichi13 | 0:77205fc699b9 | 175 | } |
braichi13 | 0:77205fc699b9 | 176 | |
braichi13 | 0:77205fc699b9 | 177 | Thread::State Thread::get_state() { |
braichi13 | 0:77205fc699b9 | 178 | #if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM) |
braichi13 | 0:77205fc699b9 | 179 | #ifdef CMSIS_OS_RTX |
braichi13 | 0:77205fc699b9 | 180 | State status = Deleted; |
braichi13 | 0:77205fc699b9 | 181 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 182 | |
braichi13 | 0:77205fc699b9 | 183 | if (_tid != NULL) { |
braichi13 | 0:77205fc699b9 | 184 | status = (State)_thread_def.tcb.state; |
braichi13 | 0:77205fc699b9 | 185 | } |
braichi13 | 0:77205fc699b9 | 186 | |
braichi13 | 0:77205fc699b9 | 187 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 188 | return status; |
braichi13 | 0:77205fc699b9 | 189 | #endif |
braichi13 | 0:77205fc699b9 | 190 | #else |
braichi13 | 0:77205fc699b9 | 191 | State status = Deleted; |
braichi13 | 0:77205fc699b9 | 192 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 193 | |
braichi13 | 0:77205fc699b9 | 194 | if (_tid != NULL) { |
braichi13 | 0:77205fc699b9 | 195 | status = (State)osThreadGetState(_tid); |
braichi13 | 0:77205fc699b9 | 196 | } |
braichi13 | 0:77205fc699b9 | 197 | |
braichi13 | 0:77205fc699b9 | 198 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 199 | return status; |
braichi13 | 0:77205fc699b9 | 200 | #endif |
braichi13 | 0:77205fc699b9 | 201 | } |
braichi13 | 0:77205fc699b9 | 202 | |
braichi13 | 0:77205fc699b9 | 203 | uint32_t Thread::stack_size() { |
braichi13 | 0:77205fc699b9 | 204 | #ifndef __MBED_CMSIS_RTOS_CA9 |
braichi13 | 0:77205fc699b9 | 205 | #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) |
braichi13 | 0:77205fc699b9 | 206 | uint32_t size = 0; |
braichi13 | 0:77205fc699b9 | 207 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 208 | |
braichi13 | 0:77205fc699b9 | 209 | if (_tid != NULL) { |
braichi13 | 0:77205fc699b9 | 210 | size = _thread_def.tcb.priv_stack; |
braichi13 | 0:77205fc699b9 | 211 | } |
braichi13 | 0:77205fc699b9 | 212 | |
braichi13 | 0:77205fc699b9 | 213 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 214 | return size; |
braichi13 | 0:77205fc699b9 | 215 | #else |
braichi13 | 0:77205fc699b9 | 216 | uint32_t size = 0; |
braichi13 | 0:77205fc699b9 | 217 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 218 | |
braichi13 | 0:77205fc699b9 | 219 | if (_tid != NULL) { |
braichi13 | 0:77205fc699b9 | 220 | P_TCB tcb = rt_tid2ptcb(_tid); |
braichi13 | 0:77205fc699b9 | 221 | size = tcb->priv_stack; |
braichi13 | 0:77205fc699b9 | 222 | } |
braichi13 | 0:77205fc699b9 | 223 | |
braichi13 | 0:77205fc699b9 | 224 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 225 | return size; |
braichi13 | 0:77205fc699b9 | 226 | #endif |
braichi13 | 0:77205fc699b9 | 227 | #else |
braichi13 | 0:77205fc699b9 | 228 | return 0; |
braichi13 | 0:77205fc699b9 | 229 | #endif |
braichi13 | 0:77205fc699b9 | 230 | } |
braichi13 | 0:77205fc699b9 | 231 | |
braichi13 | 0:77205fc699b9 | 232 | uint32_t Thread::free_stack() { |
braichi13 | 0:77205fc699b9 | 233 | #ifndef __MBED_CMSIS_RTOS_CA9 |
braichi13 | 0:77205fc699b9 | 234 | #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) |
braichi13 | 0:77205fc699b9 | 235 | uint32_t size = 0; |
braichi13 | 0:77205fc699b9 | 236 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 237 | |
braichi13 | 0:77205fc699b9 | 238 | if (_tid != NULL) { |
braichi13 | 0:77205fc699b9 | 239 | uint32_t bottom = (uint32_t)_thread_def.tcb.stack; |
braichi13 | 0:77205fc699b9 | 240 | size = _thread_def.tcb.tsk_stack - bottom; |
braichi13 | 0:77205fc699b9 | 241 | } |
braichi13 | 0:77205fc699b9 | 242 | |
braichi13 | 0:77205fc699b9 | 243 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 244 | return size; |
braichi13 | 0:77205fc699b9 | 245 | #else |
braichi13 | 0:77205fc699b9 | 246 | uint32_t size = 0; |
braichi13 | 0:77205fc699b9 | 247 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 248 | |
braichi13 | 0:77205fc699b9 | 249 | if (_tid != NULL) { |
braichi13 | 0:77205fc699b9 | 250 | P_TCB tcb = rt_tid2ptcb(_tid); |
braichi13 | 0:77205fc699b9 | 251 | uint32_t bottom = (uint32_t)tcb->stack; |
braichi13 | 0:77205fc699b9 | 252 | size = tcb->tsk_stack - bottom; |
braichi13 | 0:77205fc699b9 | 253 | } |
braichi13 | 0:77205fc699b9 | 254 | |
braichi13 | 0:77205fc699b9 | 255 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 256 | return size; |
braichi13 | 0:77205fc699b9 | 257 | #endif |
braichi13 | 0:77205fc699b9 | 258 | #else |
braichi13 | 0:77205fc699b9 | 259 | return 0; |
braichi13 | 0:77205fc699b9 | 260 | #endif |
braichi13 | 0:77205fc699b9 | 261 | } |
braichi13 | 0:77205fc699b9 | 262 | |
braichi13 | 0:77205fc699b9 | 263 | uint32_t Thread::used_stack() { |
braichi13 | 0:77205fc699b9 | 264 | #ifndef __MBED_CMSIS_RTOS_CA9 |
braichi13 | 0:77205fc699b9 | 265 | #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) |
braichi13 | 0:77205fc699b9 | 266 | uint32_t size = 0; |
braichi13 | 0:77205fc699b9 | 267 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 268 | |
braichi13 | 0:77205fc699b9 | 269 | if (_tid != NULL) { |
braichi13 | 0:77205fc699b9 | 270 | uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack; |
braichi13 | 0:77205fc699b9 | 271 | size = top - _thread_def.tcb.tsk_stack; |
braichi13 | 0:77205fc699b9 | 272 | } |
braichi13 | 0:77205fc699b9 | 273 | |
braichi13 | 0:77205fc699b9 | 274 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 275 | return size; |
braichi13 | 0:77205fc699b9 | 276 | #else |
braichi13 | 0:77205fc699b9 | 277 | uint32_t size = 0; |
braichi13 | 0:77205fc699b9 | 278 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 279 | |
braichi13 | 0:77205fc699b9 | 280 | if (_tid != NULL) { |
braichi13 | 0:77205fc699b9 | 281 | P_TCB tcb = rt_tid2ptcb(_tid); |
braichi13 | 0:77205fc699b9 | 282 | uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack; |
braichi13 | 0:77205fc699b9 | 283 | size = top - tcb->tsk_stack; |
braichi13 | 0:77205fc699b9 | 284 | } |
braichi13 | 0:77205fc699b9 | 285 | |
braichi13 | 0:77205fc699b9 | 286 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 287 | return size; |
braichi13 | 0:77205fc699b9 | 288 | #endif |
braichi13 | 0:77205fc699b9 | 289 | #else |
braichi13 | 0:77205fc699b9 | 290 | return 0; |
braichi13 | 0:77205fc699b9 | 291 | #endif |
braichi13 | 0:77205fc699b9 | 292 | } |
braichi13 | 0:77205fc699b9 | 293 | |
braichi13 | 0:77205fc699b9 | 294 | uint32_t Thread::max_stack() { |
braichi13 | 0:77205fc699b9 | 295 | #ifndef __MBED_CMSIS_RTOS_CA9 |
braichi13 | 0:77205fc699b9 | 296 | #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) |
braichi13 | 0:77205fc699b9 | 297 | uint32_t size = 0; |
braichi13 | 0:77205fc699b9 | 298 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 299 | |
braichi13 | 0:77205fc699b9 | 300 | if (_tid != NULL) { |
braichi13 | 0:77205fc699b9 | 301 | uint32_t high_mark = 0; |
braichi13 | 0:77205fc699b9 | 302 | while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5) |
braichi13 | 0:77205fc699b9 | 303 | high_mark++; |
braichi13 | 0:77205fc699b9 | 304 | size = _thread_def.tcb.priv_stack - (high_mark * 4); |
braichi13 | 0:77205fc699b9 | 305 | } |
braichi13 | 0:77205fc699b9 | 306 | |
braichi13 | 0:77205fc699b9 | 307 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 308 | return size; |
braichi13 | 0:77205fc699b9 | 309 | #else |
braichi13 | 0:77205fc699b9 | 310 | uint32_t size = 0; |
braichi13 | 0:77205fc699b9 | 311 | _mutex.lock(); |
braichi13 | 0:77205fc699b9 | 312 | |
braichi13 | 0:77205fc699b9 | 313 | if (_tid != NULL) { |
braichi13 | 0:77205fc699b9 | 314 | P_TCB tcb = rt_tid2ptcb(_tid); |
braichi13 | 0:77205fc699b9 | 315 | uint32_t high_mark = 0; |
braichi13 | 0:77205fc699b9 | 316 | while (tcb->stack[high_mark] == 0xE25A2EA5) |
braichi13 | 0:77205fc699b9 | 317 | high_mark++; |
braichi13 | 0:77205fc699b9 | 318 | size = tcb->priv_stack - (high_mark * 4); |
braichi13 | 0:77205fc699b9 | 319 | } |
braichi13 | 0:77205fc699b9 | 320 | |
braichi13 | 0:77205fc699b9 | 321 | _mutex.unlock(); |
braichi13 | 0:77205fc699b9 | 322 | return size; |
braichi13 | 0:77205fc699b9 | 323 | #endif |
braichi13 | 0:77205fc699b9 | 324 | #else |
braichi13 | 0:77205fc699b9 | 325 | return 0; |
braichi13 | 0:77205fc699b9 | 326 | #endif |
braichi13 | 0:77205fc699b9 | 327 | } |
braichi13 | 0:77205fc699b9 | 328 | |
braichi13 | 0:77205fc699b9 | 329 | osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) { |
braichi13 | 0:77205fc699b9 | 330 | return osSignalWait(signals, millisec); |
braichi13 | 0:77205fc699b9 | 331 | } |
braichi13 | 0:77205fc699b9 | 332 | |
braichi13 | 0:77205fc699b9 | 333 | osStatus Thread::wait(uint32_t millisec) { |
braichi13 | 0:77205fc699b9 | 334 | return osDelay(millisec); |
braichi13 | 0:77205fc699b9 | 335 | } |
braichi13 | 0:77205fc699b9 | 336 | |
braichi13 | 0:77205fc699b9 | 337 | osStatus Thread::yield() { |
braichi13 | 0:77205fc699b9 | 338 | return osThreadYield(); |
braichi13 | 0:77205fc699b9 | 339 | } |
braichi13 | 0:77205fc699b9 | 340 | |
braichi13 | 0:77205fc699b9 | 341 | osThreadId Thread::gettid() { |
braichi13 | 0:77205fc699b9 | 342 | return osThreadGetId(); |
braichi13 | 0:77205fc699b9 | 343 | } |
braichi13 | 0:77205fc699b9 | 344 | |
braichi13 | 0:77205fc699b9 | 345 | void Thread::attach_idle_hook(void (*fptr)(void)) { |
braichi13 | 0:77205fc699b9 | 346 | rtos_attach_idle_hook(fptr); |
braichi13 | 0:77205fc699b9 | 347 | } |
braichi13 | 0:77205fc699b9 | 348 | |
braichi13 | 0:77205fc699b9 | 349 | void Thread::attach_terminate_hook(void (*fptr)(osThreadId id)) { |
braichi13 | 0:77205fc699b9 | 350 | terminate_hook = fptr; |
braichi13 | 0:77205fc699b9 | 351 | } |
braichi13 | 0:77205fc699b9 | 352 | |
braichi13 | 0:77205fc699b9 | 353 | Thread::~Thread() { |
braichi13 | 0:77205fc699b9 | 354 | // terminate is thread safe |
braichi13 | 0:77205fc699b9 | 355 | terminate(); |
braichi13 | 0:77205fc699b9 | 356 | #ifdef __MBED_CMSIS_RTOS_CM |
braichi13 | 0:77205fc699b9 | 357 | if (_dynamic_stack) { |
braichi13 | 0:77205fc699b9 | 358 | delete[] (_thread_def.stack_pointer); |
braichi13 | 0:77205fc699b9 | 359 | _thread_def.stack_pointer = (uint32_t*)NULL; |
braichi13 | 0:77205fc699b9 | 360 | } |
braichi13 | 0:77205fc699b9 | 361 | #endif |
braichi13 | 0:77205fc699b9 | 362 | } |
braichi13 | 0:77205fc699b9 | 363 | |
braichi13 | 0:77205fc699b9 | 364 | void Thread::_thunk(const void * thread_ptr) |
braichi13 | 0:77205fc699b9 | 365 | { |
braichi13 | 0:77205fc699b9 | 366 | Thread *t = (Thread*)thread_ptr; |
braichi13 | 0:77205fc699b9 | 367 | t->_task(); |
braichi13 | 0:77205fc699b9 | 368 | t->_mutex.lock(); |
braichi13 | 0:77205fc699b9 | 369 | t->_tid = (osThreadId)NULL; |
braichi13 | 0:77205fc699b9 | 370 | t->_join_sem.release(); |
braichi13 | 0:77205fc699b9 | 371 | // rtos will release the mutex automatically |
braichi13 | 0:77205fc699b9 | 372 | } |
braichi13 | 0:77205fc699b9 | 373 | |
braichi13 | 0:77205fc699b9 | 374 | } |