Simple Thread Class
CyThread.h@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 | #ifndef _THREAD_CNHZCY14_ |
cnhzcy14 | 0:c555ee16b8e7 | 2 | #define _THREAD_CNHZCY14_ |
cnhzcy14 | 0:c555ee16b8e7 | 3 | |
cnhzcy14 | 0:c555ee16b8e7 | 4 | #include <stdint.h> |
cnhzcy14 | 0:c555ee16b8e7 | 5 | #include "cmsis_os.h" |
cnhzcy14 | 0:c555ee16b8e7 | 6 | |
cnhzcy14 | 0:c555ee16b8e7 | 7 | |
cnhzcy14 | 0:c555ee16b8e7 | 8 | namespace cnhzcy14 |
cnhzcy14 | 0:c555ee16b8e7 | 9 | { |
cnhzcy14 | 0:c555ee16b8e7 | 10 | |
cnhzcy14 | 0:c555ee16b8e7 | 11 | class CyThread |
cnhzcy14 | 0:c555ee16b8e7 | 12 | { |
cnhzcy14 | 0:c555ee16b8e7 | 13 | public: |
cnhzcy14 | 0:c555ee16b8e7 | 14 | /** Create a new thread, and start it executing the specified function. |
cnhzcy14 | 0:c555ee16b8e7 | 15 | @param task function to be executed by this thread. |
cnhzcy14 | 0:c555ee16b8e7 | 16 | @param argument pointer that is passed to the thread function as start argument. (default: NULL). |
cnhzcy14 | 0:c555ee16b8e7 | 17 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
cnhzcy14 | 0:c555ee16b8e7 | 18 | @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). |
cnhzcy14 | 0:c555ee16b8e7 | 19 | @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). |
cnhzcy14 | 0:c555ee16b8e7 | 20 | */ |
cnhzcy14 | 0:c555ee16b8e7 | 21 | CyThread(); |
cnhzcy14 | 0:c555ee16b8e7 | 22 | |
cnhzcy14 | 0:c555ee16b8e7 | 23 | void init(void (*task)(void const *argument), void *argument=NULL, |
cnhzcy14 | 0:c555ee16b8e7 | 24 | osPriority priority=osPriorityNormal, |
cnhzcy14 | 0:c555ee16b8e7 | 25 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
cnhzcy14 | 0:c555ee16b8e7 | 26 | unsigned char *stack_pointer=NULL); |
cnhzcy14 | 0:c555ee16b8e7 | 27 | |
cnhzcy14 | 0:c555ee16b8e7 | 28 | /** Terminate execution of a thread and remove it from Active Threads |
cnhzcy14 | 0:c555ee16b8e7 | 29 | @return status code that indicates the execution status of the function. |
cnhzcy14 | 0:c555ee16b8e7 | 30 | */ |
cnhzcy14 | 0:c555ee16b8e7 | 31 | osStatus terminate(); |
cnhzcy14 | 0:c555ee16b8e7 | 32 | |
cnhzcy14 | 0:c555ee16b8e7 | 33 | /** Set priority of an active thread |
cnhzcy14 | 0:c555ee16b8e7 | 34 | @param priority new priority value for the thread function. |
cnhzcy14 | 0:c555ee16b8e7 | 35 | @return status code that indicates the execution status of the function. |
cnhzcy14 | 0:c555ee16b8e7 | 36 | */ |
cnhzcy14 | 0:c555ee16b8e7 | 37 | osStatus set_priority(osPriority priority); |
cnhzcy14 | 0:c555ee16b8e7 | 38 | |
cnhzcy14 | 0:c555ee16b8e7 | 39 | /** Get priority of an active thread |
cnhzcy14 | 0:c555ee16b8e7 | 40 | @return current priority value of the thread function. |
cnhzcy14 | 0:c555ee16b8e7 | 41 | */ |
cnhzcy14 | 0:c555ee16b8e7 | 42 | osPriority get_priority(); |
cnhzcy14 | 0:c555ee16b8e7 | 43 | |
cnhzcy14 | 0:c555ee16b8e7 | 44 | /** Set the specified Signal Flags of an active thread. |
cnhzcy14 | 0:c555ee16b8e7 | 45 | @param signals specifies the signal flags of the thread that should be set. |
cnhzcy14 | 0:c555ee16b8e7 | 46 | @return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters. |
cnhzcy14 | 0:c555ee16b8e7 | 47 | */ |
cnhzcy14 | 0:c555ee16b8e7 | 48 | int32_t signal_set(int32_t signals); |
cnhzcy14 | 0:c555ee16b8e7 | 49 | |
cnhzcy14 | 0:c555ee16b8e7 | 50 | /** State of the Thread */ |
cnhzcy14 | 0:c555ee16b8e7 | 51 | enum State { |
cnhzcy14 | 0:c555ee16b8e7 | 52 | Inactive, /**< Not created or terminated */ |
cnhzcy14 | 0:c555ee16b8e7 | 53 | Ready, /**< Ready to run */ |
cnhzcy14 | 0:c555ee16b8e7 | 54 | Running, /**< Running */ |
cnhzcy14 | 0:c555ee16b8e7 | 55 | WaitingDelay, /**< Waiting for a delay to occur */ |
cnhzcy14 | 0:c555ee16b8e7 | 56 | WaitingInterval, /**< Waiting for an interval to occur */ |
cnhzcy14 | 0:c555ee16b8e7 | 57 | WaitingOr, /**< Waiting for one event in a set to occur */ |
cnhzcy14 | 0:c555ee16b8e7 | 58 | WaitingAnd, /**< Waiting for multiple events in a set to occur */ |
cnhzcy14 | 0:c555ee16b8e7 | 59 | WaitingSemaphore, /**< Waiting for a semaphore event to occur */ |
cnhzcy14 | 0:c555ee16b8e7 | 60 | WaitingMailbox, /**< Waiting for a mailbox event to occur */ |
cnhzcy14 | 0:c555ee16b8e7 | 61 | WaitingMutex, /**< Waiting for a mutex event to occur */ |
cnhzcy14 | 0:c555ee16b8e7 | 62 | }; |
cnhzcy14 | 0:c555ee16b8e7 | 63 | |
cnhzcy14 | 0:c555ee16b8e7 | 64 | /** State of this Thread |
cnhzcy14 | 0:c555ee16b8e7 | 65 | @return the State of this Thread |
cnhzcy14 | 0:c555ee16b8e7 | 66 | */ |
cnhzcy14 | 0:c555ee16b8e7 | 67 | // State get_state(); |
cnhzcy14 | 0:c555ee16b8e7 | 68 | |
cnhzcy14 | 0:c555ee16b8e7 | 69 | /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread. |
cnhzcy14 | 0:c555ee16b8e7 | 70 | @param signals wait until all specified signal flags set or 0 for any single signal flag. |
cnhzcy14 | 0:c555ee16b8e7 | 71 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). |
cnhzcy14 | 0:c555ee16b8e7 | 72 | @return event flag information or error code. |
cnhzcy14 | 0:c555ee16b8e7 | 73 | */ |
cnhzcy14 | 0:c555ee16b8e7 | 74 | static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever); |
cnhzcy14 | 0:c555ee16b8e7 | 75 | |
cnhzcy14 | 0:c555ee16b8e7 | 76 | /** Wait for a specified time period in millisec: |
cnhzcy14 | 0:c555ee16b8e7 | 77 | @param millisec time delay value |
cnhzcy14 | 0:c555ee16b8e7 | 78 | @return status code that indicates the execution status of the function. |
cnhzcy14 | 0:c555ee16b8e7 | 79 | */ |
cnhzcy14 | 0:c555ee16b8e7 | 80 | static osStatus wait(uint32_t millisec); |
cnhzcy14 | 0:c555ee16b8e7 | 81 | |
cnhzcy14 | 0:c555ee16b8e7 | 82 | /** Pass control to next thread that is in state READY. |
cnhzcy14 | 0:c555ee16b8e7 | 83 | @return status code that indicates the execution status of the function. |
cnhzcy14 | 0:c555ee16b8e7 | 84 | */ |
cnhzcy14 | 0:c555ee16b8e7 | 85 | static osStatus yield(); |
cnhzcy14 | 0:c555ee16b8e7 | 86 | |
cnhzcy14 | 0:c555ee16b8e7 | 87 | /** Get the thread id of the current running thread. |
cnhzcy14 | 0:c555ee16b8e7 | 88 | @return thread ID for reference by other functions or NULL in case of error. |
cnhzcy14 | 0:c555ee16b8e7 | 89 | */ |
cnhzcy14 | 0:c555ee16b8e7 | 90 | static osThreadId gettid(); |
cnhzcy14 | 0:c555ee16b8e7 | 91 | |
cnhzcy14 | 0:c555ee16b8e7 | 92 | virtual ~CyThread(); |
cnhzcy14 | 0:c555ee16b8e7 | 93 | |
cnhzcy14 | 0:c555ee16b8e7 | 94 | private: |
cnhzcy14 | 0:c555ee16b8e7 | 95 | // osThreadId _tid; |
cnhzcy14 | 0:c555ee16b8e7 | 96 | osThreadDef_t _thread_def; |
cnhzcy14 | 0:c555ee16b8e7 | 97 | bool _dynamic_stack; |
cnhzcy14 | 0:c555ee16b8e7 | 98 | }; |
cnhzcy14 | 0:c555ee16b8e7 | 99 | |
cnhzcy14 | 0:c555ee16b8e7 | 100 | } |
cnhzcy14 | 0:c555ee16b8e7 | 101 | |
cnhzcy14 | 0:c555ee16b8e7 | 102 | #endif //_THREAD_CNHZCY14_ |