Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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