Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.
Fork of mbed-rtos by
rtos/Thread.cpp@111:de54bc598ef1, 2016-04-29 (annotated)
- Committer:
- acarter2
- Date:
- Fri Apr 29 14:04:02 2016 +0000
- Revision:
- 111:de54bc598ef1
current code 2;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
acarter2 | 111:de54bc598ef1 | 1 | /* mbed Microcontroller Library |
acarter2 | 111:de54bc598ef1 | 2 | * Copyright (c) 2006-2012 ARM Limited |
acarter2 | 111:de54bc598ef1 | 3 | * |
acarter2 | 111:de54bc598ef1 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
acarter2 | 111:de54bc598ef1 | 5 | * of this software and associated documentation files (the "Software"), to deal |
acarter2 | 111:de54bc598ef1 | 6 | * in the Software without restriction, including without limitation the rights |
acarter2 | 111:de54bc598ef1 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
acarter2 | 111:de54bc598ef1 | 8 | * copies of the Software, and to permit persons to whom the Software is |
acarter2 | 111:de54bc598ef1 | 9 | * furnished to do so, subject to the following conditions: |
acarter2 | 111:de54bc598ef1 | 10 | * |
acarter2 | 111:de54bc598ef1 | 11 | * The above copyright notice and this permission notice shall be included in |
acarter2 | 111:de54bc598ef1 | 12 | * all copies or substantial portions of the Software. |
acarter2 | 111:de54bc598ef1 | 13 | * |
acarter2 | 111:de54bc598ef1 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
acarter2 | 111:de54bc598ef1 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
acarter2 | 111:de54bc598ef1 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
acarter2 | 111:de54bc598ef1 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
acarter2 | 111:de54bc598ef1 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
acarter2 | 111:de54bc598ef1 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
acarter2 | 111:de54bc598ef1 | 20 | * SOFTWARE. |
acarter2 | 111:de54bc598ef1 | 21 | */ |
acarter2 | 111:de54bc598ef1 | 22 | #include "Thread.h" |
acarter2 | 111:de54bc598ef1 | 23 | |
acarter2 | 111:de54bc598ef1 | 24 | #include "mbed_error.h" |
acarter2 | 111:de54bc598ef1 | 25 | #include "rtos_idle.h" |
acarter2 | 111:de54bc598ef1 | 26 | |
acarter2 | 111:de54bc598ef1 | 27 | namespace rtos { |
acarter2 | 111:de54bc598ef1 | 28 | |
acarter2 | 111:de54bc598ef1 | 29 | Thread::Thread(void (*task)(void const *argument), void *argument, |
acarter2 | 111:de54bc598ef1 | 30 | osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { |
acarter2 | 111:de54bc598ef1 | 31 | #ifdef CMSIS_OS_RTX |
acarter2 | 111:de54bc598ef1 | 32 | _thread_def.pthread = task; |
acarter2 | 111:de54bc598ef1 | 33 | _thread_def.tpriority = priority; |
acarter2 | 111:de54bc598ef1 | 34 | _thread_def.stacksize = stack_size; |
acarter2 | 111:de54bc598ef1 | 35 | if (stack_pointer != NULL) { |
acarter2 | 111:de54bc598ef1 | 36 | _thread_def.stack_pointer = (uint32_t*)stack_pointer; |
acarter2 | 111:de54bc598ef1 | 37 | _dynamic_stack = false; |
acarter2 | 111:de54bc598ef1 | 38 | } else { |
acarter2 | 111:de54bc598ef1 | 39 | _thread_def.stack_pointer = new uint32_t[stack_size/sizeof(uint32_t)]; |
acarter2 | 111:de54bc598ef1 | 40 | if (_thread_def.stack_pointer == NULL) |
acarter2 | 111:de54bc598ef1 | 41 | error("Error allocating the stack memory\n"); |
acarter2 | 111:de54bc598ef1 | 42 | _dynamic_stack = true; |
acarter2 | 111:de54bc598ef1 | 43 | } |
acarter2 | 111:de54bc598ef1 | 44 | |
acarter2 | 111:de54bc598ef1 | 45 | //Fill the stack with a magic word for maximum usage checking |
acarter2 | 111:de54bc598ef1 | 46 | for (uint32_t i = 0; i < (stack_size / sizeof(uint32_t)); i++) { |
acarter2 | 111:de54bc598ef1 | 47 | _thread_def.stack_pointer[i] = 0xE25A2EA5; |
acarter2 | 111:de54bc598ef1 | 48 | } |
acarter2 | 111:de54bc598ef1 | 49 | #endif |
acarter2 | 111:de54bc598ef1 | 50 | _tid = osThreadCreate(&_thread_def, argument); |
acarter2 | 111:de54bc598ef1 | 51 | } |
acarter2 | 111:de54bc598ef1 | 52 | |
acarter2 | 111:de54bc598ef1 | 53 | osStatus Thread::terminate() { |
acarter2 | 111:de54bc598ef1 | 54 | return osThreadTerminate(_tid); |
acarter2 | 111:de54bc598ef1 | 55 | } |
acarter2 | 111:de54bc598ef1 | 56 | |
acarter2 | 111:de54bc598ef1 | 57 | osStatus Thread::set_priority(osPriority priority) { |
acarter2 | 111:de54bc598ef1 | 58 | return osThreadSetPriority(_tid, priority); |
acarter2 | 111:de54bc598ef1 | 59 | } |
acarter2 | 111:de54bc598ef1 | 60 | |
acarter2 | 111:de54bc598ef1 | 61 | osPriority Thread::get_priority() { |
acarter2 | 111:de54bc598ef1 | 62 | return osThreadGetPriority(_tid); |
acarter2 | 111:de54bc598ef1 | 63 | } |
acarter2 | 111:de54bc598ef1 | 64 | |
acarter2 | 111:de54bc598ef1 | 65 | int32_t Thread::signal_set(int32_t signals) { |
acarter2 | 111:de54bc598ef1 | 66 | return osSignalSet(_tid, signals); |
acarter2 | 111:de54bc598ef1 | 67 | } |
acarter2 | 111:de54bc598ef1 | 68 | |
acarter2 | 111:de54bc598ef1 | 69 | int32_t Thread::signal_clr(int32_t signals) { |
acarter2 | 111:de54bc598ef1 | 70 | return osSignalClear(_tid, signals); |
acarter2 | 111:de54bc598ef1 | 71 | } |
acarter2 | 111:de54bc598ef1 | 72 | |
acarter2 | 111:de54bc598ef1 | 73 | Thread::State Thread::get_state() { |
acarter2 | 111:de54bc598ef1 | 74 | #ifndef __MBED_CMSIS_RTOS_CA9 |
acarter2 | 111:de54bc598ef1 | 75 | return ((State)_thread_def.tcb.state); |
acarter2 | 111:de54bc598ef1 | 76 | #else |
acarter2 | 111:de54bc598ef1 | 77 | uint8_t status; |
acarter2 | 111:de54bc598ef1 | 78 | status = osThreadGetState(_tid); |
acarter2 | 111:de54bc598ef1 | 79 | return ((State)status); |
acarter2 | 111:de54bc598ef1 | 80 | #endif |
acarter2 | 111:de54bc598ef1 | 81 | } |
acarter2 | 111:de54bc598ef1 | 82 | |
acarter2 | 111:de54bc598ef1 | 83 | uint32_t Thread::stack_size() { |
acarter2 | 111:de54bc598ef1 | 84 | #ifndef __MBED_CMSIS_RTOS_CA9 |
acarter2 | 111:de54bc598ef1 | 85 | return _thread_def.tcb.priv_stack; |
acarter2 | 111:de54bc598ef1 | 86 | #else |
acarter2 | 111:de54bc598ef1 | 87 | return 0; |
acarter2 | 111:de54bc598ef1 | 88 | #endif |
acarter2 | 111:de54bc598ef1 | 89 | } |
acarter2 | 111:de54bc598ef1 | 90 | |
acarter2 | 111:de54bc598ef1 | 91 | uint32_t Thread::free_stack() { |
acarter2 | 111:de54bc598ef1 | 92 | #ifndef __MBED_CMSIS_RTOS_CA9 |
acarter2 | 111:de54bc598ef1 | 93 | uint32_t bottom = (uint32_t)_thread_def.tcb.stack; |
acarter2 | 111:de54bc598ef1 | 94 | return _thread_def.tcb.tsk_stack - bottom; |
acarter2 | 111:de54bc598ef1 | 95 | #else |
acarter2 | 111:de54bc598ef1 | 96 | return 0; |
acarter2 | 111:de54bc598ef1 | 97 | #endif |
acarter2 | 111:de54bc598ef1 | 98 | } |
acarter2 | 111:de54bc598ef1 | 99 | |
acarter2 | 111:de54bc598ef1 | 100 | uint32_t Thread::used_stack() { |
acarter2 | 111:de54bc598ef1 | 101 | #ifndef __MBED_CMSIS_RTOS_CA9 |
acarter2 | 111:de54bc598ef1 | 102 | uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack; |
acarter2 | 111:de54bc598ef1 | 103 | return top - _thread_def.tcb.tsk_stack; |
acarter2 | 111:de54bc598ef1 | 104 | #else |
acarter2 | 111:de54bc598ef1 | 105 | return 0; |
acarter2 | 111:de54bc598ef1 | 106 | #endif |
acarter2 | 111:de54bc598ef1 | 107 | } |
acarter2 | 111:de54bc598ef1 | 108 | |
acarter2 | 111:de54bc598ef1 | 109 | uint32_t Thread::max_stack() { |
acarter2 | 111:de54bc598ef1 | 110 | #ifndef __MBED_CMSIS_RTOS_CA9 |
acarter2 | 111:de54bc598ef1 | 111 | uint32_t high_mark = 0; |
acarter2 | 111:de54bc598ef1 | 112 | while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5) |
acarter2 | 111:de54bc598ef1 | 113 | high_mark++; |
acarter2 | 111:de54bc598ef1 | 114 | return _thread_def.tcb.priv_stack - (high_mark * 4); |
acarter2 | 111:de54bc598ef1 | 115 | #else |
acarter2 | 111:de54bc598ef1 | 116 | return 0; |
acarter2 | 111:de54bc598ef1 | 117 | #endif |
acarter2 | 111:de54bc598ef1 | 118 | } |
acarter2 | 111:de54bc598ef1 | 119 | |
acarter2 | 111:de54bc598ef1 | 120 | osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) { |
acarter2 | 111:de54bc598ef1 | 121 | return osSignalWait(signals, millisec); |
acarter2 | 111:de54bc598ef1 | 122 | } |
acarter2 | 111:de54bc598ef1 | 123 | |
acarter2 | 111:de54bc598ef1 | 124 | osStatus Thread::wait(uint32_t millisec) { |
acarter2 | 111:de54bc598ef1 | 125 | return osDelay(millisec); |
acarter2 | 111:de54bc598ef1 | 126 | } |
acarter2 | 111:de54bc598ef1 | 127 | |
acarter2 | 111:de54bc598ef1 | 128 | osStatus Thread::yield() { |
acarter2 | 111:de54bc598ef1 | 129 | return osThreadYield(); |
acarter2 | 111:de54bc598ef1 | 130 | } |
acarter2 | 111:de54bc598ef1 | 131 | |
acarter2 | 111:de54bc598ef1 | 132 | osThreadId Thread::gettid() { |
acarter2 | 111:de54bc598ef1 | 133 | return osThreadGetId(); |
acarter2 | 111:de54bc598ef1 | 134 | } |
acarter2 | 111:de54bc598ef1 | 135 | |
acarter2 | 111:de54bc598ef1 | 136 | void Thread::attach_idle_hook(void (*fptr)(void)) { |
acarter2 | 111:de54bc598ef1 | 137 | rtos_attach_idle_hook(fptr); |
acarter2 | 111:de54bc598ef1 | 138 | } |
acarter2 | 111:de54bc598ef1 | 139 | |
acarter2 | 111:de54bc598ef1 | 140 | Thread::~Thread() { |
acarter2 | 111:de54bc598ef1 | 141 | terminate(); |
acarter2 | 111:de54bc598ef1 | 142 | if (_dynamic_stack) { |
acarter2 | 111:de54bc598ef1 | 143 | delete[] (_thread_def.stack_pointer); |
acarter2 | 111:de54bc598ef1 | 144 | } |
acarter2 | 111:de54bc598ef1 | 145 | } |
acarter2 | 111:de54bc598ef1 | 146 | |
acarter2 | 111:de54bc598ef1 | 147 | } |