Daniel Vizcaya
/
04_RTOS_Embebidos
Entrega 3er corte - sistemas embebidos
mbed-os/rtos/Thread.h@0:6ad07c9019fd, 2018-05-30 (annotated)
- Committer:
- Bethory
- Date:
- Wed May 30 00:01:50 2018 +0000
- Revision:
- 0:6ad07c9019fd
Codigo de tales para todos los pasculaes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bethory | 0:6ad07c9019fd | 1 | /* mbed Microcontroller Library |
Bethory | 0:6ad07c9019fd | 2 | * Copyright (c) 2006-2012 ARM Limited |
Bethory | 0:6ad07c9019fd | 3 | * |
Bethory | 0:6ad07c9019fd | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Bethory | 0:6ad07c9019fd | 5 | * of this software and associated documentation files (the "Software"), to deal |
Bethory | 0:6ad07c9019fd | 6 | * in the Software without restriction, including without limitation the rights |
Bethory | 0:6ad07c9019fd | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Bethory | 0:6ad07c9019fd | 8 | * copies of the Software, and to permit persons to whom the Software is |
Bethory | 0:6ad07c9019fd | 9 | * furnished to do so, subject to the following conditions: |
Bethory | 0:6ad07c9019fd | 10 | * |
Bethory | 0:6ad07c9019fd | 11 | * The above copyright notice and this permission notice shall be included in |
Bethory | 0:6ad07c9019fd | 12 | * all copies or substantial portions of the Software. |
Bethory | 0:6ad07c9019fd | 13 | * |
Bethory | 0:6ad07c9019fd | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Bethory | 0:6ad07c9019fd | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Bethory | 0:6ad07c9019fd | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Bethory | 0:6ad07c9019fd | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Bethory | 0:6ad07c9019fd | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Bethory | 0:6ad07c9019fd | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
Bethory | 0:6ad07c9019fd | 20 | * SOFTWARE. |
Bethory | 0:6ad07c9019fd | 21 | */ |
Bethory | 0:6ad07c9019fd | 22 | #ifndef THREAD_H |
Bethory | 0:6ad07c9019fd | 23 | #define THREAD_H |
Bethory | 0:6ad07c9019fd | 24 | |
Bethory | 0:6ad07c9019fd | 25 | #include <stdint.h> |
Bethory | 0:6ad07c9019fd | 26 | #include "cmsis_os2.h" |
Bethory | 0:6ad07c9019fd | 27 | #include "mbed_rtos1_types.h" |
Bethory | 0:6ad07c9019fd | 28 | #include "mbed_rtos_storage.h" |
Bethory | 0:6ad07c9019fd | 29 | #include "platform/Callback.h" |
Bethory | 0:6ad07c9019fd | 30 | #include "platform/mbed_toolchain.h" |
Bethory | 0:6ad07c9019fd | 31 | #include "platform/NonCopyable.h" |
Bethory | 0:6ad07c9019fd | 32 | #include "rtos/Semaphore.h" |
Bethory | 0:6ad07c9019fd | 33 | #include "rtos/Mutex.h" |
Bethory | 0:6ad07c9019fd | 34 | |
Bethory | 0:6ad07c9019fd | 35 | namespace rtos { |
Bethory | 0:6ad07c9019fd | 36 | /** \addtogroup rtos */ |
Bethory | 0:6ad07c9019fd | 37 | /** @{*/ |
Bethory | 0:6ad07c9019fd | 38 | /** |
Bethory | 0:6ad07c9019fd | 39 | * \defgroup rtos_Thread Thread class |
Bethory | 0:6ad07c9019fd | 40 | * @{ |
Bethory | 0:6ad07c9019fd | 41 | */ |
Bethory | 0:6ad07c9019fd | 42 | |
Bethory | 0:6ad07c9019fd | 43 | /** The Thread class allow defining, creating, and controlling thread functions in the system. |
Bethory | 0:6ad07c9019fd | 44 | * |
Bethory | 0:6ad07c9019fd | 45 | * Example: |
Bethory | 0:6ad07c9019fd | 46 | * @code |
Bethory | 0:6ad07c9019fd | 47 | * #include "mbed.h" |
Bethory | 0:6ad07c9019fd | 48 | * #include "rtos.h" |
Bethory | 0:6ad07c9019fd | 49 | * |
Bethory | 0:6ad07c9019fd | 50 | * Thread thread; |
Bethory | 0:6ad07c9019fd | 51 | * DigitalOut led1(LED1); |
Bethory | 0:6ad07c9019fd | 52 | * volatile bool running = true; |
Bethory | 0:6ad07c9019fd | 53 | * |
Bethory | 0:6ad07c9019fd | 54 | * // Blink function toggles the led in a long running loop |
Bethory | 0:6ad07c9019fd | 55 | * void blink(DigitalOut *led) { |
Bethory | 0:6ad07c9019fd | 56 | * while (running) { |
Bethory | 0:6ad07c9019fd | 57 | * *led = !*led; |
Bethory | 0:6ad07c9019fd | 58 | * wait(1); |
Bethory | 0:6ad07c9019fd | 59 | * } |
Bethory | 0:6ad07c9019fd | 60 | * } |
Bethory | 0:6ad07c9019fd | 61 | * |
Bethory | 0:6ad07c9019fd | 62 | * // Spawns a thread to run blink for 5 seconds |
Bethory | 0:6ad07c9019fd | 63 | * int main() { |
Bethory | 0:6ad07c9019fd | 64 | * thread.start(callback(blink, &led1)); |
Bethory | 0:6ad07c9019fd | 65 | * wait(5); |
Bethory | 0:6ad07c9019fd | 66 | * running = false; |
Bethory | 0:6ad07c9019fd | 67 | * thread.join(); |
Bethory | 0:6ad07c9019fd | 68 | * } |
Bethory | 0:6ad07c9019fd | 69 | * @endcode |
Bethory | 0:6ad07c9019fd | 70 | * |
Bethory | 0:6ad07c9019fd | 71 | * @note |
Bethory | 0:6ad07c9019fd | 72 | * Memory considerations: The thread control structures will be created on current thread's stack, both for the mbed OS |
Bethory | 0:6ad07c9019fd | 73 | * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). |
Bethory | 0:6ad07c9019fd | 74 | * Additionally the stack memory for this thread will be allocated on the heap, if it wasn't supplied to the constructor. |
Bethory | 0:6ad07c9019fd | 75 | * |
Bethory | 0:6ad07c9019fd | 76 | * @note |
Bethory | 0:6ad07c9019fd | 77 | * MBED_TZ_DEFAULT_ACCESS (default:0) flag can be used to change the default access of all user threads in non-secure mode. |
Bethory | 0:6ad07c9019fd | 78 | * MBED_TZ_DEFAULT_ACCESS set to 1, means all non-secure user threads have access to call secure functions. |
Bethory | 0:6ad07c9019fd | 79 | * MBED_TZ_DEFAULT_ACCESS set to 0, means none of the non-secure user thread have access to call secure functions, |
Bethory | 0:6ad07c9019fd | 80 | * to give access to particular thread used overloaded constructor with `tz_module` as argument during thread creation. |
Bethory | 0:6ad07c9019fd | 81 | * |
Bethory | 0:6ad07c9019fd | 82 | * MBED_TZ_DEFAULT_ACCESS is target specific define, should be set in targets.json file for Cortex-M23/M33 devices. |
Bethory | 0:6ad07c9019fd | 83 | */ |
Bethory | 0:6ad07c9019fd | 84 | |
Bethory | 0:6ad07c9019fd | 85 | class Thread : private mbed::NonCopyable<Thread> { |
Bethory | 0:6ad07c9019fd | 86 | public: |
Bethory | 0:6ad07c9019fd | 87 | /** Allocate a new thread without starting execution |
Bethory | 0:6ad07c9019fd | 88 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
Bethory | 0:6ad07c9019fd | 89 | @param stack_size stack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE). |
Bethory | 0:6ad07c9019fd | 90 | @param stack_mem pointer to the stack area to be used by this thread (default: NULL). |
Bethory | 0:6ad07c9019fd | 91 | @param name name to be used for this thread. It has to stay allocated for the lifetime of the thread (default: NULL) |
Bethory | 0:6ad07c9019fd | 92 | |
Bethory | 0:6ad07c9019fd | 93 | @note Default value of tz_module will be MBED_TZ_DEFAULT_ACCESS |
Bethory | 0:6ad07c9019fd | 94 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 95 | */ |
Bethory | 0:6ad07c9019fd | 96 | |
Bethory | 0:6ad07c9019fd | 97 | Thread(osPriority priority=osPriorityNormal, |
Bethory | 0:6ad07c9019fd | 98 | uint32_t stack_size=OS_STACK_SIZE, |
Bethory | 0:6ad07c9019fd | 99 | unsigned char *stack_mem=NULL, const char *name=NULL) { |
Bethory | 0:6ad07c9019fd | 100 | constructor(priority, stack_size, stack_mem, name); |
Bethory | 0:6ad07c9019fd | 101 | } |
Bethory | 0:6ad07c9019fd | 102 | |
Bethory | 0:6ad07c9019fd | 103 | /** Allocate a new thread without starting execution |
Bethory | 0:6ad07c9019fd | 104 | @param tz_module trustzone thread identifier (osThreadAttr_t::tz_module) |
Bethory | 0:6ad07c9019fd | 105 | Context of RTOS threads in non-secure state must be saved when calling secure functions. |
Bethory | 0:6ad07c9019fd | 106 | tz_module ID is used to allocate context memory for threads, and it can be safely set to zero for |
Bethory | 0:6ad07c9019fd | 107 | threads not using secure calls at all. See "TrustZone RTOS Context Management" for more details. |
Bethory | 0:6ad07c9019fd | 108 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
Bethory | 0:6ad07c9019fd | 109 | @param stack_size stack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE). |
Bethory | 0:6ad07c9019fd | 110 | @param stack_mem pointer to the stack area to be used by this thread (default: NULL). |
Bethory | 0:6ad07c9019fd | 111 | @param name name to be used for this thread. It has to stay allocated for the lifetime of the thread (default: NULL) |
Bethory | 0:6ad07c9019fd | 112 | |
Bethory | 0:6ad07c9019fd | 113 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 114 | */ |
Bethory | 0:6ad07c9019fd | 115 | |
Bethory | 0:6ad07c9019fd | 116 | Thread(uint32_t tz_module, osPriority priority=osPriorityNormal, |
Bethory | 0:6ad07c9019fd | 117 | uint32_t stack_size=OS_STACK_SIZE, |
Bethory | 0:6ad07c9019fd | 118 | unsigned char *stack_mem=NULL, const char *name=NULL) { |
Bethory | 0:6ad07c9019fd | 119 | constructor(tz_module, priority, stack_size, stack_mem, name); |
Bethory | 0:6ad07c9019fd | 120 | } |
Bethory | 0:6ad07c9019fd | 121 | |
Bethory | 0:6ad07c9019fd | 122 | |
Bethory | 0:6ad07c9019fd | 123 | /** Create a new thread, and start it executing the specified function. |
Bethory | 0:6ad07c9019fd | 124 | @param task function to be executed by this thread. |
Bethory | 0:6ad07c9019fd | 125 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
Bethory | 0:6ad07c9019fd | 126 | @param stack_size stack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE). |
Bethory | 0:6ad07c9019fd | 127 | @param stack_mem pointer to the stack area to be used by this thread (default: NULL). |
Bethory | 0:6ad07c9019fd | 128 | @deprecated |
Bethory | 0:6ad07c9019fd | 129 | Thread-spawning constructors hide errors. Replaced by thread.start(task). |
Bethory | 0:6ad07c9019fd | 130 | |
Bethory | 0:6ad07c9019fd | 131 | @code |
Bethory | 0:6ad07c9019fd | 132 | Thread thread(priority, stack_size, stack_mem); |
Bethory | 0:6ad07c9019fd | 133 | |
Bethory | 0:6ad07c9019fd | 134 | osStatus status = thread.start(task); |
Bethory | 0:6ad07c9019fd | 135 | if (status != osOK) { |
Bethory | 0:6ad07c9019fd | 136 | error("oh no!"); |
Bethory | 0:6ad07c9019fd | 137 | } |
Bethory | 0:6ad07c9019fd | 138 | @endcode |
Bethory | 0:6ad07c9019fd | 139 | |
Bethory | 0:6ad07c9019fd | 140 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 141 | */ |
Bethory | 0:6ad07c9019fd | 142 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
Bethory | 0:6ad07c9019fd | 143 | "Thread-spawning constructors hide errors. " |
Bethory | 0:6ad07c9019fd | 144 | "Replaced by thread.start(task).") |
Bethory | 0:6ad07c9019fd | 145 | Thread(mbed::Callback<void()> task, |
Bethory | 0:6ad07c9019fd | 146 | osPriority priority=osPriorityNormal, |
Bethory | 0:6ad07c9019fd | 147 | uint32_t stack_size=OS_STACK_SIZE, |
Bethory | 0:6ad07c9019fd | 148 | unsigned char *stack_mem=NULL) { |
Bethory | 0:6ad07c9019fd | 149 | constructor(task, priority, stack_size, stack_mem); |
Bethory | 0:6ad07c9019fd | 150 | } |
Bethory | 0:6ad07c9019fd | 151 | |
Bethory | 0:6ad07c9019fd | 152 | /** Create a new thread, and start it executing the specified function. |
Bethory | 0:6ad07c9019fd | 153 | @param argument pointer that is passed to the thread function as start argument. (default: NULL). |
Bethory | 0:6ad07c9019fd | 154 | @param task argument to task. |
Bethory | 0:6ad07c9019fd | 155 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
Bethory | 0:6ad07c9019fd | 156 | @param stack_size stack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE). |
Bethory | 0:6ad07c9019fd | 157 | @param stack_mem pointer to the stack area to be used by this thread (default: NULL). |
Bethory | 0:6ad07c9019fd | 158 | @deprecated |
Bethory | 0:6ad07c9019fd | 159 | Thread-spawning constructors hide errors. Replaced by thread.start(callback(task, argument)). |
Bethory | 0:6ad07c9019fd | 160 | |
Bethory | 0:6ad07c9019fd | 161 | @code |
Bethory | 0:6ad07c9019fd | 162 | Thread thread(priority, stack_size, stack_mem); |
Bethory | 0:6ad07c9019fd | 163 | |
Bethory | 0:6ad07c9019fd | 164 | osStatus status = thread.start(callback(task, argument)); |
Bethory | 0:6ad07c9019fd | 165 | if (status != osOK) { |
Bethory | 0:6ad07c9019fd | 166 | error("oh no!"); |
Bethory | 0:6ad07c9019fd | 167 | } |
Bethory | 0:6ad07c9019fd | 168 | @endcode |
Bethory | 0:6ad07c9019fd | 169 | |
Bethory | 0:6ad07c9019fd | 170 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 171 | */ |
Bethory | 0:6ad07c9019fd | 172 | template <typename T> |
Bethory | 0:6ad07c9019fd | 173 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
Bethory | 0:6ad07c9019fd | 174 | "Thread-spawning constructors hide errors. " |
Bethory | 0:6ad07c9019fd | 175 | "Replaced by thread.start(callback(task, argument)).") |
Bethory | 0:6ad07c9019fd | 176 | Thread(T *argument, void (T::*task)(), |
Bethory | 0:6ad07c9019fd | 177 | osPriority priority=osPriorityNormal, |
Bethory | 0:6ad07c9019fd | 178 | uint32_t stack_size=OS_STACK_SIZE, |
Bethory | 0:6ad07c9019fd | 179 | unsigned char *stack_mem=NULL) { |
Bethory | 0:6ad07c9019fd | 180 | constructor(mbed::callback(task, argument), |
Bethory | 0:6ad07c9019fd | 181 | priority, stack_size, stack_mem); |
Bethory | 0:6ad07c9019fd | 182 | } |
Bethory | 0:6ad07c9019fd | 183 | |
Bethory | 0:6ad07c9019fd | 184 | /** Create a new thread, and start it executing the specified function. |
Bethory | 0:6ad07c9019fd | 185 | @param argument pointer that is passed to the thread function as start argument. (default: NULL). |
Bethory | 0:6ad07c9019fd | 186 | @param task argument to task. |
Bethory | 0:6ad07c9019fd | 187 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
Bethory | 0:6ad07c9019fd | 188 | @param stack_size stack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE). |
Bethory | 0:6ad07c9019fd | 189 | @param stack_mem pointer to the stack area to be used by this thread (default: NULL). |
Bethory | 0:6ad07c9019fd | 190 | @deprecated |
Bethory | 0:6ad07c9019fd | 191 | Thread-spawning constructors hide errors. Replaced by thread.start(callback(task, argument)). |
Bethory | 0:6ad07c9019fd | 192 | |
Bethory | 0:6ad07c9019fd | 193 | @code |
Bethory | 0:6ad07c9019fd | 194 | Thread thread(priority, stack_size, stack_mem); |
Bethory | 0:6ad07c9019fd | 195 | |
Bethory | 0:6ad07c9019fd | 196 | osStatus status = thread.start(callback(task, argument)); |
Bethory | 0:6ad07c9019fd | 197 | if (status != osOK) { |
Bethory | 0:6ad07c9019fd | 198 | error("oh no!"); |
Bethory | 0:6ad07c9019fd | 199 | } |
Bethory | 0:6ad07c9019fd | 200 | @endcode |
Bethory | 0:6ad07c9019fd | 201 | |
Bethory | 0:6ad07c9019fd | 202 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 203 | */ |
Bethory | 0:6ad07c9019fd | 204 | template <typename T> |
Bethory | 0:6ad07c9019fd | 205 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
Bethory | 0:6ad07c9019fd | 206 | "Thread-spawning constructors hide errors. " |
Bethory | 0:6ad07c9019fd | 207 | "Replaced by thread.start(callback(task, argument)).") |
Bethory | 0:6ad07c9019fd | 208 | Thread(T *argument, void (*task)(T *), |
Bethory | 0:6ad07c9019fd | 209 | osPriority priority=osPriorityNormal, |
Bethory | 0:6ad07c9019fd | 210 | uint32_t stack_size=OS_STACK_SIZE, |
Bethory | 0:6ad07c9019fd | 211 | unsigned char *stack_mem=NULL) { |
Bethory | 0:6ad07c9019fd | 212 | constructor(mbed::callback(task, argument), |
Bethory | 0:6ad07c9019fd | 213 | priority, stack_size, stack_mem); |
Bethory | 0:6ad07c9019fd | 214 | } |
Bethory | 0:6ad07c9019fd | 215 | |
Bethory | 0:6ad07c9019fd | 216 | /** Create a new thread, and start it executing the specified function. |
Bethory | 0:6ad07c9019fd | 217 | Provided for backwards compatibility |
Bethory | 0:6ad07c9019fd | 218 | @param task function to be executed by this thread. |
Bethory | 0:6ad07c9019fd | 219 | @param argument pointer that is passed to the thread function as start argument. (default: NULL). |
Bethory | 0:6ad07c9019fd | 220 | @param priority initial priority of the thread function. (default: osPriorityNormal). |
Bethory | 0:6ad07c9019fd | 221 | @param stack_size stack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE). |
Bethory | 0:6ad07c9019fd | 222 | @param stack_mem pointer to the stack area to be used by this thread (default: NULL). |
Bethory | 0:6ad07c9019fd | 223 | @deprecated |
Bethory | 0:6ad07c9019fd | 224 | Thread-spawning constructors hide errors. Replaced by thread.start(callback(task, argument)). |
Bethory | 0:6ad07c9019fd | 225 | |
Bethory | 0:6ad07c9019fd | 226 | @code |
Bethory | 0:6ad07c9019fd | 227 | Thread thread(priority, stack_size, stack_mem); |
Bethory | 0:6ad07c9019fd | 228 | |
Bethory | 0:6ad07c9019fd | 229 | osStatus status = thread.start(callback(task, argument)); |
Bethory | 0:6ad07c9019fd | 230 | if (status != osOK) { |
Bethory | 0:6ad07c9019fd | 231 | error("oh no!"); |
Bethory | 0:6ad07c9019fd | 232 | } |
Bethory | 0:6ad07c9019fd | 233 | @endcode |
Bethory | 0:6ad07c9019fd | 234 | |
Bethory | 0:6ad07c9019fd | 235 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 236 | */ |
Bethory | 0:6ad07c9019fd | 237 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
Bethory | 0:6ad07c9019fd | 238 | "Thread-spawning constructors hide errors. " |
Bethory | 0:6ad07c9019fd | 239 | "Replaced by thread.start(callback(task, argument)).") |
Bethory | 0:6ad07c9019fd | 240 | Thread(void (*task)(void const *argument), void *argument=NULL, |
Bethory | 0:6ad07c9019fd | 241 | osPriority priority=osPriorityNormal, |
Bethory | 0:6ad07c9019fd | 242 | uint32_t stack_size=OS_STACK_SIZE, |
Bethory | 0:6ad07c9019fd | 243 | unsigned char *stack_mem=NULL) { |
Bethory | 0:6ad07c9019fd | 244 | constructor(mbed::callback((void (*)(void *))task, argument), |
Bethory | 0:6ad07c9019fd | 245 | priority, stack_size, stack_mem); |
Bethory | 0:6ad07c9019fd | 246 | } |
Bethory | 0:6ad07c9019fd | 247 | |
Bethory | 0:6ad07c9019fd | 248 | /** Starts a thread executing the specified function. |
Bethory | 0:6ad07c9019fd | 249 | @param task function to be executed by this thread. |
Bethory | 0:6ad07c9019fd | 250 | @return status code that indicates the execution status of the function. |
Bethory | 0:6ad07c9019fd | 251 | @note a thread can only be started once |
Bethory | 0:6ad07c9019fd | 252 | |
Bethory | 0:6ad07c9019fd | 253 | @note You cannot call this function ISR context. |
Bethory | 0:6ad07c9019fd | 254 | */ |
Bethory | 0:6ad07c9019fd | 255 | osStatus start(mbed::Callback<void()> task); |
Bethory | 0:6ad07c9019fd | 256 | |
Bethory | 0:6ad07c9019fd | 257 | /** Starts a thread executing the specified function. |
Bethory | 0:6ad07c9019fd | 258 | @param obj argument to task |
Bethory | 0:6ad07c9019fd | 259 | @param method function to be executed by this thread. |
Bethory | 0:6ad07c9019fd | 260 | @return status code that indicates the execution status of the function. |
Bethory | 0:6ad07c9019fd | 261 | @deprecated |
Bethory | 0:6ad07c9019fd | 262 | The start function does not support cv-qualifiers. Replaced by start(callback(obj, method)). |
Bethory | 0:6ad07c9019fd | 263 | |
Bethory | 0:6ad07c9019fd | 264 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 265 | */ |
Bethory | 0:6ad07c9019fd | 266 | template <typename T, typename M> |
Bethory | 0:6ad07c9019fd | 267 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
Bethory | 0:6ad07c9019fd | 268 | "The start function does not support cv-qualifiers. " |
Bethory | 0:6ad07c9019fd | 269 | "Replaced by thread.start(callback(obj, method)).") |
Bethory | 0:6ad07c9019fd | 270 | osStatus start(T *obj, M method) { |
Bethory | 0:6ad07c9019fd | 271 | return start(mbed::callback(obj, method)); |
Bethory | 0:6ad07c9019fd | 272 | } |
Bethory | 0:6ad07c9019fd | 273 | |
Bethory | 0:6ad07c9019fd | 274 | /** Wait for thread to terminate |
Bethory | 0:6ad07c9019fd | 275 | @return status code that indicates the execution status of the function. |
Bethory | 0:6ad07c9019fd | 276 | @note not callable from interrupt |
Bethory | 0:6ad07c9019fd | 277 | |
Bethory | 0:6ad07c9019fd | 278 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 279 | */ |
Bethory | 0:6ad07c9019fd | 280 | osStatus join(); |
Bethory | 0:6ad07c9019fd | 281 | |
Bethory | 0:6ad07c9019fd | 282 | /** Terminate execution of a thread and remove it from Active Threads |
Bethory | 0:6ad07c9019fd | 283 | @return status code that indicates the execution status of the function. |
Bethory | 0:6ad07c9019fd | 284 | |
Bethory | 0:6ad07c9019fd | 285 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 286 | */ |
Bethory | 0:6ad07c9019fd | 287 | osStatus terminate(); |
Bethory | 0:6ad07c9019fd | 288 | |
Bethory | 0:6ad07c9019fd | 289 | /** Set priority of an active thread |
Bethory | 0:6ad07c9019fd | 290 | @param priority new priority value for the thread function. |
Bethory | 0:6ad07c9019fd | 291 | @return status code that indicates the execution status of the function. |
Bethory | 0:6ad07c9019fd | 292 | |
Bethory | 0:6ad07c9019fd | 293 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 294 | */ |
Bethory | 0:6ad07c9019fd | 295 | osStatus set_priority(osPriority priority); |
Bethory | 0:6ad07c9019fd | 296 | |
Bethory | 0:6ad07c9019fd | 297 | /** Get priority of an active thread |
Bethory | 0:6ad07c9019fd | 298 | @return current priority value of the thread function. |
Bethory | 0:6ad07c9019fd | 299 | |
Bethory | 0:6ad07c9019fd | 300 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 301 | */ |
Bethory | 0:6ad07c9019fd | 302 | osPriority get_priority(); |
Bethory | 0:6ad07c9019fd | 303 | |
Bethory | 0:6ad07c9019fd | 304 | /** Set the specified Thread Flags for the thread. |
Bethory | 0:6ad07c9019fd | 305 | @param signals specifies the signal flags of the thread that should be set. |
Bethory | 0:6ad07c9019fd | 306 | @return signal flags after setting or osFlagsError in case of incorrect parameters. |
Bethory | 0:6ad07c9019fd | 307 | |
Bethory | 0:6ad07c9019fd | 308 | @note You may call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 309 | */ |
Bethory | 0:6ad07c9019fd | 310 | int32_t signal_set(int32_t signals); |
Bethory | 0:6ad07c9019fd | 311 | |
Bethory | 0:6ad07c9019fd | 312 | /** State of the Thread */ |
Bethory | 0:6ad07c9019fd | 313 | enum State { |
Bethory | 0:6ad07c9019fd | 314 | Inactive, /**< NOT USED */ |
Bethory | 0:6ad07c9019fd | 315 | Ready, /**< Ready to run */ |
Bethory | 0:6ad07c9019fd | 316 | Running, /**< Running */ |
Bethory | 0:6ad07c9019fd | 317 | WaitingDelay, /**< Waiting for a delay to occur */ |
Bethory | 0:6ad07c9019fd | 318 | WaitingJoin, /**< Waiting for thread to join. Only happens when using RTX directly. */ |
Bethory | 0:6ad07c9019fd | 319 | WaitingThreadFlag, /**< Waiting for a thread flag to be set */ |
Bethory | 0:6ad07c9019fd | 320 | WaitingEventFlag, /**< Waiting for a event flag to be set */ |
Bethory | 0:6ad07c9019fd | 321 | WaitingMutex, /**< Waiting for a mutex event to occur */ |
Bethory | 0:6ad07c9019fd | 322 | WaitingSemaphore, /**< Waiting for a semaphore event to occur */ |
Bethory | 0:6ad07c9019fd | 323 | WaitingMemoryPool, /**< Waiting for a memory pool */ |
Bethory | 0:6ad07c9019fd | 324 | WaitingMessageGet, /**< Waiting for message to arrive */ |
Bethory | 0:6ad07c9019fd | 325 | WaitingMessagePut, /**< Waiting for message to be send */ |
Bethory | 0:6ad07c9019fd | 326 | WaitingInterval, /**< NOT USED */ |
Bethory | 0:6ad07c9019fd | 327 | WaitingOr, /**< NOT USED */ |
Bethory | 0:6ad07c9019fd | 328 | WaitingAnd, /**< NOT USED */ |
Bethory | 0:6ad07c9019fd | 329 | WaitingMailbox, /**< NOT USED (Mail is implemented as MemoryPool and Queue) */ |
Bethory | 0:6ad07c9019fd | 330 | |
Bethory | 0:6ad07c9019fd | 331 | /* Not in sync with RTX below here */ |
Bethory | 0:6ad07c9019fd | 332 | Deleted, /**< The task has been deleted or not started */ |
Bethory | 0:6ad07c9019fd | 333 | }; |
Bethory | 0:6ad07c9019fd | 334 | |
Bethory | 0:6ad07c9019fd | 335 | /** State of this Thread |
Bethory | 0:6ad07c9019fd | 336 | @return the State of this Thread |
Bethory | 0:6ad07c9019fd | 337 | |
Bethory | 0:6ad07c9019fd | 338 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 339 | */ |
Bethory | 0:6ad07c9019fd | 340 | State get_state(); |
Bethory | 0:6ad07c9019fd | 341 | |
Bethory | 0:6ad07c9019fd | 342 | /** Get the total stack memory size for this Thread |
Bethory | 0:6ad07c9019fd | 343 | @return the total stack memory size in bytes |
Bethory | 0:6ad07c9019fd | 344 | |
Bethory | 0:6ad07c9019fd | 345 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 346 | */ |
Bethory | 0:6ad07c9019fd | 347 | uint32_t stack_size(); |
Bethory | 0:6ad07c9019fd | 348 | |
Bethory | 0:6ad07c9019fd | 349 | /** Get the currently unused stack memory for this Thread |
Bethory | 0:6ad07c9019fd | 350 | @return the currently unused stack memory in bytes |
Bethory | 0:6ad07c9019fd | 351 | |
Bethory | 0:6ad07c9019fd | 352 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 353 | */ |
Bethory | 0:6ad07c9019fd | 354 | uint32_t free_stack(); |
Bethory | 0:6ad07c9019fd | 355 | |
Bethory | 0:6ad07c9019fd | 356 | /** Get the currently used stack memory for this Thread |
Bethory | 0:6ad07c9019fd | 357 | @return the currently used stack memory in bytes |
Bethory | 0:6ad07c9019fd | 358 | |
Bethory | 0:6ad07c9019fd | 359 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 360 | */ |
Bethory | 0:6ad07c9019fd | 361 | uint32_t used_stack(); |
Bethory | 0:6ad07c9019fd | 362 | |
Bethory | 0:6ad07c9019fd | 363 | /** Get the maximum stack memory usage to date for this Thread |
Bethory | 0:6ad07c9019fd | 364 | @return the maximum stack memory usage to date in bytes |
Bethory | 0:6ad07c9019fd | 365 | |
Bethory | 0:6ad07c9019fd | 366 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 367 | */ |
Bethory | 0:6ad07c9019fd | 368 | uint32_t max_stack(); |
Bethory | 0:6ad07c9019fd | 369 | |
Bethory | 0:6ad07c9019fd | 370 | /** Get thread name |
Bethory | 0:6ad07c9019fd | 371 | @return thread name or NULL if the name was not set. |
Bethory | 0:6ad07c9019fd | 372 | |
Bethory | 0:6ad07c9019fd | 373 | @note You may call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 374 | */ |
Bethory | 0:6ad07c9019fd | 375 | const char *get_name(); |
Bethory | 0:6ad07c9019fd | 376 | |
Bethory | 0:6ad07c9019fd | 377 | /** Clears the specified Thread Flags of the currently running thread. |
Bethory | 0:6ad07c9019fd | 378 | @param signals specifies the signal flags of the thread that should be cleared. |
Bethory | 0:6ad07c9019fd | 379 | @return signal flags before clearing or osFlagsError in case of incorrect parameters. |
Bethory | 0:6ad07c9019fd | 380 | |
Bethory | 0:6ad07c9019fd | 381 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 382 | */ |
Bethory | 0:6ad07c9019fd | 383 | static int32_t signal_clr(int32_t signals); |
Bethory | 0:6ad07c9019fd | 384 | |
Bethory | 0:6ad07c9019fd | 385 | /** Wait for one or more Thread Flags to become signaled for the current RUNNING thread. |
Bethory | 0:6ad07c9019fd | 386 | @param signals wait until all specified signal flags are set or 0 for any single signal flag. |
Bethory | 0:6ad07c9019fd | 387 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). |
Bethory | 0:6ad07c9019fd | 388 | @return event flag information or error code. @note if @a millisec is set to 0 and flag is no set the event carries osOK value. |
Bethory | 0:6ad07c9019fd | 389 | |
Bethory | 0:6ad07c9019fd | 390 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 391 | */ |
Bethory | 0:6ad07c9019fd | 392 | static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever); |
Bethory | 0:6ad07c9019fd | 393 | |
Bethory | 0:6ad07c9019fd | 394 | /** Wait for a specified time period in milliseconds |
Bethory | 0:6ad07c9019fd | 395 | Being tick-based, the delay will be up to the specified time - eg for |
Bethory | 0:6ad07c9019fd | 396 | a value of 1 the system waits until the next millisecond tick occurs, |
Bethory | 0:6ad07c9019fd | 397 | leading to a delay of 0-1 milliseconds. |
Bethory | 0:6ad07c9019fd | 398 | @param millisec time delay value |
Bethory | 0:6ad07c9019fd | 399 | @return status code that indicates the execution status of the function. |
Bethory | 0:6ad07c9019fd | 400 | |
Bethory | 0:6ad07c9019fd | 401 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 402 | */ |
Bethory | 0:6ad07c9019fd | 403 | static osStatus wait(uint32_t millisec); |
Bethory | 0:6ad07c9019fd | 404 | |
Bethory | 0:6ad07c9019fd | 405 | /** Wait until a specified time in millisec |
Bethory | 0:6ad07c9019fd | 406 | The specified time is according to Kernel::get_ms_count(). |
Bethory | 0:6ad07c9019fd | 407 | @param millisec absolute time in millisec |
Bethory | 0:6ad07c9019fd | 408 | @return status code that indicates the execution status of the function. |
Bethory | 0:6ad07c9019fd | 409 | @note not callable from interrupt |
Bethory | 0:6ad07c9019fd | 410 | @note if millisec is equal to or lower than the current tick count, this |
Bethory | 0:6ad07c9019fd | 411 | returns immediately, either with an error or "osOK". |
Bethory | 0:6ad07c9019fd | 412 | @note the underlying RTOS may have a limit to the maximum wait time |
Bethory | 0:6ad07c9019fd | 413 | due to internal 32-bit computations, but this is guaranteed to work if the |
Bethory | 0:6ad07c9019fd | 414 | delay is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, |
Bethory | 0:6ad07c9019fd | 415 | it may return with an immediate error, or wait for the maximum delay. |
Bethory | 0:6ad07c9019fd | 416 | |
Bethory | 0:6ad07c9019fd | 417 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 418 | */ |
Bethory | 0:6ad07c9019fd | 419 | static osStatus wait_until(uint64_t millisec); |
Bethory | 0:6ad07c9019fd | 420 | |
Bethory | 0:6ad07c9019fd | 421 | /** Pass control to next thread that is in state READY. |
Bethory | 0:6ad07c9019fd | 422 | @return status code that indicates the execution status of the function. |
Bethory | 0:6ad07c9019fd | 423 | |
Bethory | 0:6ad07c9019fd | 424 | @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 425 | */ |
Bethory | 0:6ad07c9019fd | 426 | static osStatus yield(); |
Bethory | 0:6ad07c9019fd | 427 | |
Bethory | 0:6ad07c9019fd | 428 | /** Get the thread id of the current running thread. |
Bethory | 0:6ad07c9019fd | 429 | @return thread ID for reference by other functions or NULL in case of error. |
Bethory | 0:6ad07c9019fd | 430 | |
Bethory | 0:6ad07c9019fd | 431 | @note You may call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 432 | */ |
Bethory | 0:6ad07c9019fd | 433 | static osThreadId gettid(); |
Bethory | 0:6ad07c9019fd | 434 | |
Bethory | 0:6ad07c9019fd | 435 | /** Attach a function to be called by the RTOS idle task |
Bethory | 0:6ad07c9019fd | 436 | @param fptr pointer to the function to be called |
Bethory | 0:6ad07c9019fd | 437 | |
Bethory | 0:6ad07c9019fd | 438 | @note You may call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 439 | */ |
Bethory | 0:6ad07c9019fd | 440 | static void attach_idle_hook(void (*fptr)(void)); |
Bethory | 0:6ad07c9019fd | 441 | |
Bethory | 0:6ad07c9019fd | 442 | /** Attach a function to be called when a task is killed |
Bethory | 0:6ad07c9019fd | 443 | @param fptr pointer to the function to be called |
Bethory | 0:6ad07c9019fd | 444 | |
Bethory | 0:6ad07c9019fd | 445 | @note You may call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 446 | */ |
Bethory | 0:6ad07c9019fd | 447 | static void attach_terminate_hook(void (*fptr)(osThreadId id)); |
Bethory | 0:6ad07c9019fd | 448 | |
Bethory | 0:6ad07c9019fd | 449 | /** Thread destructor |
Bethory | 0:6ad07c9019fd | 450 | * |
Bethory | 0:6ad07c9019fd | 451 | * @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 452 | */ |
Bethory | 0:6ad07c9019fd | 453 | virtual ~Thread(); |
Bethory | 0:6ad07c9019fd | 454 | |
Bethory | 0:6ad07c9019fd | 455 | private: |
Bethory | 0:6ad07c9019fd | 456 | // Required to share definitions without |
Bethory | 0:6ad07c9019fd | 457 | // delegated constructors |
Bethory | 0:6ad07c9019fd | 458 | void constructor(osPriority priority=osPriorityNormal, |
Bethory | 0:6ad07c9019fd | 459 | uint32_t stack_size=OS_STACK_SIZE, |
Bethory | 0:6ad07c9019fd | 460 | unsigned char *stack_mem=NULL, |
Bethory | 0:6ad07c9019fd | 461 | const char *name=NULL); |
Bethory | 0:6ad07c9019fd | 462 | void constructor(mbed::Callback<void()> task, |
Bethory | 0:6ad07c9019fd | 463 | osPriority priority=osPriorityNormal, |
Bethory | 0:6ad07c9019fd | 464 | uint32_t stack_size=OS_STACK_SIZE, |
Bethory | 0:6ad07c9019fd | 465 | unsigned char *stack_mem=NULL, |
Bethory | 0:6ad07c9019fd | 466 | const char *name=NULL); |
Bethory | 0:6ad07c9019fd | 467 | void constructor(uint32_t tz_module, |
Bethory | 0:6ad07c9019fd | 468 | osPriority priority=osPriorityNormal, |
Bethory | 0:6ad07c9019fd | 469 | uint32_t stack_size=OS_STACK_SIZE, |
Bethory | 0:6ad07c9019fd | 470 | unsigned char *stack_mem=NULL, |
Bethory | 0:6ad07c9019fd | 471 | const char *name=NULL); |
Bethory | 0:6ad07c9019fd | 472 | static void _thunk(void * thread_ptr); |
Bethory | 0:6ad07c9019fd | 473 | |
Bethory | 0:6ad07c9019fd | 474 | mbed::Callback<void()> _task; |
Bethory | 0:6ad07c9019fd | 475 | osThreadId_t _tid; |
Bethory | 0:6ad07c9019fd | 476 | osThreadAttr_t _attr; |
Bethory | 0:6ad07c9019fd | 477 | bool _dynamic_stack; |
Bethory | 0:6ad07c9019fd | 478 | Semaphore _join_sem; |
Bethory | 0:6ad07c9019fd | 479 | Mutex _mutex; |
Bethory | 0:6ad07c9019fd | 480 | mbed_rtos_storage_thread_t _obj_mem; |
Bethory | 0:6ad07c9019fd | 481 | bool _finished; |
Bethory | 0:6ad07c9019fd | 482 | }; |
Bethory | 0:6ad07c9019fd | 483 | /** @}*/ |
Bethory | 0:6ad07c9019fd | 484 | /** @}*/ |
Bethory | 0:6ad07c9019fd | 485 | } |
Bethory | 0:6ad07c9019fd | 486 | #endif |
Bethory | 0:6ad07c9019fd | 487 | |
Bethory | 0:6ad07c9019fd | 488 |