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