Clone of mbed rtos 2 for easy accessibility.
Fork of mbed-rtos by
rtos/Thread.cpp@125:5713cbbdb706, 2017-07-04 (annotated)
- Committer:
- Kojto
- Date:
- Tue Jul 04 13:32:20 2017 +0100
- Revision:
- 125:5713cbbdb706
- Parent:
- 123:58563e6cba1e
replace mbed_rtx by mbed_rtx4
Not causing a conflict with mbed_rtx that is for newer rtos
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 | */ |
c1728p9 | 123:58563e6cba1e | 22 | #include "rtos/Thread.h" |
emilmont | 8:88a1a9c26ae3 | 23 | |
Kojto | 118:6635230e06ba | 24 | #include "mbed.h" |
c1728p9 | 123:58563e6cba1e | 25 | #include "rtos/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 | |
c1728p9 | 123:58563e6cba1e | 33 | |
c1728p9 | 123:58563e6cba1e | 34 | static void (*terminate_hook)(osThreadId id) = 0; |
c1728p9 | 123:58563e6cba1e | 35 | extern "C" void thread_terminate_hook(osThreadId id) |
c1728p9 | 123:58563e6cba1e | 36 | { |
c1728p9 | 123:58563e6cba1e | 37 | if (terminate_hook != (void (*)(osThreadId))NULL) { |
c1728p9 | 123:58563e6cba1e | 38 | terminate_hook(id); |
c1728p9 | 123:58563e6cba1e | 39 | } |
c1728p9 | 123:58563e6cba1e | 40 | } |
c1728p9 | 123:58563e6cba1e | 41 | |
emilmont | 8:88a1a9c26ae3 | 42 | namespace rtos { |
emilmont | 8:88a1a9c26ae3 | 43 | |
Kojto | 118:6635230e06ba | 44 | void Thread::constructor(osPriority priority, |
Kojto | 118:6635230e06ba | 45 | uint32_t stack_size, unsigned char *stack_pointer) { |
Kojto | 118:6635230e06ba | 46 | _tid = 0; |
Kojto | 118:6635230e06ba | 47 | _dynamic_stack = (stack_pointer == NULL); |
Kojto | 118:6635230e06ba | 48 | |
mbed_official | 115:11950e007d8a | 49 | #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM) |
emilmont | 8:88a1a9c26ae3 | 50 | _thread_def.tpriority = priority; |
emilmont | 8:88a1a9c26ae3 | 51 | _thread_def.stacksize = stack_size; |
Kojto | 118:6635230e06ba | 52 | _thread_def.stack_pointer = (uint32_t*)stack_pointer; |
Kojto | 118:6635230e06ba | 53 | #endif |
Kojto | 118:6635230e06ba | 54 | } |
Kojto | 118:6635230e06ba | 55 | |
Kojto | 118:6635230e06ba | 56 | void Thread::constructor(Callback<void()> task, |
Kojto | 118:6635230e06ba | 57 | osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { |
Kojto | 118:6635230e06ba | 58 | constructor(priority, stack_size, stack_pointer); |
Kojto | 118:6635230e06ba | 59 | |
Kojto | 118:6635230e06ba | 60 | switch (start(task)) { |
Kojto | 118:6635230e06ba | 61 | case osErrorResource: |
Kojto | 118:6635230e06ba | 62 | error("OS ran out of threads!\n"); |
Kojto | 118:6635230e06ba | 63 | break; |
Kojto | 118:6635230e06ba | 64 | case osErrorParameter: |
Kojto | 118:6635230e06ba | 65 | error("Thread already running!\n"); |
Kojto | 118:6635230e06ba | 66 | break; |
Kojto | 118:6635230e06ba | 67 | case osErrorNoMemory: |
Kojto | 118:6635230e06ba | 68 | error("Error allocating the stack memory\n"); |
Kojto | 118:6635230e06ba | 69 | default: |
Kojto | 118:6635230e06ba | 70 | break; |
Kojto | 118:6635230e06ba | 71 | } |
Kojto | 118:6635230e06ba | 72 | } |
Kojto | 118:6635230e06ba | 73 | |
Kojto | 118:6635230e06ba | 74 | osStatus Thread::start(Callback<void()> task) { |
Kojto | 119:19af2d39a542 | 75 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 76 | |
Kojto | 118:6635230e06ba | 77 | if (_tid != 0) { |
Kojto | 119:19af2d39a542 | 78 | _mutex.unlock(); |
Kojto | 118:6635230e06ba | 79 | return osErrorParameter; |
Kojto | 118:6635230e06ba | 80 | } |
Kojto | 118:6635230e06ba | 81 | |
Kojto | 118:6635230e06ba | 82 | #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM) |
Kojto | 119:19af2d39a542 | 83 | _thread_def.pthread = Thread::_thunk; |
Kojto | 118:6635230e06ba | 84 | if (_thread_def.stack_pointer == NULL) { |
Kojto | 118:6635230e06ba | 85 | _thread_def.stack_pointer = new uint32_t[_thread_def.stacksize/sizeof(uint32_t)]; |
c1728p9 | 123:58563e6cba1e | 86 | MBED_ASSERT(_thread_def.stack_pointer != NULL); |
emilmont | 8:88a1a9c26ae3 | 87 | } |
Kojto | 118:6635230e06ba | 88 | |
mbed_official | 84:143955ffb790 | 89 | //Fill the stack with a magic word for maximum usage checking |
Kojto | 118:6635230e06ba | 90 | for (uint32_t i = 0; i < (_thread_def.stacksize / sizeof(uint32_t)); i++) { |
mbed_official | 84:143955ffb790 | 91 | _thread_def.stack_pointer[i] = 0xE25A2EA5; |
mbed_official | 84:143955ffb790 | 92 | } |
emilmont | 8:88a1a9c26ae3 | 93 | #endif |
Kojto | 118:6635230e06ba | 94 | _task = task; |
Kojto | 119:19af2d39a542 | 95 | _tid = osThreadCreate(&_thread_def, this); |
Kojto | 118:6635230e06ba | 96 | if (_tid == NULL) { |
c1728p9 | 123:58563e6cba1e | 97 | if (_dynamic_stack) { |
c1728p9 | 123:58563e6cba1e | 98 | delete[] (_thread_def.stack_pointer); |
c1728p9 | 123:58563e6cba1e | 99 | _thread_def.stack_pointer = (uint32_t*)NULL; |
c1728p9 | 123:58563e6cba1e | 100 | } |
Kojto | 119:19af2d39a542 | 101 | _mutex.unlock(); |
c1728p9 | 123:58563e6cba1e | 102 | _join_sem.release(); |
Kojto | 118:6635230e06ba | 103 | return osErrorResource; |
Kojto | 118:6635230e06ba | 104 | } |
Kojto | 119:19af2d39a542 | 105 | |
Kojto | 119:19af2d39a542 | 106 | _mutex.unlock(); |
Kojto | 118:6635230e06ba | 107 | return osOK; |
emilmont | 8:88a1a9c26ae3 | 108 | } |
emilmont | 8:88a1a9c26ae3 | 109 | |
emilmont | 8:88a1a9c26ae3 | 110 | osStatus Thread::terminate() { |
Kojto | 119:19af2d39a542 | 111 | osStatus ret; |
Kojto | 119:19af2d39a542 | 112 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 113 | |
c1728p9 | 123:58563e6cba1e | 114 | // Set the Thread's tid to NULL and |
c1728p9 | 123:58563e6cba1e | 115 | // release the semaphore before terminating |
c1728p9 | 123:58563e6cba1e | 116 | // since this thread could be terminating itself |
c1728p9 | 123:58563e6cba1e | 117 | osThreadId local_id = _tid; |
c1728p9 | 123:58563e6cba1e | 118 | _join_sem.release(); |
Kojto | 119:19af2d39a542 | 119 | _tid = (osThreadId)NULL; |
Kojto | 119:19af2d39a542 | 120 | |
c1728p9 | 123:58563e6cba1e | 121 | ret = osThreadTerminate(local_id); |
Kojto | 119:19af2d39a542 | 122 | |
Kojto | 119:19af2d39a542 | 123 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 124 | return ret; |
emilmont | 8:88a1a9c26ae3 | 125 | } |
emilmont | 8:88a1a9c26ae3 | 126 | |
Kojto | 118:6635230e06ba | 127 | osStatus Thread::join() { |
Kojto | 119:19af2d39a542 | 128 | int32_t ret = _join_sem.wait(); |
Kojto | 119:19af2d39a542 | 129 | if (ret < 0) { |
Kojto | 119:19af2d39a542 | 130 | return osErrorOS; |
Kojto | 118:6635230e06ba | 131 | } |
c1728p9 | 123:58563e6cba1e | 132 | |
c1728p9 | 123:58563e6cba1e | 133 | // The semaphore has been released so this thread is being |
c1728p9 | 123:58563e6cba1e | 134 | // terminated or has been terminated. Once the mutex has |
c1728p9 | 123:58563e6cba1e | 135 | // been locked it is ensured that the thread is deleted. |
c1728p9 | 123:58563e6cba1e | 136 | _mutex.lock(); |
c1728p9 | 123:58563e6cba1e | 137 | MBED_ASSERT(NULL == _tid); |
c1728p9 | 123:58563e6cba1e | 138 | _mutex.unlock(); |
c1728p9 | 123:58563e6cba1e | 139 | |
Kojto | 119:19af2d39a542 | 140 | // Release sem so any other threads joining this thread wake up |
Kojto | 119:19af2d39a542 | 141 | _join_sem.release(); |
Kojto | 119:19af2d39a542 | 142 | return osOK; |
Kojto | 118:6635230e06ba | 143 | } |
Kojto | 118:6635230e06ba | 144 | |
emilmont | 8:88a1a9c26ae3 | 145 | osStatus Thread::set_priority(osPriority priority) { |
Kojto | 119:19af2d39a542 | 146 | osStatus ret; |
Kojto | 119:19af2d39a542 | 147 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 148 | |
Kojto | 119:19af2d39a542 | 149 | ret = osThreadSetPriority(_tid, priority); |
Kojto | 119:19af2d39a542 | 150 | |
Kojto | 119:19af2d39a542 | 151 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 152 | return ret; |
emilmont | 8:88a1a9c26ae3 | 153 | } |
emilmont | 8:88a1a9c26ae3 | 154 | |
emilmont | 8:88a1a9c26ae3 | 155 | osPriority Thread::get_priority() { |
Kojto | 119:19af2d39a542 | 156 | osPriority ret; |
Kojto | 119:19af2d39a542 | 157 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 158 | |
Kojto | 119:19af2d39a542 | 159 | ret = osThreadGetPriority(_tid); |
Kojto | 119:19af2d39a542 | 160 | |
Kojto | 119:19af2d39a542 | 161 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 162 | return ret; |
emilmont | 8:88a1a9c26ae3 | 163 | } |
emilmont | 8:88a1a9c26ae3 | 164 | |
emilmont | 8:88a1a9c26ae3 | 165 | int32_t Thread::signal_set(int32_t signals) { |
Kojto | 119:19af2d39a542 | 166 | // osSignalSet is thread safe as long as the underlying |
Kojto | 119:19af2d39a542 | 167 | // thread does not get terminated or return from main |
emilmont | 8:88a1a9c26ae3 | 168 | return osSignalSet(_tid, signals); |
emilmont | 8:88a1a9c26ae3 | 169 | } |
emilmont | 8:88a1a9c26ae3 | 170 | |
mbed_official | 76:85a52b7ef44b | 171 | int32_t Thread::signal_clr(int32_t signals) { |
Kojto | 119:19af2d39a542 | 172 | // osSignalClear is thread safe as long as the underlying |
Kojto | 119:19af2d39a542 | 173 | // thread does not get terminated or return from main |
mbed_official | 76:85a52b7ef44b | 174 | return osSignalClear(_tid, signals); |
mbed_official | 76:85a52b7ef44b | 175 | } |
mbed_official | 76:85a52b7ef44b | 176 | |
emilmont | 8:88a1a9c26ae3 | 177 | Thread::State Thread::get_state() { |
mbed_official | 112:53ace74b190c | 178 | #if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM) |
mbed_official | 112:53ace74b190c | 179 | #ifdef CMSIS_OS_RTX |
Kojto | 119:19af2d39a542 | 180 | State status = Deleted; |
Kojto | 119:19af2d39a542 | 181 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 182 | |
Kojto | 119:19af2d39a542 | 183 | if (_tid != NULL) { |
Kojto | 119:19af2d39a542 | 184 | status = (State)_thread_def.tcb.state; |
Kojto | 119:19af2d39a542 | 185 | } |
Kojto | 119:19af2d39a542 | 186 | |
Kojto | 119:19af2d39a542 | 187 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 188 | return status; |
mbed_official | 112:53ace74b190c | 189 | #endif |
mbed_official | 48:e9a2c7cb57a4 | 190 | #else |
Kojto | 119:19af2d39a542 | 191 | State status = Deleted; |
Kojto | 119:19af2d39a542 | 192 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 193 | |
Kojto | 119:19af2d39a542 | 194 | if (_tid != NULL) { |
Kojto | 119:19af2d39a542 | 195 | status = (State)osThreadGetState(_tid); |
Kojto | 119:19af2d39a542 | 196 | } |
Kojto | 119:19af2d39a542 | 197 | |
Kojto | 119:19af2d39a542 | 198 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 199 | return status; |
mbed_official | 48:e9a2c7cb57a4 | 200 | #endif |
emilmont | 8:88a1a9c26ae3 | 201 | } |
emilmont | 8:88a1a9c26ae3 | 202 | |
mbed_official | 84:143955ffb790 | 203 | uint32_t Thread::stack_size() { |
mbed_official | 84:143955ffb790 | 204 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbed_official | 112:53ace74b190c | 205 | #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) |
Kojto | 119:19af2d39a542 | 206 | uint32_t size = 0; |
Kojto | 119:19af2d39a542 | 207 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 208 | |
Kojto | 119:19af2d39a542 | 209 | if (_tid != NULL) { |
Kojto | 119:19af2d39a542 | 210 | size = _thread_def.tcb.priv_stack; |
Kojto | 119:19af2d39a542 | 211 | } |
Kojto | 119:19af2d39a542 | 212 | |
Kojto | 119:19af2d39a542 | 213 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 214 | return size; |
mbed_official | 84:143955ffb790 | 215 | #else |
Kojto | 119:19af2d39a542 | 216 | uint32_t size = 0; |
Kojto | 119:19af2d39a542 | 217 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 218 | |
Kojto | 119:19af2d39a542 | 219 | if (_tid != NULL) { |
Kojto | 119:19af2d39a542 | 220 | P_TCB tcb = rt_tid2ptcb(_tid); |
Kojto | 119:19af2d39a542 | 221 | size = tcb->priv_stack; |
Kojto | 119:19af2d39a542 | 222 | } |
Kojto | 119:19af2d39a542 | 223 | |
Kojto | 119:19af2d39a542 | 224 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 225 | return size; |
mbed_official | 112:53ace74b190c | 226 | #endif |
mbed_official | 112:53ace74b190c | 227 | #else |
mbed_official | 84:143955ffb790 | 228 | return 0; |
mbed_official | 84:143955ffb790 | 229 | #endif |
mbed_official | 84:143955ffb790 | 230 | } |
mbed_official | 84:143955ffb790 | 231 | |
mbed_official | 84:143955ffb790 | 232 | uint32_t Thread::free_stack() { |
mbed_official | 84:143955ffb790 | 233 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbed_official | 112:53ace74b190c | 234 | #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) |
Kojto | 119:19af2d39a542 | 235 | uint32_t size = 0; |
Kojto | 119:19af2d39a542 | 236 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 237 | |
Kojto | 119:19af2d39a542 | 238 | if (_tid != NULL) { |
Kojto | 119:19af2d39a542 | 239 | uint32_t bottom = (uint32_t)_thread_def.tcb.stack; |
Kojto | 119:19af2d39a542 | 240 | size = _thread_def.tcb.tsk_stack - bottom; |
Kojto | 119:19af2d39a542 | 241 | } |
Kojto | 119:19af2d39a542 | 242 | |
Kojto | 119:19af2d39a542 | 243 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 244 | return size; |
mbed_official | 84:143955ffb790 | 245 | #else |
Kojto | 119:19af2d39a542 | 246 | uint32_t size = 0; |
Kojto | 119:19af2d39a542 | 247 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 248 | |
Kojto | 119:19af2d39a542 | 249 | if (_tid != NULL) { |
Kojto | 119:19af2d39a542 | 250 | P_TCB tcb = rt_tid2ptcb(_tid); |
Kojto | 119:19af2d39a542 | 251 | uint32_t bottom = (uint32_t)tcb->stack; |
Kojto | 119:19af2d39a542 | 252 | size = tcb->tsk_stack - bottom; |
Kojto | 119:19af2d39a542 | 253 | } |
Kojto | 119:19af2d39a542 | 254 | |
Kojto | 119:19af2d39a542 | 255 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 256 | return size; |
mbed_official | 112:53ace74b190c | 257 | #endif |
mbed_official | 112:53ace74b190c | 258 | #else |
mbed_official | 84:143955ffb790 | 259 | return 0; |
mbed_official | 84:143955ffb790 | 260 | #endif |
mbed_official | 84:143955ffb790 | 261 | } |
mbed_official | 84:143955ffb790 | 262 | |
mbed_official | 84:143955ffb790 | 263 | uint32_t Thread::used_stack() { |
mbed_official | 84:143955ffb790 | 264 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbed_official | 112:53ace74b190c | 265 | #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) |
Kojto | 119:19af2d39a542 | 266 | uint32_t size = 0; |
Kojto | 119:19af2d39a542 | 267 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 268 | |
Kojto | 119:19af2d39a542 | 269 | if (_tid != NULL) { |
Kojto | 119:19af2d39a542 | 270 | uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack; |
Kojto | 119:19af2d39a542 | 271 | size = top - _thread_def.tcb.tsk_stack; |
Kojto | 119:19af2d39a542 | 272 | } |
Kojto | 119:19af2d39a542 | 273 | |
Kojto | 119:19af2d39a542 | 274 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 275 | return size; |
mbed_official | 84:143955ffb790 | 276 | #else |
Kojto | 119:19af2d39a542 | 277 | uint32_t size = 0; |
Kojto | 119:19af2d39a542 | 278 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 279 | |
Kojto | 119:19af2d39a542 | 280 | if (_tid != NULL) { |
Kojto | 119:19af2d39a542 | 281 | P_TCB tcb = rt_tid2ptcb(_tid); |
Kojto | 119:19af2d39a542 | 282 | uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack; |
Kojto | 119:19af2d39a542 | 283 | size = top - tcb->tsk_stack; |
Kojto | 119:19af2d39a542 | 284 | } |
Kojto | 119:19af2d39a542 | 285 | |
Kojto | 119:19af2d39a542 | 286 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 287 | return size; |
mbed_official | 112:53ace74b190c | 288 | #endif |
mbed_official | 112:53ace74b190c | 289 | #else |
mbed_official | 84:143955ffb790 | 290 | return 0; |
mbed_official | 84:143955ffb790 | 291 | #endif |
mbed_official | 84:143955ffb790 | 292 | } |
mbed_official | 84:143955ffb790 | 293 | |
mbed_official | 84:143955ffb790 | 294 | uint32_t Thread::max_stack() { |
mbed_official | 84:143955ffb790 | 295 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbed_official | 112:53ace74b190c | 296 | #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) |
Kojto | 119:19af2d39a542 | 297 | uint32_t size = 0; |
Kojto | 119:19af2d39a542 | 298 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 299 | |
Kojto | 119:19af2d39a542 | 300 | if (_tid != NULL) { |
Kojto | 119:19af2d39a542 | 301 | uint32_t high_mark = 0; |
Kojto | 119:19af2d39a542 | 302 | while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5) |
Kojto | 119:19af2d39a542 | 303 | high_mark++; |
Kojto | 119:19af2d39a542 | 304 | size = _thread_def.tcb.priv_stack - (high_mark * 4); |
Kojto | 119:19af2d39a542 | 305 | } |
Kojto | 119:19af2d39a542 | 306 | |
Kojto | 119:19af2d39a542 | 307 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 308 | return size; |
mbed_official | 84:143955ffb790 | 309 | #else |
Kojto | 119:19af2d39a542 | 310 | uint32_t size = 0; |
Kojto | 119:19af2d39a542 | 311 | _mutex.lock(); |
Kojto | 119:19af2d39a542 | 312 | |
Kojto | 119:19af2d39a542 | 313 | if (_tid != NULL) { |
Kojto | 119:19af2d39a542 | 314 | P_TCB tcb = rt_tid2ptcb(_tid); |
Kojto | 119:19af2d39a542 | 315 | uint32_t high_mark = 0; |
Kojto | 119:19af2d39a542 | 316 | while (tcb->stack[high_mark] == 0xE25A2EA5) |
Kojto | 119:19af2d39a542 | 317 | high_mark++; |
Kojto | 119:19af2d39a542 | 318 | size = tcb->priv_stack - (high_mark * 4); |
Kojto | 119:19af2d39a542 | 319 | } |
Kojto | 119:19af2d39a542 | 320 | |
Kojto | 119:19af2d39a542 | 321 | _mutex.unlock(); |
Kojto | 119:19af2d39a542 | 322 | return size; |
mbed_official | 112:53ace74b190c | 323 | #endif |
mbed_official | 112:53ace74b190c | 324 | #else |
mbed_official | 84:143955ffb790 | 325 | return 0; |
mbed_official | 84:143955ffb790 | 326 | #endif |
mbed_official | 84:143955ffb790 | 327 | } |
mbed_official | 84:143955ffb790 | 328 | |
emilmont | 8:88a1a9c26ae3 | 329 | osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) { |
emilmont | 8:88a1a9c26ae3 | 330 | return osSignalWait(signals, millisec); |
emilmont | 8:88a1a9c26ae3 | 331 | } |
emilmont | 8:88a1a9c26ae3 | 332 | |
emilmont | 8:88a1a9c26ae3 | 333 | osStatus Thread::wait(uint32_t millisec) { |
emilmont | 8:88a1a9c26ae3 | 334 | return osDelay(millisec); |
emilmont | 8:88a1a9c26ae3 | 335 | } |
emilmont | 8:88a1a9c26ae3 | 336 | |
emilmont | 8:88a1a9c26ae3 | 337 | osStatus Thread::yield() { |
emilmont | 8:88a1a9c26ae3 | 338 | return osThreadYield(); |
emilmont | 8:88a1a9c26ae3 | 339 | } |
emilmont | 8:88a1a9c26ae3 | 340 | |
emilmont | 8:88a1a9c26ae3 | 341 | osThreadId Thread::gettid() { |
emilmont | 8:88a1a9c26ae3 | 342 | return osThreadGetId(); |
emilmont | 8:88a1a9c26ae3 | 343 | } |
emilmont | 8:88a1a9c26ae3 | 344 | |
mbed_official | 107:bdd541595fc5 | 345 | void Thread::attach_idle_hook(void (*fptr)(void)) { |
mbed_official | 107:bdd541595fc5 | 346 | rtos_attach_idle_hook(fptr); |
mbed_official | 107:bdd541595fc5 | 347 | } |
mbed_official | 107:bdd541595fc5 | 348 | |
c1728p9 | 123:58563e6cba1e | 349 | void Thread::attach_terminate_hook(void (*fptr)(osThreadId id)) { |
c1728p9 | 123:58563e6cba1e | 350 | terminate_hook = fptr; |
c1728p9 | 123:58563e6cba1e | 351 | } |
c1728p9 | 123:58563e6cba1e | 352 | |
emilmont | 8:88a1a9c26ae3 | 353 | Thread::~Thread() { |
Kojto | 119:19af2d39a542 | 354 | // terminate is thread safe |
emilmont | 8:88a1a9c26ae3 | 355 | terminate(); |
mbed_official | 112:53ace74b190c | 356 | #ifdef __MBED_CMSIS_RTOS_CM |
emilmont | 8:88a1a9c26ae3 | 357 | if (_dynamic_stack) { |
emilmont | 8:88a1a9c26ae3 | 358 | delete[] (_thread_def.stack_pointer); |
c1728p9 | 123:58563e6cba1e | 359 | _thread_def.stack_pointer = (uint32_t*)NULL; |
emilmont | 8:88a1a9c26ae3 | 360 | } |
mbed_official | 112:53ace74b190c | 361 | #endif |
emilmont | 8:88a1a9c26ae3 | 362 | } |
emilmont | 8:88a1a9c26ae3 | 363 | |
Kojto | 119:19af2d39a542 | 364 | void Thread::_thunk(const void * thread_ptr) |
Kojto | 119:19af2d39a542 | 365 | { |
Kojto | 119:19af2d39a542 | 366 | Thread *t = (Thread*)thread_ptr; |
Kojto | 119:19af2d39a542 | 367 | t->_task(); |
Kojto | 119:19af2d39a542 | 368 | t->_mutex.lock(); |
Kojto | 119:19af2d39a542 | 369 | t->_tid = (osThreadId)NULL; |
Kojto | 119:19af2d39a542 | 370 | t->_join_sem.release(); |
Kojto | 119:19af2d39a542 | 371 | // rtos will release the mutex automatically |
emilmont | 8:88a1a9c26ae3 | 372 | } |
Kojto | 119:19af2d39a542 | 373 | |
Kojto | 119:19af2d39a542 | 374 | } |