skeleton for lab1
Dependencies: AvailableMemory mbed-rtos mbed
Fork of helloaabbc by
mbed-rtos/rtos/Thread.cpp@1:55e99f6e2aa5, 2014-04-04 (annotated)
- Committer:
- mbed36372
- Date:
- Fri Apr 04 21:31:22 2014 +0000
- Revision:
- 1:55e99f6e2aa5
- Parent:
- 0:1c8f2727e9f5
SP14_lab1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
y7jin | 0:1c8f2727e9f5 | 1 | /* mbed Microcontroller Library |
y7jin | 0:1c8f2727e9f5 | 2 | * Copyright (c) 2006-2012 ARM Limited |
y7jin | 0:1c8f2727e9f5 | 3 | * |
y7jin | 0:1c8f2727e9f5 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
y7jin | 0:1c8f2727e9f5 | 5 | * of this software and associated documentation files (the "Software"), to deal |
y7jin | 0:1c8f2727e9f5 | 6 | * in the Software without restriction, including without limitation the rights |
y7jin | 0:1c8f2727e9f5 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
y7jin | 0:1c8f2727e9f5 | 8 | * copies of the Software, and to permit persons to whom the Software is |
y7jin | 0:1c8f2727e9f5 | 9 | * furnished to do so, subject to the following conditions: |
y7jin | 0:1c8f2727e9f5 | 10 | * |
y7jin | 0:1c8f2727e9f5 | 11 | * The above copyright notice and this permission notice shall be included in |
y7jin | 0:1c8f2727e9f5 | 12 | * all copies or substantial portions of the Software. |
y7jin | 0:1c8f2727e9f5 | 13 | * |
y7jin | 0:1c8f2727e9f5 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
y7jin | 0:1c8f2727e9f5 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
y7jin | 0:1c8f2727e9f5 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
y7jin | 0:1c8f2727e9f5 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
y7jin | 0:1c8f2727e9f5 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
y7jin | 0:1c8f2727e9f5 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
y7jin | 0:1c8f2727e9f5 | 20 | * SOFTWARE. |
y7jin | 0:1c8f2727e9f5 | 21 | */ |
y7jin | 0:1c8f2727e9f5 | 22 | #include "Thread.h" |
y7jin | 0:1c8f2727e9f5 | 23 | |
y7jin | 0:1c8f2727e9f5 | 24 | #include "error.h" |
y7jin | 0:1c8f2727e9f5 | 25 | |
y7jin | 0:1c8f2727e9f5 | 26 | namespace rtos { |
y7jin | 0:1c8f2727e9f5 | 27 | |
y7jin | 0:1c8f2727e9f5 | 28 | Thread::Thread(void (*task)(void const *argument), void *argument, |
y7jin | 0:1c8f2727e9f5 | 29 | osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { |
y7jin | 0:1c8f2727e9f5 | 30 | #ifdef CMSIS_OS_RTX |
y7jin | 0:1c8f2727e9f5 | 31 | _thread_def.pthread = task; |
y7jin | 0:1c8f2727e9f5 | 32 | _thread_def.tpriority = priority; |
y7jin | 0:1c8f2727e9f5 | 33 | _thread_def.stacksize = stack_size; |
y7jin | 0:1c8f2727e9f5 | 34 | if (stack_pointer != NULL) { |
y7jin | 0:1c8f2727e9f5 | 35 | _thread_def.stack_pointer = stack_pointer; |
y7jin | 0:1c8f2727e9f5 | 36 | _dynamic_stack = false; |
y7jin | 0:1c8f2727e9f5 | 37 | } else { |
y7jin | 0:1c8f2727e9f5 | 38 | _thread_def.stack_pointer = new unsigned char[stack_size]; |
y7jin | 0:1c8f2727e9f5 | 39 | if (_thread_def.stack_pointer == NULL) |
y7jin | 0:1c8f2727e9f5 | 40 | error("Error allocating the stack memory"); |
y7jin | 0:1c8f2727e9f5 | 41 | _dynamic_stack = true; |
y7jin | 0:1c8f2727e9f5 | 42 | } |
y7jin | 0:1c8f2727e9f5 | 43 | #endif |
y7jin | 0:1c8f2727e9f5 | 44 | _tid = osThreadCreate(&_thread_def, argument); |
y7jin | 0:1c8f2727e9f5 | 45 | } |
y7jin | 0:1c8f2727e9f5 | 46 | |
y7jin | 0:1c8f2727e9f5 | 47 | osStatus Thread::terminate() { |
y7jin | 0:1c8f2727e9f5 | 48 | return osThreadTerminate(_tid); |
y7jin | 0:1c8f2727e9f5 | 49 | } |
y7jin | 0:1c8f2727e9f5 | 50 | |
y7jin | 0:1c8f2727e9f5 | 51 | osStatus Thread::set_priority(osPriority priority) { |
y7jin | 0:1c8f2727e9f5 | 52 | return osThreadSetPriority(_tid, priority); |
y7jin | 0:1c8f2727e9f5 | 53 | } |
y7jin | 0:1c8f2727e9f5 | 54 | |
y7jin | 0:1c8f2727e9f5 | 55 | osPriority Thread::get_priority() { |
y7jin | 0:1c8f2727e9f5 | 56 | return osThreadGetPriority(_tid); |
y7jin | 0:1c8f2727e9f5 | 57 | } |
y7jin | 0:1c8f2727e9f5 | 58 | |
y7jin | 0:1c8f2727e9f5 | 59 | int32_t Thread::signal_set(int32_t signals) { |
y7jin | 0:1c8f2727e9f5 | 60 | return osSignalSet(_tid, signals); |
y7jin | 0:1c8f2727e9f5 | 61 | } |
y7jin | 0:1c8f2727e9f5 | 62 | |
y7jin | 0:1c8f2727e9f5 | 63 | Thread::State Thread::get_state() { |
y7jin | 0:1c8f2727e9f5 | 64 | return ((State)_thread_def.tcb.state); |
y7jin | 0:1c8f2727e9f5 | 65 | } |
y7jin | 0:1c8f2727e9f5 | 66 | |
y7jin | 0:1c8f2727e9f5 | 67 | osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) { |
y7jin | 0:1c8f2727e9f5 | 68 | return osSignalWait(signals, millisec); |
y7jin | 0:1c8f2727e9f5 | 69 | } |
y7jin | 0:1c8f2727e9f5 | 70 | |
y7jin | 0:1c8f2727e9f5 | 71 | osStatus Thread::wait(uint32_t millisec) { |
y7jin | 0:1c8f2727e9f5 | 72 | return osDelay(millisec); |
y7jin | 0:1c8f2727e9f5 | 73 | } |
y7jin | 0:1c8f2727e9f5 | 74 | |
y7jin | 0:1c8f2727e9f5 | 75 | osStatus Thread::yield() { |
y7jin | 0:1c8f2727e9f5 | 76 | return osThreadYield(); |
y7jin | 0:1c8f2727e9f5 | 77 | } |
y7jin | 0:1c8f2727e9f5 | 78 | |
y7jin | 0:1c8f2727e9f5 | 79 | osThreadId Thread::gettid() { |
y7jin | 0:1c8f2727e9f5 | 80 | return osThreadGetId(); |
y7jin | 0:1c8f2727e9f5 | 81 | } |
y7jin | 0:1c8f2727e9f5 | 82 | |
y7jin | 0:1c8f2727e9f5 | 83 | Thread::~Thread() { |
y7jin | 0:1c8f2727e9f5 | 84 | terminate(); |
y7jin | 0:1c8f2727e9f5 | 85 | if (_dynamic_stack) { |
y7jin | 0:1c8f2727e9f5 | 86 | delete[] (_thread_def.stack_pointer); |
y7jin | 0:1c8f2727e9f5 | 87 | } |
y7jin | 0:1c8f2727e9f5 | 88 | } |
y7jin | 0:1c8f2727e9f5 | 89 | |
y7jin | 0:1c8f2727e9f5 | 90 | } |