1

Committer:
valeyev
Date:
Tue Mar 13 07:17:50 2018 +0000
Revision:
0:e056ac8fecf8
looking for...

Who changed what in which revision?

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