asd
Fork of mbed-rtos by
rtos/Thread.h@8:88a1a9c26ae3, 2012-11-27 (annotated)
- Committer:
- emilmont
- Date:
- Tue Nov 27 16:55:38 2012 +0000
- Revision:
- 8:88a1a9c26ae3
- Parent:
- 6:350b53afb889
- Child:
- 31:015df9e602b6
Add "Thread::get_state" method.; Update license.; Update documentation.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 8:88a1a9c26ae3 | 1 | /* mbed Microcontroller Library |
emilmont | 8:88a1a9c26ae3 | 2 | * Copyright (c) 2006-2012 ARM Limited |
emilmont | 8:88a1a9c26ae3 | 3 | * |
emilmont | 8:88a1a9c26ae3 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
emilmont | 8:88a1a9c26ae3 | 5 | * of this software and associated documentation files (the "Software"), to deal |
emilmont | 8:88a1a9c26ae3 | 6 | * in the Software without restriction, including without limitation the rights |
emilmont | 8:88a1a9c26ae3 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
emilmont | 8:88a1a9c26ae3 | 8 | * copies of the Software, and to permit persons to whom the Software is |
emilmont | 8:88a1a9c26ae3 | 9 | * furnished to do so, subject to the following conditions: |
emilmont | 8:88a1a9c26ae3 | 10 | * |
emilmont | 8:88a1a9c26ae3 | 11 | * The above copyright notice and this permission notice shall be included in |
emilmont | 8:88a1a9c26ae3 | 12 | * all copies or substantial portions of the Software. |
emilmont | 8:88a1a9c26ae3 | 13 | * |
emilmont | 8:88a1a9c26ae3 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
emilmont | 8:88a1a9c26ae3 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
emilmont | 8:88a1a9c26ae3 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
emilmont | 8:88a1a9c26ae3 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
emilmont | 8:88a1a9c26ae3 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emilmont | 8:88a1a9c26ae3 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
emilmont | 8:88a1a9c26ae3 | 20 | * SOFTWARE. |
emilmont | 8:88a1a9c26ae3 | 21 | */ |
emilmont | 6:350b53afb889 | 22 | #ifndef THREAD_H |
emilmont | 8:88a1a9c26ae3 | 23 | #define THREAD_H |
emilmont | 6:350b53afb889 | 24 | |
emilmont | 6:350b53afb889 | 25 | #include <stdint.h> |
emilmont | 6:350b53afb889 | 26 | #include "cmsis_os.h" |
emilmont | 6:350b53afb889 | 27 | |
emilmont | 6:350b53afb889 | 28 | namespace rtos { |
emilmont | 6:350b53afb889 | 29 | |
emilmont | 8:88a1a9c26ae3 | 30 | /** The Thread class allow defining, creating, and controlling thread functions in the system. */ |
emilmont | 6:350b53afb889 | 31 | class Thread { |
emilmont | 6:350b53afb889 | 32 | public: |
emilmont | 8:88a1a9c26ae3 | 33 | /** Create a new thread, and start it executing the specified function. |
emilmont | 8:88a1a9c26ae3 | 34 | @param task function to be executed by this thread. |
emilmont | 8:88a1a9c26ae3 | 35 | @param argument pointer that is passed to the thread function as start argument. (default: NULL). |
emilmont | 8:88a1a9c26ae3 | 36 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
emilmont | 8:88a1a9c26ae3 | 37 | @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). |
emilmont | 8:88a1a9c26ae3 | 38 | @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). |
emilmont | 6:350b53afb889 | 39 | */ |
emilmont | 6:350b53afb889 | 40 | Thread(void (*task)(void const *argument), void *argument=NULL, |
emilmont | 6:350b53afb889 | 41 | osPriority priority=osPriorityNormal, |
emilmont | 6:350b53afb889 | 42 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
emilmont | 6:350b53afb889 | 43 | unsigned char *stack_pointer=NULL); |
emilmont | 6:350b53afb889 | 44 | |
emilmont | 8:88a1a9c26ae3 | 45 | /** Terminate execution of a thread and remove it from Active Threads |
emilmont | 8:88a1a9c26ae3 | 46 | @return status code that indicates the execution status of the function. |
emilmont | 6:350b53afb889 | 47 | */ |
emilmont | 6:350b53afb889 | 48 | osStatus terminate(); |
emilmont | 6:350b53afb889 | 49 | |
emilmont | 8:88a1a9c26ae3 | 50 | /** Set priority of an active thread |
emilmont | 8:88a1a9c26ae3 | 51 | @param priority new priority value for the thread function. |
emilmont | 8:88a1a9c26ae3 | 52 | @return status code that indicates the execution status of the function. |
emilmont | 6:350b53afb889 | 53 | */ |
emilmont | 6:350b53afb889 | 54 | osStatus set_priority(osPriority priority); |
emilmont | 6:350b53afb889 | 55 | |
emilmont | 8:88a1a9c26ae3 | 56 | /** Get priority of an active thread |
emilmont | 8:88a1a9c26ae3 | 57 | @return current priority value of the thread function. |
emilmont | 6:350b53afb889 | 58 | */ |
emilmont | 6:350b53afb889 | 59 | osPriority get_priority(); |
emilmont | 6:350b53afb889 | 60 | |
emilmont | 8:88a1a9c26ae3 | 61 | /** Set the specified Signal Flags of an active thread. |
emilmont | 8:88a1a9c26ae3 | 62 | @param signals specifies the signal flags of the thread that should be set. |
emilmont | 8:88a1a9c26ae3 | 63 | @return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters. |
emilmont | 6:350b53afb889 | 64 | */ |
emilmont | 6:350b53afb889 | 65 | int32_t signal_set(int32_t signals); |
emilmont | 6:350b53afb889 | 66 | |
emilmont | 8:88a1a9c26ae3 | 67 | /** State of the Thread */ |
emilmont | 8:88a1a9c26ae3 | 68 | enum State { |
emilmont | 8:88a1a9c26ae3 | 69 | Inactive, /**< Not created or terminated */ |
emilmont | 8:88a1a9c26ae3 | 70 | Ready, /**< Ready to run */ |
emilmont | 8:88a1a9c26ae3 | 71 | Running, /**< Running */ |
emilmont | 8:88a1a9c26ae3 | 72 | WaitingDelay, /**< Waiting for a delay to occur */ |
emilmont | 8:88a1a9c26ae3 | 73 | WaitingInterval, /**< Waiting for an interval to occur */ |
emilmont | 8:88a1a9c26ae3 | 74 | WaitingOr, /**< Waiting for one event in a set to occur */ |
emilmont | 8:88a1a9c26ae3 | 75 | WaitingAnd, /**< Waiting for multiple events in a set to occur */ |
emilmont | 8:88a1a9c26ae3 | 76 | WaitingSemaphore, /**< Waiting for a semaphore event to occur */ |
emilmont | 8:88a1a9c26ae3 | 77 | WaitingMailbox, /**< Waiting for a mailbox event to occur */ |
emilmont | 8:88a1a9c26ae3 | 78 | WaitingMutex, /**< Waiting for a mutex event to occur */ |
emilmont | 8:88a1a9c26ae3 | 79 | }; |
emilmont | 8:88a1a9c26ae3 | 80 | |
emilmont | 8:88a1a9c26ae3 | 81 | /** State of this Thread |
emilmont | 8:88a1a9c26ae3 | 82 | @return the State of this Thread |
emilmont | 8:88a1a9c26ae3 | 83 | */ |
emilmont | 8:88a1a9c26ae3 | 84 | State get_state(); |
emilmont | 8:88a1a9c26ae3 | 85 | |
emilmont | 8:88a1a9c26ae3 | 86 | /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread. |
emilmont | 8:88a1a9c26ae3 | 87 | @param signals wait until all specified signal flags set or 0 for any single signal flag. |
emilmont | 8:88a1a9c26ae3 | 88 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). |
emilmont | 8:88a1a9c26ae3 | 89 | @return event flag information or error code. |
emilmont | 6:350b53afb889 | 90 | */ |
emilmont | 6:350b53afb889 | 91 | static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever); |
emilmont | 6:350b53afb889 | 92 | |
emilmont | 8:88a1a9c26ae3 | 93 | /** Wait for a specified time period in millisec: |
emilmont | 8:88a1a9c26ae3 | 94 | @param millisec time delay value |
emilmont | 8:88a1a9c26ae3 | 95 | @return status code that indicates the execution status of the function. |
emilmont | 6:350b53afb889 | 96 | */ |
emilmont | 6:350b53afb889 | 97 | static osStatus wait(uint32_t millisec); |
emilmont | 6:350b53afb889 | 98 | |
emilmont | 8:88a1a9c26ae3 | 99 | /** Pass control to next thread that is in state READY. |
emilmont | 8:88a1a9c26ae3 | 100 | @return status code that indicates the execution status of the function. |
emilmont | 6:350b53afb889 | 101 | */ |
emilmont | 6:350b53afb889 | 102 | static osStatus yield(); |
emilmont | 6:350b53afb889 | 103 | |
emilmont | 8:88a1a9c26ae3 | 104 | /** Get the thread id of the current running thread. |
emilmont | 8:88a1a9c26ae3 | 105 | @return thread ID for reference by other functions or NULL in case of error. |
emilmont | 6:350b53afb889 | 106 | */ |
emilmont | 6:350b53afb889 | 107 | static osThreadId gettid(); |
emilmont | 6:350b53afb889 | 108 | |
emilmont | 6:350b53afb889 | 109 | virtual ~Thread(); |
emilmont | 6:350b53afb889 | 110 | |
emilmont | 6:350b53afb889 | 111 | private: |
emilmont | 6:350b53afb889 | 112 | osThreadId _tid; |
emilmont | 6:350b53afb889 | 113 | osThreadDef_t _thread_def; |
emilmont | 6:350b53afb889 | 114 | bool _dynamic_stack; |
emilmont | 6:350b53afb889 | 115 | }; |
emilmont | 6:350b53afb889 | 116 | |
emilmont | 6:350b53afb889 | 117 | } |
emilmont | 6:350b53afb889 | 118 | #endif |