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