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