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