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