Mistake on this page?
Report an issue in GitHub or email us
ThisThread.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2018 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef THIS_THREAD_H
23 #define THIS_THREAD_H
24 
25 #include <stdint.h>
26 #include "cmsis_os2.h"
27 #include "mbed_rtos1_types.h"
28 #include "mbed_rtos_storage.h"
29 #include "platform/Callback.h"
30 #include "platform/mbed_toolchain.h"
31 #include "platform/NonCopyable.h"
32 #include "rtos/Semaphore.h"
33 #include "rtos/Mutex.h"
34 
35 namespace rtos {
36 /** \addtogroup rtos */
37 /** @{*/
38 /**
39  * \defgroup rtos_ThisThread ThisThread namespace
40  * @{
41  */
42 
43 /** The ThisThread namespace allows controlling the current thread.
44  *
45  * Example:
46  * @code
47  * #include "mbed.h"
48  * #include "rtos.h"
49  *
50  * Thread thread;
51  * DigitalOut led1(LED1);
52  *
53  * #define STOP_FLAG 1
54  *
55  * // Blink function toggles the led in a long running loop
56  * void blink(DigitalOut *led) {
57  * while (!ThisThread::flags_wait_any_for(STOP_FLAG, 1000)) {
58  * *led = !*led;
59  * }
60  * }
61  *
62  * // Spawns a thread to run blink for 5 seconds
63  * int main() {
64  * thread.start(callback(blink, &led1));
65  * ThisThread::sleep_for(5000);
66  * thread.signal_set(STOP_FLAG);
67  * thread.join();
68  * }
69  * @endcode
70  *
71  */
72 namespace ThisThread {
73 /** Clears the specified Thread Flags of the currently running thread.
74  @param flags specifies the flags of the thread that should be cleared.
75  @return thread flags before clearing.
76  @note You cannot call this function from ISR context.
77  @see Thread::flags_set
78 */
79 uint32_t flags_clear(uint32_t flags);
80 
81 /** Returns the Thread Flags currently set for the currently running thread.
82  @return current thread flags or 0 if not in a valid thread.
83  @note You cannot call this function from ISR context.
84  @see Thread::flags_set
85 */
86 uint32_t flags_get();
87 
88 /** Wait for all of the specified Thread Flags to become signaled for the current thread.
89  @param flags specifies the flags to wait for
90  @param clear whether to clear the specified flags after waiting for them. (default: true)
91  @return actual thread flags before clearing, which will satisfy the wait
92  @note You cannot call this function from ISR context.
93  @see Thread::flags_set
94 */
95 uint32_t flags_wait_all(uint32_t flags, bool clear = true);
96 
97 /** Wait for any of the specified Thread Flags to become signaled for the current thread.
98  @param flags specifies the flags to wait for
99  @param clear whether to clear the specified flags after waiting for them. (default: true)
100  @return actual thread flags before clearing, which will satisfy the wait
101  @note You cannot call this function from ISR context.
102  @see Thread::flags_set
103 */
104 uint32_t flags_wait_any(uint32_t flags, bool clear = true);
105 
106 /** Wait for all of the specified Thread Flags to become signaled for the current thread.
107  @param flags specifies the flags to wait for
108  @param millisec timeout value.
109  @param clear whether to clear the specified flags after waiting for them. (default: true)
110  @return actual thread flags before clearing, which may not satisfy the wait
111  @note You cannot call this function from ISR context.
112  @see Thread::flags_set
113 */
114 uint32_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 millisec absolute timeout time, referenced to Kernel::get_ms_count()
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  @note the underlying RTOS may have a limit to the maximum wait time
123  due to internal 32-bit computations, but this is guaranteed to work if the
124  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
125  the wait will time out earlier than specified.
126  @see Thread::flags_set
127 */
128 uint32_t flags_wait_all_until(uint32_t flags, uint64_t millisec, bool clear = true);
129 
130 /** Wait for any of the specified Thread Flags to become signaled for the current thread.
131  @param flags specifies the flags to wait for
132  @param millisec timeout value.
133  @param clear whether to clear the specified flags after waiting for them. (default: true)
134  @return actual thread flags before clearing, which may not satisfy the wait
135  @note You cannot call this function from ISR context.
136  @see Thread::flags_set
137 */
138 uint32_t flags_wait_any_for(uint32_t flags, uint32_t millisec, bool clear = true);
139 
140 /** Wait for any of the specified Thread Flags to become signaled for the current thread.
141  @param flags specifies the flags to wait for
142  @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
143  @param clear whether to clear the specified flags after waiting for them. (default: true)
144  @return actual thread flags before clearing, which may not satisfy the wait
145  @note You cannot call this function from ISR context.
146  @note the underlying RTOS may have a limit to the maximum wait time
147  due to internal 32-bit computations, but this is guaranteed to work if the
148  wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
149  the wait will time out earlier than specified.
150  @see Thread::flags_set
151 */
152 uint32_t flags_wait_any_until(uint32_t flags, uint64_t millisec, bool clear = true);
153 
154 /** Sleep for a specified time period in millisec:
155  @param millisec time delay value
156  @note You cannot call this function from ISR context.
157 */
158 void sleep_for(uint32_t millisec);
159 
160 /** Sleep until a specified time in millisec
161  The specified time is according to Kernel::get_ms_count().
162  @param millisec absolute time in millisec
163  @note You cannot call this function from ISR context.
164  @note if millisec is equal to or lower than the current tick count, this
165  returns immediately.
166 */
167 void sleep_until(uint64_t millisec);
168 
169 /** Pass control to next equal-priority thread that is in state READY.
170  (Higher-priority READY threads would prevent us from running; this
171  will not enable lower-priority threads to run, as we remain READY).
172  @note You cannot call this function from ISR context.
173 */
174 void yield();
175 
176 /** Get the thread id of the current running thread.
177  @return thread ID for reference by other functions or NULL in case of error or in ISR context.
178  @note You may call this function from ISR context.
179 */
180 osThreadId_t get_id();
181 
182 /** Get the thread name of the current running thread.
183  @return thread name pointer or NULL if thread has no name or in case of error.
184  @note You cannot call this function from ISR context.
185 */
186 const char *get_name();
187 
188 };
189 /** @}*/
190 /** @}*/
191 }
192 #endif
193 
194 
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_clear(uint32_t flags)
Clears the specified Thread Flags of the currently running thread.
osThreadId_t get_id()
Get the thread id of the current running thread.
uint32_t flags_get()
Returns the Thread Flags currently set for the currently running 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.
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.
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.
void sleep_until(uint64_t millisec)
Sleep until a specified time in millisec The specified time is according to Kernel::get_ms_count().
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.
void yield()
Pass control to next equal-priority thread that is in state READY.
const char * get_name()
Get the thread name of the current running 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.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.