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