help :(
Dependencies: FFT
mbed-rtos/rtos/Thread.h@0:d6c9b09b4042, 2020-12-02 (annotated)
- Committer:
- annieluo2
- Date:
- Wed Dec 02 18:02:03 2020 +0000
- Revision:
- 0:d6c9b09b4042
boo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
annieluo2 | 0:d6c9b09b4042 | 1 | /* mbed Microcontroller Library |
annieluo2 | 0:d6c9b09b4042 | 2 | * Copyright (c) 2006-2012 ARM Limited |
annieluo2 | 0:d6c9b09b4042 | 3 | * |
annieluo2 | 0:d6c9b09b4042 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
annieluo2 | 0:d6c9b09b4042 | 5 | * of this software and associated documentation files (the "Software"), to deal |
annieluo2 | 0:d6c9b09b4042 | 6 | * in the Software without restriction, including without limitation the rights |
annieluo2 | 0:d6c9b09b4042 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
annieluo2 | 0:d6c9b09b4042 | 8 | * copies of the Software, and to permit persons to whom the Software is |
annieluo2 | 0:d6c9b09b4042 | 9 | * furnished to do so, subject to the following conditions: |
annieluo2 | 0:d6c9b09b4042 | 10 | * |
annieluo2 | 0:d6c9b09b4042 | 11 | * The above copyright notice and this permission notice shall be included in |
annieluo2 | 0:d6c9b09b4042 | 12 | * all copies or substantial portions of the Software. |
annieluo2 | 0:d6c9b09b4042 | 13 | * |
annieluo2 | 0:d6c9b09b4042 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
annieluo2 | 0:d6c9b09b4042 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
annieluo2 | 0:d6c9b09b4042 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
annieluo2 | 0:d6c9b09b4042 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
annieluo2 | 0:d6c9b09b4042 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
annieluo2 | 0:d6c9b09b4042 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
annieluo2 | 0:d6c9b09b4042 | 20 | * SOFTWARE. |
annieluo2 | 0:d6c9b09b4042 | 21 | */ |
annieluo2 | 0:d6c9b09b4042 | 22 | #ifndef THREAD_H |
annieluo2 | 0:d6c9b09b4042 | 23 | #define THREAD_H |
annieluo2 | 0:d6c9b09b4042 | 24 | |
annieluo2 | 0:d6c9b09b4042 | 25 | #include <stdint.h> |
annieluo2 | 0:d6c9b09b4042 | 26 | #include "cmsis_os.h" |
annieluo2 | 0:d6c9b09b4042 | 27 | #include "platform/Callback.h" |
annieluo2 | 0:d6c9b09b4042 | 28 | #include "platform/toolchain.h" |
annieluo2 | 0:d6c9b09b4042 | 29 | #include "rtos/Semaphore.h" |
annieluo2 | 0:d6c9b09b4042 | 30 | #include "rtos/Mutex.h" |
annieluo2 | 0:d6c9b09b4042 | 31 | |
annieluo2 | 0:d6c9b09b4042 | 32 | namespace rtos { |
annieluo2 | 0:d6c9b09b4042 | 33 | /** \addtogroup rtos */ |
annieluo2 | 0:d6c9b09b4042 | 34 | /** @{*/ |
annieluo2 | 0:d6c9b09b4042 | 35 | |
annieluo2 | 0:d6c9b09b4042 | 36 | /** The Thread class allow defining, creating, and controlling thread functions in the system. |
annieluo2 | 0:d6c9b09b4042 | 37 | * |
annieluo2 | 0:d6c9b09b4042 | 38 | * Example: |
annieluo2 | 0:d6c9b09b4042 | 39 | * @code |
annieluo2 | 0:d6c9b09b4042 | 40 | * #include "mbed.h" |
annieluo2 | 0:d6c9b09b4042 | 41 | * #include "rtos.h" |
annieluo2 | 0:d6c9b09b4042 | 42 | * |
annieluo2 | 0:d6c9b09b4042 | 43 | * Thread thread; |
annieluo2 | 0:d6c9b09b4042 | 44 | * DigitalOut led1(LED1); |
annieluo2 | 0:d6c9b09b4042 | 45 | * volatile bool running = true; |
annieluo2 | 0:d6c9b09b4042 | 46 | * |
annieluo2 | 0:d6c9b09b4042 | 47 | * // Blink function toggles the led in a long running loop |
annieluo2 | 0:d6c9b09b4042 | 48 | * void blink(DigitalOut *led) { |
annieluo2 | 0:d6c9b09b4042 | 49 | * while (running) { |
annieluo2 | 0:d6c9b09b4042 | 50 | * *led = !*led; |
annieluo2 | 0:d6c9b09b4042 | 51 | * Thread::wait(1000); |
annieluo2 | 0:d6c9b09b4042 | 52 | * } |
annieluo2 | 0:d6c9b09b4042 | 53 | * } |
annieluo2 | 0:d6c9b09b4042 | 54 | * |
annieluo2 | 0:d6c9b09b4042 | 55 | * // Spawns a thread to run blink for 5 seconds |
annieluo2 | 0:d6c9b09b4042 | 56 | * int main() { |
annieluo2 | 0:d6c9b09b4042 | 57 | * thread.start(led1, blink); |
annieluo2 | 0:d6c9b09b4042 | 58 | * Thread::wait(5000); |
annieluo2 | 0:d6c9b09b4042 | 59 | * running = false; |
annieluo2 | 0:d6c9b09b4042 | 60 | * thread.join(); |
annieluo2 | 0:d6c9b09b4042 | 61 | * } |
annieluo2 | 0:d6c9b09b4042 | 62 | * @endcode |
annieluo2 | 0:d6c9b09b4042 | 63 | */ |
annieluo2 | 0:d6c9b09b4042 | 64 | class Thread { |
annieluo2 | 0:d6c9b09b4042 | 65 | public: |
annieluo2 | 0:d6c9b09b4042 | 66 | /** Allocate a new thread without starting execution |
annieluo2 | 0:d6c9b09b4042 | 67 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
annieluo2 | 0:d6c9b09b4042 | 68 | @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). |
annieluo2 | 0:d6c9b09b4042 | 69 | @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). |
annieluo2 | 0:d6c9b09b4042 | 70 | */ |
annieluo2 | 0:d6c9b09b4042 | 71 | Thread(osPriority priority=osPriorityNormal, |
annieluo2 | 0:d6c9b09b4042 | 72 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
annieluo2 | 0:d6c9b09b4042 | 73 | unsigned char *stack_pointer=NULL) { |
annieluo2 | 0:d6c9b09b4042 | 74 | constructor(priority, stack_size, stack_pointer); |
annieluo2 | 0:d6c9b09b4042 | 75 | } |
annieluo2 | 0:d6c9b09b4042 | 76 | |
annieluo2 | 0:d6c9b09b4042 | 77 | /** Create a new thread, and start it executing the specified function. |
annieluo2 | 0:d6c9b09b4042 | 78 | @param task function to be executed by this thread. |
annieluo2 | 0:d6c9b09b4042 | 79 | @param argument pointer that is passed to the thread function as start argument. (default: NULL). |
annieluo2 | 0:d6c9b09b4042 | 80 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
annieluo2 | 0:d6c9b09b4042 | 81 | @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). |
annieluo2 | 0:d6c9b09b4042 | 82 | @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). |
annieluo2 | 0:d6c9b09b4042 | 83 | @deprecated |
annieluo2 | 0:d6c9b09b4042 | 84 | Thread-spawning constructors hide errors. Replaced by thread.start(task). |
annieluo2 | 0:d6c9b09b4042 | 85 | |
annieluo2 | 0:d6c9b09b4042 | 86 | @code |
annieluo2 | 0:d6c9b09b4042 | 87 | Thread thread(priority, stack_size, stack_pointer); |
annieluo2 | 0:d6c9b09b4042 | 88 | |
annieluo2 | 0:d6c9b09b4042 | 89 | osStatus status = thread.start(task); |
annieluo2 | 0:d6c9b09b4042 | 90 | if (status != osOK) { |
annieluo2 | 0:d6c9b09b4042 | 91 | error("oh no!"); |
annieluo2 | 0:d6c9b09b4042 | 92 | } |
annieluo2 | 0:d6c9b09b4042 | 93 | @endcode |
annieluo2 | 0:d6c9b09b4042 | 94 | */ |
annieluo2 | 0:d6c9b09b4042 | 95 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
annieluo2 | 0:d6c9b09b4042 | 96 | "Thread-spawning constructors hide errors. " |
annieluo2 | 0:d6c9b09b4042 | 97 | "Replaced by thread.start(task).") |
annieluo2 | 0:d6c9b09b4042 | 98 | Thread(mbed::Callback<void()> task, |
annieluo2 | 0:d6c9b09b4042 | 99 | osPriority priority=osPriorityNormal, |
annieluo2 | 0:d6c9b09b4042 | 100 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
annieluo2 | 0:d6c9b09b4042 | 101 | unsigned char *stack_pointer=NULL) { |
annieluo2 | 0:d6c9b09b4042 | 102 | constructor(task, priority, stack_size, stack_pointer); |
annieluo2 | 0:d6c9b09b4042 | 103 | } |
annieluo2 | 0:d6c9b09b4042 | 104 | |
annieluo2 | 0:d6c9b09b4042 | 105 | /** Create a new thread, and start it executing the specified function. |
annieluo2 | 0:d6c9b09b4042 | 106 | @param obj argument to task. |
annieluo2 | 0:d6c9b09b4042 | 107 | @param method function to be executed by this thread. |
annieluo2 | 0:d6c9b09b4042 | 108 | @param argument pointer that is passed to the thread function as start argument. (default: NULL). |
annieluo2 | 0:d6c9b09b4042 | 109 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
annieluo2 | 0:d6c9b09b4042 | 110 | @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). |
annieluo2 | 0:d6c9b09b4042 | 111 | @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). |
annieluo2 | 0:d6c9b09b4042 | 112 | @deprecated |
annieluo2 | 0:d6c9b09b4042 | 113 | Thread-spawning constructors hide errors. Replaced by thread.start(callback(task, argument)). |
annieluo2 | 0:d6c9b09b4042 | 114 | |
annieluo2 | 0:d6c9b09b4042 | 115 | @code |
annieluo2 | 0:d6c9b09b4042 | 116 | Thread thread(priority, stack_size, stack_pointer); |
annieluo2 | 0:d6c9b09b4042 | 117 | |
annieluo2 | 0:d6c9b09b4042 | 118 | osStatus status = thread.start(callback(task, argument)); |
annieluo2 | 0:d6c9b09b4042 | 119 | if (status != osOK) { |
annieluo2 | 0:d6c9b09b4042 | 120 | error("oh no!"); |
annieluo2 | 0:d6c9b09b4042 | 121 | } |
annieluo2 | 0:d6c9b09b4042 | 122 | @endcode |
annieluo2 | 0:d6c9b09b4042 | 123 | */ |
annieluo2 | 0:d6c9b09b4042 | 124 | template <typename T> |
annieluo2 | 0:d6c9b09b4042 | 125 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
annieluo2 | 0:d6c9b09b4042 | 126 | "Thread-spawning constructors hide errors. " |
annieluo2 | 0:d6c9b09b4042 | 127 | "Replaced by thread.start(callback(task, argument)).") |
annieluo2 | 0:d6c9b09b4042 | 128 | Thread(T *argument, void (T::*task)(), |
annieluo2 | 0:d6c9b09b4042 | 129 | osPriority priority=osPriorityNormal, |
annieluo2 | 0:d6c9b09b4042 | 130 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
annieluo2 | 0:d6c9b09b4042 | 131 | unsigned char *stack_pointer=NULL) { |
annieluo2 | 0:d6c9b09b4042 | 132 | constructor(mbed::callback(task, argument), |
annieluo2 | 0:d6c9b09b4042 | 133 | priority, stack_size, stack_pointer); |
annieluo2 | 0:d6c9b09b4042 | 134 | } |
annieluo2 | 0:d6c9b09b4042 | 135 | |
annieluo2 | 0:d6c9b09b4042 | 136 | /** Create a new thread, and start it executing the specified function. |
annieluo2 | 0:d6c9b09b4042 | 137 | @param obj argument to task. |
annieluo2 | 0:d6c9b09b4042 | 138 | @param method function to be executed by this thread. |
annieluo2 | 0:d6c9b09b4042 | 139 | @param argument pointer that is passed to the thread function as start argument. (default: NULL). |
annieluo2 | 0:d6c9b09b4042 | 140 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
annieluo2 | 0:d6c9b09b4042 | 141 | @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). |
annieluo2 | 0:d6c9b09b4042 | 142 | @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). |
annieluo2 | 0:d6c9b09b4042 | 143 | @deprecated |
annieluo2 | 0:d6c9b09b4042 | 144 | Thread-spawning constructors hide errors. Replaced by thread.start(callback(task, argument)). |
annieluo2 | 0:d6c9b09b4042 | 145 | |
annieluo2 | 0:d6c9b09b4042 | 146 | @code |
annieluo2 | 0:d6c9b09b4042 | 147 | Thread thread(priority, stack_size, stack_pointer); |
annieluo2 | 0:d6c9b09b4042 | 148 | |
annieluo2 | 0:d6c9b09b4042 | 149 | osStatus status = thread.start(callback(task, argument)); |
annieluo2 | 0:d6c9b09b4042 | 150 | if (status != osOK) { |
annieluo2 | 0:d6c9b09b4042 | 151 | error("oh no!"); |
annieluo2 | 0:d6c9b09b4042 | 152 | } |
annieluo2 | 0:d6c9b09b4042 | 153 | @endcode |
annieluo2 | 0:d6c9b09b4042 | 154 | */ |
annieluo2 | 0:d6c9b09b4042 | 155 | template <typename T> |
annieluo2 | 0:d6c9b09b4042 | 156 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
annieluo2 | 0:d6c9b09b4042 | 157 | "Thread-spawning constructors hide errors. " |
annieluo2 | 0:d6c9b09b4042 | 158 | "Replaced by thread.start(callback(task, argument)).") |
annieluo2 | 0:d6c9b09b4042 | 159 | Thread(T *argument, void (*task)(T *), |
annieluo2 | 0:d6c9b09b4042 | 160 | osPriority priority=osPriorityNormal, |
annieluo2 | 0:d6c9b09b4042 | 161 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
annieluo2 | 0:d6c9b09b4042 | 162 | unsigned char *stack_pointer=NULL) { |
annieluo2 | 0:d6c9b09b4042 | 163 | constructor(mbed::callback(task, argument), |
annieluo2 | 0:d6c9b09b4042 | 164 | priority, stack_size, stack_pointer); |
annieluo2 | 0:d6c9b09b4042 | 165 | } |
annieluo2 | 0:d6c9b09b4042 | 166 | |
annieluo2 | 0:d6c9b09b4042 | 167 | /** Create a new thread, and start it executing the specified function. |
annieluo2 | 0:d6c9b09b4042 | 168 | Provided for backwards compatibility |
annieluo2 | 0:d6c9b09b4042 | 169 | @param task function to be executed by this thread. |
annieluo2 | 0:d6c9b09b4042 | 170 | @param argument pointer that is passed to the thread function as start argument. (default: NULL). |
annieluo2 | 0:d6c9b09b4042 | 171 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
annieluo2 | 0:d6c9b09b4042 | 172 | @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). |
annieluo2 | 0:d6c9b09b4042 | 173 | @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). |
annieluo2 | 0:d6c9b09b4042 | 174 | @deprecated |
annieluo2 | 0:d6c9b09b4042 | 175 | Thread-spawning constructors hide errors. Replaced by thread.start(callback(task, argument)). |
annieluo2 | 0:d6c9b09b4042 | 176 | |
annieluo2 | 0:d6c9b09b4042 | 177 | @code |
annieluo2 | 0:d6c9b09b4042 | 178 | Thread thread(priority, stack_size, stack_pointer); |
annieluo2 | 0:d6c9b09b4042 | 179 | |
annieluo2 | 0:d6c9b09b4042 | 180 | osStatus status = thread.start(callback(task, argument)); |
annieluo2 | 0:d6c9b09b4042 | 181 | if (status != osOK) { |
annieluo2 | 0:d6c9b09b4042 | 182 | error("oh no!"); |
annieluo2 | 0:d6c9b09b4042 | 183 | } |
annieluo2 | 0:d6c9b09b4042 | 184 | @endcode |
annieluo2 | 0:d6c9b09b4042 | 185 | */ |
annieluo2 | 0:d6c9b09b4042 | 186 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
annieluo2 | 0:d6c9b09b4042 | 187 | "Thread-spawning constructors hide errors. " |
annieluo2 | 0:d6c9b09b4042 | 188 | "Replaced by thread.start(callback(task, argument)).") |
annieluo2 | 0:d6c9b09b4042 | 189 | Thread(void (*task)(void const *argument), void *argument=NULL, |
annieluo2 | 0:d6c9b09b4042 | 190 | osPriority priority=osPriorityNormal, |
annieluo2 | 0:d6c9b09b4042 | 191 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
annieluo2 | 0:d6c9b09b4042 | 192 | unsigned char *stack_pointer=NULL) { |
annieluo2 | 0:d6c9b09b4042 | 193 | constructor(mbed::callback((void (*)(void *))task, argument), |
annieluo2 | 0:d6c9b09b4042 | 194 | priority, stack_size, stack_pointer); |
annieluo2 | 0:d6c9b09b4042 | 195 | } |
annieluo2 | 0:d6c9b09b4042 | 196 | |
annieluo2 | 0:d6c9b09b4042 | 197 | /** Starts a thread executing the specified function. |
annieluo2 | 0:d6c9b09b4042 | 198 | @param task function to be executed by this thread. |
annieluo2 | 0:d6c9b09b4042 | 199 | @return status code that indicates the execution status of the function. |
annieluo2 | 0:d6c9b09b4042 | 200 | */ |
annieluo2 | 0:d6c9b09b4042 | 201 | osStatus start(mbed::Callback<void()> task); |
annieluo2 | 0:d6c9b09b4042 | 202 | |
annieluo2 | 0:d6c9b09b4042 | 203 | /** Starts a thread executing the specified function. |
annieluo2 | 0:d6c9b09b4042 | 204 | @param obj argument to task |
annieluo2 | 0:d6c9b09b4042 | 205 | @param method function to be executed by this thread. |
annieluo2 | 0:d6c9b09b4042 | 206 | @return status code that indicates the execution status of the function. |
annieluo2 | 0:d6c9b09b4042 | 207 | @deprecated |
annieluo2 | 0:d6c9b09b4042 | 208 | The start function does not support cv-qualifiers. Replaced by start(callback(obj, method)). |
annieluo2 | 0:d6c9b09b4042 | 209 | */ |
annieluo2 | 0:d6c9b09b4042 | 210 | template <typename T, typename M> |
annieluo2 | 0:d6c9b09b4042 | 211 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
annieluo2 | 0:d6c9b09b4042 | 212 | "The start function does not support cv-qualifiers. " |
annieluo2 | 0:d6c9b09b4042 | 213 | "Replaced by thread.start(callback(obj, method)).") |
annieluo2 | 0:d6c9b09b4042 | 214 | osStatus start(T *obj, M method) { |
annieluo2 | 0:d6c9b09b4042 | 215 | return start(mbed::callback(obj, method)); |
annieluo2 | 0:d6c9b09b4042 | 216 | } |
annieluo2 | 0:d6c9b09b4042 | 217 | |
annieluo2 | 0:d6c9b09b4042 | 218 | /** Wait for thread to terminate |
annieluo2 | 0:d6c9b09b4042 | 219 | @return status code that indicates the execution status of the function. |
annieluo2 | 0:d6c9b09b4042 | 220 | @note not callable from interrupt |
annieluo2 | 0:d6c9b09b4042 | 221 | */ |
annieluo2 | 0:d6c9b09b4042 | 222 | osStatus join(); |
annieluo2 | 0:d6c9b09b4042 | 223 | |
annieluo2 | 0:d6c9b09b4042 | 224 | /** Terminate execution of a thread and remove it from Active Threads |
annieluo2 | 0:d6c9b09b4042 | 225 | @return status code that indicates the execution status of the function. |
annieluo2 | 0:d6c9b09b4042 | 226 | */ |
annieluo2 | 0:d6c9b09b4042 | 227 | osStatus terminate(); |
annieluo2 | 0:d6c9b09b4042 | 228 | |
annieluo2 | 0:d6c9b09b4042 | 229 | /** Set priority of an active thread |
annieluo2 | 0:d6c9b09b4042 | 230 | @param priority new priority value for the thread function. |
annieluo2 | 0:d6c9b09b4042 | 231 | @return status code that indicates the execution status of the function. |
annieluo2 | 0:d6c9b09b4042 | 232 | */ |
annieluo2 | 0:d6c9b09b4042 | 233 | osStatus set_priority(osPriority priority); |
annieluo2 | 0:d6c9b09b4042 | 234 | |
annieluo2 | 0:d6c9b09b4042 | 235 | /** Get priority of an active thread |
annieluo2 | 0:d6c9b09b4042 | 236 | @return current priority value of the thread function. |
annieluo2 | 0:d6c9b09b4042 | 237 | */ |
annieluo2 | 0:d6c9b09b4042 | 238 | osPriority get_priority(); |
annieluo2 | 0:d6c9b09b4042 | 239 | |
annieluo2 | 0:d6c9b09b4042 | 240 | /** Set the specified Signal Flags of an active thread. |
annieluo2 | 0:d6c9b09b4042 | 241 | @param signals specifies the signal flags of the thread that should be set. |
annieluo2 | 0:d6c9b09b4042 | 242 | @return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters. |
annieluo2 | 0:d6c9b09b4042 | 243 | */ |
annieluo2 | 0:d6c9b09b4042 | 244 | int32_t signal_set(int32_t signals); |
annieluo2 | 0:d6c9b09b4042 | 245 | |
annieluo2 | 0:d6c9b09b4042 | 246 | /** Clears the specified Signal Flags of an active thread. |
annieluo2 | 0:d6c9b09b4042 | 247 | @param signals specifies the signal flags of the thread that should be cleared. |
annieluo2 | 0:d6c9b09b4042 | 248 | @return resultant signal flags of the specified thread or 0x80000000 in case of incorrect parameters. |
annieluo2 | 0:d6c9b09b4042 | 249 | */ |
annieluo2 | 0:d6c9b09b4042 | 250 | int32_t signal_clr(int32_t signals); |
annieluo2 | 0:d6c9b09b4042 | 251 | |
annieluo2 | 0:d6c9b09b4042 | 252 | /** State of the Thread */ |
annieluo2 | 0:d6c9b09b4042 | 253 | enum State { |
annieluo2 | 0:d6c9b09b4042 | 254 | Inactive, /**< Not created or terminated */ |
annieluo2 | 0:d6c9b09b4042 | 255 | Ready, /**< Ready to run */ |
annieluo2 | 0:d6c9b09b4042 | 256 | Running, /**< Running */ |
annieluo2 | 0:d6c9b09b4042 | 257 | WaitingDelay, /**< Waiting for a delay to occur */ |
annieluo2 | 0:d6c9b09b4042 | 258 | WaitingInterval, /**< Waiting for an interval to occur */ |
annieluo2 | 0:d6c9b09b4042 | 259 | WaitingOr, /**< Waiting for one event in a set to occur */ |
annieluo2 | 0:d6c9b09b4042 | 260 | WaitingAnd, /**< Waiting for multiple events in a set to occur */ |
annieluo2 | 0:d6c9b09b4042 | 261 | WaitingSemaphore, /**< Waiting for a semaphore event to occur */ |
annieluo2 | 0:d6c9b09b4042 | 262 | WaitingMailbox, /**< Waiting for a mailbox event to occur */ |
annieluo2 | 0:d6c9b09b4042 | 263 | WaitingMutex, /**< Waiting for a mutex event to occur */ |
annieluo2 | 0:d6c9b09b4042 | 264 | |
annieluo2 | 0:d6c9b09b4042 | 265 | /* Not in sync with RTX below here */ |
annieluo2 | 0:d6c9b09b4042 | 266 | Deleted, /**< The task has been deleted */ |
annieluo2 | 0:d6c9b09b4042 | 267 | }; |
annieluo2 | 0:d6c9b09b4042 | 268 | |
annieluo2 | 0:d6c9b09b4042 | 269 | /** State of this Thread |
annieluo2 | 0:d6c9b09b4042 | 270 | @return the State of this Thread |
annieluo2 | 0:d6c9b09b4042 | 271 | */ |
annieluo2 | 0:d6c9b09b4042 | 272 | State get_state(); |
annieluo2 | 0:d6c9b09b4042 | 273 | |
annieluo2 | 0:d6c9b09b4042 | 274 | /** Get the total stack memory size for this Thread |
annieluo2 | 0:d6c9b09b4042 | 275 | @return the total stack memory size in bytes |
annieluo2 | 0:d6c9b09b4042 | 276 | */ |
annieluo2 | 0:d6c9b09b4042 | 277 | uint32_t stack_size(); |
annieluo2 | 0:d6c9b09b4042 | 278 | |
annieluo2 | 0:d6c9b09b4042 | 279 | /** Get the currently unused stack memory for this Thread |
annieluo2 | 0:d6c9b09b4042 | 280 | @return the currently unused stack memory in bytes |
annieluo2 | 0:d6c9b09b4042 | 281 | */ |
annieluo2 | 0:d6c9b09b4042 | 282 | uint32_t free_stack(); |
annieluo2 | 0:d6c9b09b4042 | 283 | |
annieluo2 | 0:d6c9b09b4042 | 284 | /** Get the currently used stack memory for this Thread |
annieluo2 | 0:d6c9b09b4042 | 285 | @return the currently used stack memory in bytes |
annieluo2 | 0:d6c9b09b4042 | 286 | */ |
annieluo2 | 0:d6c9b09b4042 | 287 | uint32_t used_stack(); |
annieluo2 | 0:d6c9b09b4042 | 288 | |
annieluo2 | 0:d6c9b09b4042 | 289 | /** Get the maximum stack memory usage to date for this Thread |
annieluo2 | 0:d6c9b09b4042 | 290 | @return the maximum stack memory usage to date in bytes |
annieluo2 | 0:d6c9b09b4042 | 291 | */ |
annieluo2 | 0:d6c9b09b4042 | 292 | uint32_t max_stack(); |
annieluo2 | 0:d6c9b09b4042 | 293 | |
annieluo2 | 0:d6c9b09b4042 | 294 | /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread. |
annieluo2 | 0:d6c9b09b4042 | 295 | @param signals wait until all specified signal flags set or 0 for any single signal flag. |
annieluo2 | 0:d6c9b09b4042 | 296 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). |
annieluo2 | 0:d6c9b09b4042 | 297 | @return event flag information or error code. |
annieluo2 | 0:d6c9b09b4042 | 298 | @note not callable from interrupt |
annieluo2 | 0:d6c9b09b4042 | 299 | */ |
annieluo2 | 0:d6c9b09b4042 | 300 | static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever); |
annieluo2 | 0:d6c9b09b4042 | 301 | |
annieluo2 | 0:d6c9b09b4042 | 302 | /** Wait for a specified time period in millisec: |
annieluo2 | 0:d6c9b09b4042 | 303 | @param millisec time delay value |
annieluo2 | 0:d6c9b09b4042 | 304 | @return status code that indicates the execution status of the function. |
annieluo2 | 0:d6c9b09b4042 | 305 | @note not callable from interrupt |
annieluo2 | 0:d6c9b09b4042 | 306 | */ |
annieluo2 | 0:d6c9b09b4042 | 307 | static osStatus wait(uint32_t millisec); |
annieluo2 | 0:d6c9b09b4042 | 308 | |
annieluo2 | 0:d6c9b09b4042 | 309 | /** Pass control to next thread that is in state READY. |
annieluo2 | 0:d6c9b09b4042 | 310 | @return status code that indicates the execution status of the function. |
annieluo2 | 0:d6c9b09b4042 | 311 | @note not callable from interrupt |
annieluo2 | 0:d6c9b09b4042 | 312 | */ |
annieluo2 | 0:d6c9b09b4042 | 313 | static osStatus yield(); |
annieluo2 | 0:d6c9b09b4042 | 314 | |
annieluo2 | 0:d6c9b09b4042 | 315 | /** Get the thread id of the current running thread. |
annieluo2 | 0:d6c9b09b4042 | 316 | @return thread ID for reference by other functions or NULL in case of error. |
annieluo2 | 0:d6c9b09b4042 | 317 | */ |
annieluo2 | 0:d6c9b09b4042 | 318 | static osThreadId gettid(); |
annieluo2 | 0:d6c9b09b4042 | 319 | |
annieluo2 | 0:d6c9b09b4042 | 320 | /** Attach a function to be called by the RTOS idle task |
annieluo2 | 0:d6c9b09b4042 | 321 | @param fptr pointer to the function to be called |
annieluo2 | 0:d6c9b09b4042 | 322 | */ |
annieluo2 | 0:d6c9b09b4042 | 323 | static void attach_idle_hook(void (*fptr)(void)); |
annieluo2 | 0:d6c9b09b4042 | 324 | |
annieluo2 | 0:d6c9b09b4042 | 325 | /** Attach a function to be called when a task is killed |
annieluo2 | 0:d6c9b09b4042 | 326 | @param fptr pointer to the function to be called |
annieluo2 | 0:d6c9b09b4042 | 327 | */ |
annieluo2 | 0:d6c9b09b4042 | 328 | static void attach_terminate_hook(void (*fptr)(osThreadId id)); |
annieluo2 | 0:d6c9b09b4042 | 329 | |
annieluo2 | 0:d6c9b09b4042 | 330 | virtual ~Thread(); |
annieluo2 | 0:d6c9b09b4042 | 331 | |
annieluo2 | 0:d6c9b09b4042 | 332 | private: |
annieluo2 | 0:d6c9b09b4042 | 333 | // Required to share definitions without |
annieluo2 | 0:d6c9b09b4042 | 334 | // delegated constructors |
annieluo2 | 0:d6c9b09b4042 | 335 | void constructor(osPriority priority=osPriorityNormal, |
annieluo2 | 0:d6c9b09b4042 | 336 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
annieluo2 | 0:d6c9b09b4042 | 337 | unsigned char *stack_pointer=NULL); |
annieluo2 | 0:d6c9b09b4042 | 338 | void constructor(mbed::Callback<void()> task, |
annieluo2 | 0:d6c9b09b4042 | 339 | osPriority priority=osPriorityNormal, |
annieluo2 | 0:d6c9b09b4042 | 340 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
annieluo2 | 0:d6c9b09b4042 | 341 | unsigned char *stack_pointer=NULL); |
annieluo2 | 0:d6c9b09b4042 | 342 | static void _thunk(const void * thread_ptr); |
annieluo2 | 0:d6c9b09b4042 | 343 | |
annieluo2 | 0:d6c9b09b4042 | 344 | mbed::Callback<void()> _task; |
annieluo2 | 0:d6c9b09b4042 | 345 | osThreadId _tid; |
annieluo2 | 0:d6c9b09b4042 | 346 | osThreadDef_t _thread_def; |
annieluo2 | 0:d6c9b09b4042 | 347 | bool _dynamic_stack; |
annieluo2 | 0:d6c9b09b4042 | 348 | Semaphore _join_sem; |
annieluo2 | 0:d6c9b09b4042 | 349 | Mutex _mutex; |
annieluo2 | 0:d6c9b09b4042 | 350 | }; |
annieluo2 | 0:d6c9b09b4042 | 351 | |
annieluo2 | 0:d6c9b09b4042 | 352 | } |
annieluo2 | 0:d6c9b09b4042 | 353 | #endif |
annieluo2 | 0:d6c9b09b4042 | 354 | |
annieluo2 | 0:d6c9b09b4042 | 355 | /** @}*/ |