Daoyu_Sofiane Yao_Belouka / mbed-rtos

Dependents:   Mecatro_Gyro_Programme_Codeur_HC06

Committer:
daoyu_sofiane
Date:
Fri Apr 16 09:25:33 2021 +0000
Revision:
0:a8ed743bc1e1
Projet Gyropode

Who changed what in which revision?

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