Fork of official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.
Dependents: Final_Coffee_Roaster
Fork of mbed-rtos by
rtos/Thread.cpp@6:350b53afb889, 2012-11-23 (annotated)
- Committer:
- emilmont
- Date:
- Fri Nov 23 09:57:31 2012 +0000
- Revision:
- 6:350b53afb889
- Child:
- 8:88a1a9c26ae3
Merge RTOS C++ API and RTX under the same library; Update RTX to version 4.60; Add proper Thread destructor;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 6:350b53afb889 | 1 | #include "Thread.h" |
emilmont | 6:350b53afb889 | 2 | |
emilmont | 6:350b53afb889 | 3 | #include "error.h" |
emilmont | 6:350b53afb889 | 4 | |
emilmont | 6:350b53afb889 | 5 | namespace rtos { |
emilmont | 6:350b53afb889 | 6 | |
emilmont | 6:350b53afb889 | 7 | Thread::Thread(void (*task)(void const *argument), void *argument, |
emilmont | 6:350b53afb889 | 8 | osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { |
emilmont | 6:350b53afb889 | 9 | #ifdef CMSIS_OS_RTX |
emilmont | 6:350b53afb889 | 10 | _thread_def.pthread = task; |
emilmont | 6:350b53afb889 | 11 | _thread_def.tpriority = priority; |
emilmont | 6:350b53afb889 | 12 | _thread_def.stacksize = stack_size; |
emilmont | 6:350b53afb889 | 13 | if (stack_pointer != NULL) { |
emilmont | 6:350b53afb889 | 14 | _thread_def.stack_pointer = stack_pointer; |
emilmont | 6:350b53afb889 | 15 | _dynamic_stack = false; |
emilmont | 6:350b53afb889 | 16 | } else { |
emilmont | 6:350b53afb889 | 17 | _thread_def.stack_pointer = new unsigned char[stack_size]; |
emilmont | 6:350b53afb889 | 18 | if (_thread_def.stack_pointer == NULL) |
emilmont | 6:350b53afb889 | 19 | error("Error allocating the stack memory"); |
emilmont | 6:350b53afb889 | 20 | _dynamic_stack = true; |
emilmont | 6:350b53afb889 | 21 | } |
emilmont | 6:350b53afb889 | 22 | #endif |
emilmont | 6:350b53afb889 | 23 | _tid = osThreadCreate(&_thread_def, argument); |
emilmont | 6:350b53afb889 | 24 | } |
emilmont | 6:350b53afb889 | 25 | |
emilmont | 6:350b53afb889 | 26 | osStatus Thread::terminate() { |
emilmont | 6:350b53afb889 | 27 | return osThreadTerminate(_tid); |
emilmont | 6:350b53afb889 | 28 | } |
emilmont | 6:350b53afb889 | 29 | |
emilmont | 6:350b53afb889 | 30 | osStatus Thread::set_priority(osPriority priority) { |
emilmont | 6:350b53afb889 | 31 | return osThreadSetPriority(_tid, priority); |
emilmont | 6:350b53afb889 | 32 | } |
emilmont | 6:350b53afb889 | 33 | |
emilmont | 6:350b53afb889 | 34 | osPriority Thread::get_priority() { |
emilmont | 6:350b53afb889 | 35 | return osThreadGetPriority(_tid); |
emilmont | 6:350b53afb889 | 36 | } |
emilmont | 6:350b53afb889 | 37 | |
emilmont | 6:350b53afb889 | 38 | int32_t Thread::signal_set(int32_t signals) { |
emilmont | 6:350b53afb889 | 39 | return osSignalSet(_tid, signals); |
emilmont | 6:350b53afb889 | 40 | } |
emilmont | 6:350b53afb889 | 41 | |
emilmont | 6:350b53afb889 | 42 | osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) { |
emilmont | 6:350b53afb889 | 43 | return osSignalWait(signals, millisec); |
emilmont | 6:350b53afb889 | 44 | } |
emilmont | 6:350b53afb889 | 45 | |
emilmont | 6:350b53afb889 | 46 | osStatus Thread::wait(uint32_t millisec) { |
emilmont | 6:350b53afb889 | 47 | return osDelay(millisec); |
emilmont | 6:350b53afb889 | 48 | } |
emilmont | 6:350b53afb889 | 49 | |
emilmont | 6:350b53afb889 | 50 | osStatus Thread::yield() { |
emilmont | 6:350b53afb889 | 51 | return osThreadYield(); |
emilmont | 6:350b53afb889 | 52 | } |
emilmont | 6:350b53afb889 | 53 | |
emilmont | 6:350b53afb889 | 54 | osThreadId Thread::gettid() { |
emilmont | 6:350b53afb889 | 55 | return osThreadGetId(); |
emilmont | 6:350b53afb889 | 56 | } |
emilmont | 6:350b53afb889 | 57 | |
emilmont | 6:350b53afb889 | 58 | Thread::~Thread() { |
emilmont | 6:350b53afb889 | 59 | terminate(); |
emilmont | 6:350b53afb889 | 60 | if (_dynamic_stack) { |
emilmont | 6:350b53afb889 | 61 | delete[] (_thread_def.stack_pointer); |
emilmont | 6:350b53afb889 | 62 | } |
emilmont | 6:350b53afb889 | 63 | } |
emilmont | 6:350b53afb889 | 64 | |
emilmont | 6:350b53afb889 | 65 | } |