]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/rtos/rtos/Thread.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / rtos / rtos / Thread.cpp
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2012 ARM Limited
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22 #include "Thread.h"
23
24 #include "mbed_error.h"
25
26 namespace rtos {
27
28 Thread::Thread(void (*task)(void const *argument), void *argument,
29 osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
30 #ifdef CMSIS_OS_RTX
31 _thread_def.pthread = task;
32 _thread_def.tpriority = priority;
33 _thread_def.stacksize = stack_size;
34 #ifndef __MBED_CMSIS_RTOS_CA9
35 if (stack_pointer != NULL) {
36 _thread_def.stack_pointer = (uint32_t*)stack_pointer;
37 _dynamic_stack = false;
38 } else {
39 _thread_def.stack_pointer = new uint32_t[stack_size/sizeof(uint32_t)];
40 if (_thread_def.stack_pointer == NULL)
41 error("Error allocating the stack memory\n");
42 _dynamic_stack = true;
43 }
44 #endif
45 #endif
46 _tid = osThreadCreate(&_thread_def, argument);
47 }
48
49 osStatus Thread::terminate() {
50 return osThreadTerminate(_tid);
51 }
52
53 osStatus Thread::set_priority(osPriority priority) {
54 return osThreadSetPriority(_tid, priority);
55 }
56
57 osPriority Thread::get_priority() {
58 return osThreadGetPriority(_tid);
59 }
60
61 int32_t Thread::signal_set(int32_t signals) {
62 return osSignalSet(_tid, signals);
63 }
64
65 Thread::State Thread::get_state() {
66 #ifndef __MBED_CMSIS_RTOS_CA9
67 return ((State)_thread_def.tcb.state);
68 #else
69 uint8_t status;
70 status = osThreadGetState(_tid);
71 return ((State)status);
72 #endif
73 }
74
75 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) {
76 return osSignalWait(signals, millisec);
77 }
78
79 osStatus Thread::wait(uint32_t millisec) {
80 return osDelay(millisec);
81 }
82
83 osStatus Thread::yield() {
84 return osThreadYield();
85 }
86
87 osThreadId Thread::gettid() {
88 return osThreadGetId();
89 }
90
91 Thread::~Thread() {
92 terminate();
93 #ifndef __MBED_CMSIS_RTOS_CA9
94 if (_dynamic_stack) {
95 delete[] (_thread_def.stack_pointer);
96 }
97 #endif
98 }
99
100 }
Imprint / Impressum