Simple Thread Class
Revision 0:c555ee16b8e7, committed 2013-09-13
- Comitter:
- cnhzcy14
- Date:
- Fri Sep 13 05:19:16 2013 +0000
- Commit message:
- Simple Thread Class
Changed in this revision
CyThread.cpp | Show annotated file Show diff for this revision Revisions of this file |
CyThread.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CyThread.cpp Fri Sep 13 05:19:16 2013 +0000 @@ -0,0 +1,75 @@ +#include "CyThread.h" +#include "error.h" +using namespace cnhzcy14; + +CyThread::CyThread() +{ +// _tid = -1; +// _thread_def = NULL; +// _dynamic_stack = false + +} + +void CyThread::init(void (*task)(void const *argument), void *argument, + osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { +#ifdef CMSIS_OS_RTX + osThreadId _tid; + _thread_def.pthread = task; + _thread_def.tpriority = priority; + _thread_def.stacksize = stack_size; + if (stack_pointer != NULL) { + _thread_def.stack_pointer = stack_pointer; + _dynamic_stack = false; + } else { + _thread_def.stack_pointer = new unsigned char[stack_size]; + if (_thread_def.stack_pointer == NULL) + error("Error allocating the stack memory"); + _dynamic_stack = true; + } +#endif + _tid = osThreadCreate(&_thread_def, argument); +} + +osStatus CyThread::terminate() { + return osThreadTerminate(osThreadGetId()); +} + +osStatus CyThread::set_priority(osPriority priority) { + return osThreadSetPriority(osThreadGetId(), priority); +} + +osPriority CyThread::get_priority() { + return osThreadGetPriority(osThreadGetId()); +} + +int32_t CyThread::signal_set(int32_t signals) { + return osSignalSet(osThreadGetId(), signals); +} + +//CyThread::State CyThread::get_state() { +// return ((State)_thread_def.tcb.state); +//} + +osEvent CyThread::signal_wait(int32_t signals, uint32_t millisec) { + return osSignalWait(signals, millisec); +} + +osStatus CyThread::wait(uint32_t millisec) { + return osDelay(millisec); +} + +osStatus CyThread::yield() { + return osThreadYield(); +} + +osThreadId CyThread::gettid() { + return osThreadGetId(); +} + +CyThread::~CyThread() { + terminate(); +// if (_dynamic_stack) { +// delete[] (_thread_def.stack_pointer); +// } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CyThread.h Fri Sep 13 05:19:16 2013 +0000 @@ -0,0 +1,102 @@ +#ifndef _THREAD_CNHZCY14_ +#define _THREAD_CNHZCY14_ + +#include <stdint.h> +#include "cmsis_os.h" + + +namespace cnhzcy14 +{ + +class CyThread +{ +public: + /** Create a new thread, and start it executing the specified function. + @param task function to be executed by this thread. + @param argument pointer that is passed to the thread function as start argument. (default: NULL). + @param priority initial priority of the thread function. (default: osPriorityNormal). + @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). + @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). + */ + CyThread(); + + void init(void (*task)(void const *argument), void *argument=NULL, + osPriority priority=osPriorityNormal, + uint32_t stack_size=DEFAULT_STACK_SIZE, + unsigned char *stack_pointer=NULL); + + /** Terminate execution of a thread and remove it from Active Threads + @return status code that indicates the execution status of the function. + */ + osStatus terminate(); + + /** Set priority of an active thread + @param priority new priority value for the thread function. + @return status code that indicates the execution status of the function. + */ + osStatus set_priority(osPriority priority); + + /** Get priority of an active thread + @return current priority value of the thread function. + */ + osPriority get_priority(); + + /** Set the specified Signal Flags of an active thread. + @param signals specifies the signal flags of the thread that should be set. + @return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters. + */ + int32_t signal_set(int32_t signals); + + /** State of the Thread */ + enum State { + Inactive, /**< Not created or terminated */ + Ready, /**< Ready to run */ + Running, /**< Running */ + WaitingDelay, /**< Waiting for a delay to occur */ + WaitingInterval, /**< Waiting for an interval to occur */ + WaitingOr, /**< Waiting for one event in a set to occur */ + WaitingAnd, /**< Waiting for multiple events in a set to occur */ + WaitingSemaphore, /**< Waiting for a semaphore event to occur */ + WaitingMailbox, /**< Waiting for a mailbox event to occur */ + WaitingMutex, /**< Waiting for a mutex event to occur */ + }; + + /** State of this Thread + @return the State of this Thread + */ +// State get_state(); + + /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread. + @param signals wait until all specified signal flags set or 0 for any single signal flag. + @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). + @return event flag information or error code. + */ + static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever); + + /** Wait for a specified time period in millisec: + @param millisec time delay value + @return status code that indicates the execution status of the function. + */ + static osStatus wait(uint32_t millisec); + + /** Pass control to next thread that is in state READY. + @return status code that indicates the execution status of the function. + */ + static osStatus yield(); + + /** Get the thread id of the current running thread. + @return thread ID for reference by other functions or NULL in case of error. + */ + static osThreadId gettid(); + + virtual ~CyThread(); + +private: +// osThreadId _tid; + osThreadDef_t _thread_def; + bool _dynamic_stack; +}; + +} + +#endif //_THREAD_CNHZCY14_ \ No newline at end of file