Mbed OS Reference
Loading...
Searching...
No Matches
ThisThread.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2019 ARM Limited
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23#ifndef THIS_THREAD_H
24#define THIS_THREAD_H
25
26#include <stdint.h>
27#include "platform/mbed_toolchain.h"
28#include "rtos/Kernel.h"
29#include "rtos/mbed_rtos_types.h"
30
31namespace rtos {
32namespace ThisThread {
33
34
35/**
36 * \addtogroup rtos-public-api
37 * @{
38 * \defgroup rtos_ThisThread ThisThread namespace
39 * @{
40 */
41
42/** The ThisThread namespace allows controlling the current thread.
43 *
44 * Example:
45 * @code
46 * #include "mbed.h"
47 * #include "rtos.h"
48 *
49 * Thread thread;
50 * DigitalOut led1(LED1);
51 *
52 * #define STOP_FLAG 1
53 *
54 * // Blink function toggles the led in a long running loop
55 * void blink(DigitalOut *led) {
56 * while (!ThisThread::flags_wait_any_for(STOP_FLAG, 1000)) {
57 * *led = !*led;
58 * }
59 * }
60 *
61 * // Spawns a thread to run blink for 5 seconds
62 * int main() {
63 * thread.start(callback(blink, &led1));
64 * ThisThread::sleep_for(5000);
65 * thread.signal_set(STOP_FLAG);
66 * thread.join();
67 * }
68 * @endcode
69 *
70 */
71/** Clears the specified Thread Flags of the currently running thread.
72 @param flags specifies the flags of the thread that should be cleared.
73 @return thread flags before clearing.
74 @note You cannot call this function from ISR context.
75 @see Thread::flags_set
76*/
77uint32_t flags_clear(uint32_t flags);
78
79/** Returns the Thread Flags currently set for the currently running thread.
80 @return current thread flags or 0 if not in a valid thread.
81 @note You cannot call this function from ISR context.
82 @see Thread::flags_set
83*/
84uint32_t flags_get();
85
86/** Wait for all of the specified Thread Flags to become signaled for the current thread.
87 @param flags specifies the flags to wait for
88 @param clear whether to clear the specified flags after waiting for them. (default: true)
89 @return actual thread flags before clearing, which will satisfy the wait
90 @note You cannot call this function from ISR context.
91 @see Thread::flags_set
92*/
93uint32_t flags_wait_all(uint32_t flags, bool clear = true);
94
95/** Wait for any of the specified Thread Flags to become signaled for the current thread.
96 @param flags specifies the flags to wait for
97 @param clear whether to clear the specified flags after waiting for them. (default: true)
98 @return actual thread flags before clearing, which will satisfy the wait
99 @note You cannot call this function from ISR context.
100 @see Thread::flags_set
101*/
102uint32_t flags_wait_any(uint32_t flags, bool clear = true);
103
104/** Wait for all of the specified Thread Flags to become signaled for the current thread.
105 @param flags specifies the flags to wait for
106 @param millisec timeout value.
107 @param clear whether to clear the specified flags after waiting for them. (default: true)
108 @return actual thread flags before clearing, which may not satisfy the wait
109 @note You cannot call this function from ISR context.
110 @see Thread::flags_set
111 @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
112*/
113MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
114uint32_t flags_wait_all_for(uint32_t flags, uint32_t millisec, bool clear = true);
115
116/** Wait for all of the specified Thread Flags to become signaled for the current thread.
117 @param flags specifies the flags to wait for
118 @param rel_time timeout value.
119 @param clear whether to clear the specified flags after waiting for them. (default: true)
120 @return actual thread flags before clearing, which may not satisfy the wait
121 @note You cannot call this function from ISR context.
122 @see Thread::flags_set
123*/
124uint32_t flags_wait_all_for(uint32_t flags, Kernel::Clock::duration_u32 rel_time, bool clear = true);
125
126/** Wait for all of the specified Thread Flags to become signaled for the current thread.
127 @param flags specifies the flags to wait for
128 @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
129 @param clear whether to clear the specified flags after waiting for them. (default: true)
130 @return actual thread flags before clearing, which may not satisfy the wait
131 @note You cannot call this function from ISR context.
132 @note the underlying RTOS may have a limit to the maximum wait time
133 due to internal 32-bit computations, but this is guaranteed to work if the
134 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
135 the wait will time out earlier than specified.
136 @see Thread::flags_set
137 @deprecated Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s`
138 rather than `Kernel::get_ms_count() + 5000`.
139*/
140MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.")
141uint32_t flags_wait_all_until(uint32_t flags, uint64_t millisec, bool clear = true);
142
143/** Wait for all of the specified Thread Flags to become signaled for the current thread.
144 @param flags specifies the flags to wait for
145 @param abs_time absolute timeout time, referenced to Kernel::Clock
146 @param clear whether to clear the specified flags after waiting for them. (default: true)
147 @return actual thread flags before clearing, which may not satisfy the wait
148 @note You cannot call this function from ISR context.
149 @note the underlying RTOS may have a limit to the maximum wait time
150 due to internal 32-bit computations, but this is guaranteed to work if the
151 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
152 the wait will time out earlier than specified.
153 @see Thread::flags_set
154*/
155uint32_t flags_wait_all_until(uint32_t flags, Kernel::Clock::time_point abs_time, bool clear = true);
156
157/** Wait for any of the specified Thread Flags to become signaled for the current thread.
158 @param flags specifies the flags to wait for
159 @param millisec timeout value.
160 @param clear whether to clear the specified flags after waiting for them. (default: true)
161 @return actual thread flags before clearing, which may not satisfy the wait
162 @note You cannot call this function from ISR context.
163 @see Thread::flags_set
164 @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
165*/
166MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
167uint32_t flags_wait_any_for(uint32_t flags, uint32_t millisec, bool clear = true);
168
169/** Wait for any of the specified Thread Flags to become signaled for the current thread.
170 @param flags specifies the flags to wait for
171 @param rel_time timeout value.
172 @param clear whether to clear the specified flags after waiting for them. (default: true)
173 @return actual thread flags before clearing, which may not satisfy the wait
174 @note You cannot call this function from ISR context.
175 @see Thread::flags_set
176*/
177uint32_t flags_wait_any_for(uint32_t flags, Kernel::Clock::duration_u32 rel_time, bool clear = true);
178
179/** Wait for any of the specified Thread Flags to become signaled for the current thread.
180 @param flags specifies the flags to wait for
181 @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
182 @param clear whether to clear the specified flags after waiting for them. (default: true)
183 @return actual thread flags before clearing, which may not satisfy the wait
184 @note You cannot call this function from ISR context.
185 @note the underlying RTOS may have a limit to the maximum wait time
186 due to internal 32-bit computations, but this is guaranteed to work if the
187 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
188 the wait will time out earlier than specified.
189 @see Thread::flags_set
190 @deprecated Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s`
191 rather than `Kernel::get_ms_count() + 5000`.
192*/
193MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.")
194uint32_t flags_wait_any_until(uint32_t flags, uint64_t millisec, bool clear = true);
195
196/** Wait for any of the specified Thread Flags to become signaled for the current thread.
197 @param flags specifies the flags to wait for
198 @param abs_time absolute timeout time, referenced to Kernel::Clock
199 @param clear whether to clear the specified flags after waiting for them. (default: true)
200 @return actual thread flags before clearing, which may not satisfy the wait
201 @note You cannot call this function from ISR context.
202 @note the underlying RTOS may have a limit to the maximum wait time
203 due to internal 32-bit computations, but this is guaranteed to work if the
204 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
205 the wait will time out earlier than specified.
206 @see Thread::flags_set
207*/
208uint32_t flags_wait_any_until(uint32_t flags, Kernel::Clock::time_point abs_time, bool clear = true);
209
210/** Sleep for a specified time period in millisec:
211 @param millisec time delay value
212 @note You cannot call this function from ISR context.
213 @note The equivalent functionality is accessible in C via thread_sleep_for.
214 @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
215*/
216MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
217void sleep_for(uint32_t millisec);
218
219/** Sleep for a specified time period:
220 @param rel_time time delay value
221 @note You cannot call this function from ISR context.
222 @note The equivalent functionality is accessible in C via thread_sleep_for.
223*/
224void sleep_for(Kernel::Clock::duration_u32 rel_time);
225
226
227/** Sleep until a specified time in millisec
228 The specified time is according to Kernel::get_ms_count().
229 @param millisec absolute time in millisec
230 @note You cannot call this function from ISR context.
231 @note if millisec is equal to or lower than the current tick count, this
232 returns immediately.
233 @note The equivalent functionality is accessible in C via thread_sleep_until.
234 @deprecated Pass a chrono time_point, not an integer millisecond count. For example use
235 `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.
236*/
237MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`.")
238void sleep_until(uint64_t millisec);
239
240/** Sleep until a specified time in millisec
241 The specified time is according to Kernel::Clock.
242 @param abs_time absolute time
243 @note You cannot call this function from ISR context.
244 @note if abs_time is equal to or lower than Kernel::Clock::now(), this
245 returns immediately.
246 @note The equivalent functionality is accessible in C via thread_sleep_until.
247*/
248void sleep_until(Kernel::Clock::time_point abs_time);
249
250/** Pass control to next equal-priority thread that is in state READY.
251 (Higher-priority READY threads would prevent us from running; this
252 will not enable lower-priority threads to run, as we remain READY).
253 @note You cannot call this function from ISR context.
254*/
255void yield();
256
257/** Get the thread id of the current running thread.
258 @return thread ID for reference by other functions or nullptr in case of error or in ISR context.
259 @note You may call this function from ISR context.
260*/
261osThreadId_t get_id();
262
263/** Get the thread name of the current running thread.
264 @return thread name pointer or nullptr if thread has no name or in case of error.
265 @note You cannot call this function from ISR context.
266*/
267const char *get_name();
268
269/** @}*/
270/** @}*/
271};
272
273namespace internal {
274
275/** \addtogroup rtos-internal-api */
276/** @{*/
277
279 uint32_t *flags;
280 uint32_t options;
281 uint32_t flags_wanted;
282 uint32_t result;
283 bool match;
284};
285
286bool non_rtos_check_flags(void *handle);
287
288/** @}*/
289}
290}
291#endif
uint64_t get_ms_count(void)
Generic thread functions.
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
uint32_t flags_clear(uint32_t flags)
The ThisThread namespace allows controlling the current thread.
uint32_t flags_wait_any_for(uint32_t flags, uint32_t millisec, bool clear=true)
Wait for any of the specified Thread Flags to become signaled for the current thread.
void sleep_until(uint64_t millisec)
Sleep until a specified time in millisec The specified time is according to Kernel::get_ms_count().
osThreadId_t get_id()
Get the thread id of the current running thread.
void yield()
Pass control to next equal-priority thread that is in state READY.
uint32_t flags_wait_any_until(uint32_t flags, uint64_t millisec, bool clear=true)
Wait for any of the specified Thread Flags to become signaled for the current thread.
uint32_t flags_wait_all_until(uint32_t flags, uint64_t millisec, bool clear=true)
Wait for all of the specified Thread Flags to become signaled for the current thread.
uint32_t flags_wait_any(uint32_t flags, bool clear=true)
Wait for any of the specified Thread Flags to become signaled for the current thread.
uint32_t flags_wait_all_for(uint32_t flags, uint32_t millisec, bool clear=true)
Wait for all of the specified Thread Flags to become signaled for the current thread.
void sleep_for(uint32_t millisec)
Sleep for a specified time period in millisec:
uint32_t flags_wait_all(uint32_t flags, bool clear=true)
Wait for all of the specified Thread Flags to become signaled for the current thread.
const char * get_name()
Get the thread name of the current running thread.
uint32_t flags_get()
Returns the Thread Flags currently set for the currently running thread.