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