mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
Child:
1:9db0e321a9f4
mbed-os5 only for TYBLE16

Who changed what in which revision?

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