Simple Thread Class
CyThread.cpp@0:c555ee16b8e7, 2013-09-13 (annotated)
- Committer:
- cnhzcy14
- Date:
- Fri Sep 13 05:19:16 2013 +0000
- Revision:
- 0:c555ee16b8e7
Simple Thread Class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cnhzcy14 | 0:c555ee16b8e7 | 1 | #include "CyThread.h" |
cnhzcy14 | 0:c555ee16b8e7 | 2 | #include "error.h" |
cnhzcy14 | 0:c555ee16b8e7 | 3 | using namespace cnhzcy14; |
cnhzcy14 | 0:c555ee16b8e7 | 4 | |
cnhzcy14 | 0:c555ee16b8e7 | 5 | CyThread::CyThread() |
cnhzcy14 | 0:c555ee16b8e7 | 6 | { |
cnhzcy14 | 0:c555ee16b8e7 | 7 | // _tid = -1; |
cnhzcy14 | 0:c555ee16b8e7 | 8 | // _thread_def = NULL; |
cnhzcy14 | 0:c555ee16b8e7 | 9 | // _dynamic_stack = false |
cnhzcy14 | 0:c555ee16b8e7 | 10 | |
cnhzcy14 | 0:c555ee16b8e7 | 11 | } |
cnhzcy14 | 0:c555ee16b8e7 | 12 | |
cnhzcy14 | 0:c555ee16b8e7 | 13 | void CyThread::init(void (*task)(void const *argument), void *argument, |
cnhzcy14 | 0:c555ee16b8e7 | 14 | osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { |
cnhzcy14 | 0:c555ee16b8e7 | 15 | #ifdef CMSIS_OS_RTX |
cnhzcy14 | 0:c555ee16b8e7 | 16 | osThreadId _tid; |
cnhzcy14 | 0:c555ee16b8e7 | 17 | _thread_def.pthread = task; |
cnhzcy14 | 0:c555ee16b8e7 | 18 | _thread_def.tpriority = priority; |
cnhzcy14 | 0:c555ee16b8e7 | 19 | _thread_def.stacksize = stack_size; |
cnhzcy14 | 0:c555ee16b8e7 | 20 | if (stack_pointer != NULL) { |
cnhzcy14 | 0:c555ee16b8e7 | 21 | _thread_def.stack_pointer = stack_pointer; |
cnhzcy14 | 0:c555ee16b8e7 | 22 | _dynamic_stack = false; |
cnhzcy14 | 0:c555ee16b8e7 | 23 | } else { |
cnhzcy14 | 0:c555ee16b8e7 | 24 | _thread_def.stack_pointer = new unsigned char[stack_size]; |
cnhzcy14 | 0:c555ee16b8e7 | 25 | if (_thread_def.stack_pointer == NULL) |
cnhzcy14 | 0:c555ee16b8e7 | 26 | error("Error allocating the stack memory"); |
cnhzcy14 | 0:c555ee16b8e7 | 27 | _dynamic_stack = true; |
cnhzcy14 | 0:c555ee16b8e7 | 28 | } |
cnhzcy14 | 0:c555ee16b8e7 | 29 | #endif |
cnhzcy14 | 0:c555ee16b8e7 | 30 | _tid = osThreadCreate(&_thread_def, argument); |
cnhzcy14 | 0:c555ee16b8e7 | 31 | } |
cnhzcy14 | 0:c555ee16b8e7 | 32 | |
cnhzcy14 | 0:c555ee16b8e7 | 33 | osStatus CyThread::terminate() { |
cnhzcy14 | 0:c555ee16b8e7 | 34 | return osThreadTerminate(osThreadGetId()); |
cnhzcy14 | 0:c555ee16b8e7 | 35 | } |
cnhzcy14 | 0:c555ee16b8e7 | 36 | |
cnhzcy14 | 0:c555ee16b8e7 | 37 | osStatus CyThread::set_priority(osPriority priority) { |
cnhzcy14 | 0:c555ee16b8e7 | 38 | return osThreadSetPriority(osThreadGetId(), priority); |
cnhzcy14 | 0:c555ee16b8e7 | 39 | } |
cnhzcy14 | 0:c555ee16b8e7 | 40 | |
cnhzcy14 | 0:c555ee16b8e7 | 41 | osPriority CyThread::get_priority() { |
cnhzcy14 | 0:c555ee16b8e7 | 42 | return osThreadGetPriority(osThreadGetId()); |
cnhzcy14 | 0:c555ee16b8e7 | 43 | } |
cnhzcy14 | 0:c555ee16b8e7 | 44 | |
cnhzcy14 | 0:c555ee16b8e7 | 45 | int32_t CyThread::signal_set(int32_t signals) { |
cnhzcy14 | 0:c555ee16b8e7 | 46 | return osSignalSet(osThreadGetId(), signals); |
cnhzcy14 | 0:c555ee16b8e7 | 47 | } |
cnhzcy14 | 0:c555ee16b8e7 | 48 | |
cnhzcy14 | 0:c555ee16b8e7 | 49 | //CyThread::State CyThread::get_state() { |
cnhzcy14 | 0:c555ee16b8e7 | 50 | // return ((State)_thread_def.tcb.state); |
cnhzcy14 | 0:c555ee16b8e7 | 51 | //} |
cnhzcy14 | 0:c555ee16b8e7 | 52 | |
cnhzcy14 | 0:c555ee16b8e7 | 53 | osEvent CyThread::signal_wait(int32_t signals, uint32_t millisec) { |
cnhzcy14 | 0:c555ee16b8e7 | 54 | return osSignalWait(signals, millisec); |
cnhzcy14 | 0:c555ee16b8e7 | 55 | } |
cnhzcy14 | 0:c555ee16b8e7 | 56 | |
cnhzcy14 | 0:c555ee16b8e7 | 57 | osStatus CyThread::wait(uint32_t millisec) { |
cnhzcy14 | 0:c555ee16b8e7 | 58 | return osDelay(millisec); |
cnhzcy14 | 0:c555ee16b8e7 | 59 | } |
cnhzcy14 | 0:c555ee16b8e7 | 60 | |
cnhzcy14 | 0:c555ee16b8e7 | 61 | osStatus CyThread::yield() { |
cnhzcy14 | 0:c555ee16b8e7 | 62 | return osThreadYield(); |
cnhzcy14 | 0:c555ee16b8e7 | 63 | } |
cnhzcy14 | 0:c555ee16b8e7 | 64 | |
cnhzcy14 | 0:c555ee16b8e7 | 65 | osThreadId CyThread::gettid() { |
cnhzcy14 | 0:c555ee16b8e7 | 66 | return osThreadGetId(); |
cnhzcy14 | 0:c555ee16b8e7 | 67 | } |
cnhzcy14 | 0:c555ee16b8e7 | 68 | |
cnhzcy14 | 0:c555ee16b8e7 | 69 | CyThread::~CyThread() { |
cnhzcy14 | 0:c555ee16b8e7 | 70 | terminate(); |
cnhzcy14 | 0:c555ee16b8e7 | 71 | // if (_dynamic_stack) { |
cnhzcy14 | 0:c555ee16b8e7 | 72 | // delete[] (_thread_def.stack_pointer); |
cnhzcy14 | 0:c555ee16b8e7 | 73 | // } |
cnhzcy14 | 0:c555ee16b8e7 | 74 | } |
cnhzcy14 | 0:c555ee16b8e7 | 75 |