Code for controlling mbed hardware (LED's, motors), as well as code for the Raspberry Pi to run a Support Vector Machine that identifies objects using the Pi camera

Dependencies:   mbed Motordriver mbed-rtos PololuLedStrip

Committer:
arogliero3
Date:
Fri Dec 06 00:58:02 2019 -0500
Revision:
3:a3ed7ff99772
Parent:
0:e0dbd261724a
update img6

Who changed what in which revision?

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