Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2016-2017 Wind River Systems, Inc.
3 : : * Copyright (c) 2024 Intel Corporation
4 : : *
5 : : * SPDX-License-Identifier: Apache-2.0
6 : : */
7 : :
8 : : #ifndef ZEPHYR_KERNEL_INCLUDE_THREAD_H_
9 : : #define ZEPHYR_KERNEL_INCLUDE_THREAD_H_
10 : :
11 : : #include <zephyr/kernel.h>
12 : : #include <kernel_internal.h>
13 : : #include <timeout_q.h>
14 : :
15 : :
16 : : #define Z_STATE_STR_DUMMY "dummy"
17 : : #define Z_STATE_STR_PENDING "pending"
18 : : #define Z_STATE_STR_SLEEPING "sleeping"
19 : : #define Z_STATE_STR_DEAD "dead"
20 : : #define Z_STATE_STR_SUSPENDED "suspended"
21 : : #define Z_STATE_STR_ABORTING "aborting"
22 : : #define Z_STATE_STR_SUSPENDING "suspending"
23 : : #define Z_STATE_STR_QUEUED "queued"
24 : :
25 : : #ifdef CONFIG_THREAD_MONITOR
26 : : /* This lock protects the linked list of active threads; i.e. the
27 : : * initial _kernel.threads pointer and the linked list made up of
28 : : * thread->next_thread (until NULL)
29 : : */
30 : : extern struct k_spinlock z_thread_monitor_lock;
31 : : #endif /* CONFIG_THREAD_MONITOR */
32 : :
33 : : #ifdef CONFIG_MULTITHREADING
34 : : extern struct k_thread z_idle_threads[CONFIG_MP_MAX_NUM_CPUS];
35 : : #endif /* CONFIG_MULTITHREADING */
36 : :
37 : : void idle(void *unused1, void *unused2, void *unused3);
38 : :
39 : : /* clean up when a thread is aborted */
40 : :
41 : : #if defined(CONFIG_THREAD_MONITOR)
42 : : void z_thread_monitor_exit(struct k_thread *thread);
43 : : #else
44 : : #define z_thread_monitor_exit(thread) \
45 : : do {/* nothing */ \
46 : : } while (false)
47 : : #endif /* CONFIG_THREAD_MONITOR */
48 : :
49 : : void z_thread_abort(struct k_thread *thread);
50 : :
51 : 54 : static inline void thread_schedule_new(struct k_thread *thread, k_timeout_t delay)
52 : : {
53 : : #ifdef CONFIG_SYS_CLOCK_EXISTS
54 [ + + ]: 54 : if (K_TIMEOUT_EQ(delay, K_NO_WAIT)) {
55 : 4 : k_thread_start(thread);
56 : : } else {
57 : 50 : z_add_thread_timeout(thread, delay);
58 : : }
59 : : #else
60 : : if (!K_TIMEOUT_EQ(delay, K_FOREVER)) {
61 : : k_thread_start(thread);
62 : : }
63 : : #endif /* CONFIG_SYS_CLOCK_EXISTS */
64 : 54 : }
65 : :
66 : 180 : static inline int thread_is_preemptible(const struct k_thread *thread)
67 : : {
68 : : /* explanation in kernel_struct.h */
69 : 180 : return thread->base.preempt <= _PREEMPT_THRESHOLD;
70 : : }
71 : :
72 : :
73 : 4 : static inline int thread_is_metairq(const struct k_thread *thread)
74 : : {
75 : : #if CONFIG_NUM_METAIRQ_PRIORITIES > 0
76 : : return (thread->base.prio - K_HIGHEST_THREAD_PRIO)
77 : : < CONFIG_NUM_METAIRQ_PRIORITIES;
78 : : #else
79 : 4 : ARG_UNUSED(thread);
80 : 4 : return 0;
81 : : #endif /* CONFIG_NUM_METAIRQ_PRIORITIES */
82 : : }
83 : :
84 : 129 : static inline bool is_thread_dummy(const struct k_thread *thread)
85 : : {
86 : 129 : return (thread->base.thread_state & _THREAD_DUMMY) != 0U;
87 : : }
88 : :
89 : 0 : static inline bool z_is_thread_suspended(const struct k_thread *thread)
90 : : {
91 : 0 : return (thread->base.thread_state & _THREAD_SUSPENDED) != 0U;
92 : : }
93 : :
94 : 0 : static inline bool z_is_thread_pending(const struct k_thread *thread)
95 : : {
96 : 0 : return (thread->base.thread_state & _THREAD_PENDING) != 0U;
97 : : }
98 : :
99 : 158 : static inline bool z_is_thread_dead(const struct k_thread *thread)
100 : : {
101 : 158 : return (thread->base.thread_state & _THREAD_DEAD) != 0U;
102 : : }
103 : :
104 : : /* Return true if the thread is aborting, else false */
105 : : static inline bool z_is_thread_aborting(const struct k_thread *thread)
106 : : {
107 : : return (thread->base.thread_state & _THREAD_ABORTING) != 0U;
108 : : }
109 : :
110 : : /* Return true if the thread is aborting or suspending, else false */
111 : 48 : static inline bool z_is_thread_halting(const struct k_thread *thread)
112 : : {
113 : 48 : return (thread->base.thread_state &
114 : 48 : (_THREAD_ABORTING | _THREAD_SUSPENDING)) != 0U;
115 : : }
116 : :
117 : :
118 : 252 : static inline bool z_is_thread_prevented_from_running(const struct k_thread *thread)
119 : : {
120 : 252 : uint8_t state = thread->base.thread_state;
121 : :
122 : 252 : return (state & (_THREAD_PENDING | _THREAD_SLEEPING | _THREAD_DEAD |
123 : 252 : _THREAD_DUMMY | _THREAD_SUSPENDED)) != 0U;
124 : : }
125 : :
126 : : static inline bool z_is_thread_timeout_active(const struct k_thread *thread)
127 : : {
128 : : return !z_is_inactive_timeout(&thread->base.timeout);
129 : : }
130 : :
131 : 65 : static inline bool z_is_thread_ready(const struct k_thread *thread)
132 : : {
133 : 65 : return !z_is_thread_prevented_from_running(thread);
134 : : }
135 : :
136 : 129 : static inline bool z_is_thread_state_set(const struct k_thread *thread, uint32_t state)
137 : : {
138 : 129 : return (thread->base.thread_state & state) != 0U;
139 : : }
140 : :
141 : 129 : static inline bool z_is_thread_queued(const struct k_thread *thread)
142 : : {
143 : 129 : return z_is_thread_state_set(thread, _THREAD_QUEUED);
144 : : }
145 : :
146 : 65 : static inline void z_mark_thread_as_queued(struct k_thread *thread)
147 : : {
148 : 65 : thread->base.thread_state |= _THREAD_QUEUED;
149 : 65 : }
150 : :
151 : 64 : static inline void z_mark_thread_as_not_queued(struct k_thread *thread)
152 : : {
153 : 64 : thread->base.thread_state &= ~_THREAD_QUEUED;
154 : 64 : }
155 : :
156 : 0 : static inline void z_mark_thread_as_suspended(struct k_thread *thread)
157 : : {
158 : 0 : thread->base.thread_state |= _THREAD_SUSPENDED;
159 : :
160 : 0 : SYS_PORT_TRACING_FUNC(k_thread, sched_suspend, thread);
161 : 0 : }
162 : :
163 : 0 : static inline void z_mark_thread_as_not_suspended(struct k_thread *thread)
164 : : {
165 : 0 : thread->base.thread_state &= ~_THREAD_SUSPENDED;
166 : :
167 : 0 : SYS_PORT_TRACING_FUNC(k_thread, sched_resume, thread);
168 : 0 : }
169 : :
170 : 10 : static inline void z_mark_thread_as_pending(struct k_thread *thread)
171 : : {
172 : 10 : thread->base.thread_state |= _THREAD_PENDING;
173 : 10 : }
174 : :
175 : 10 : static inline void z_mark_thread_as_not_pending(struct k_thread *thread)
176 : : {
177 : 10 : thread->base.thread_state &= ~_THREAD_PENDING;
178 : 10 : }
179 : :
180 : 58 : static inline bool z_is_thread_sleeping(struct k_thread *thread)
181 : : {
182 : 58 : return (thread->base.thread_state & _THREAD_SLEEPING) != 0U;
183 : : }
184 : :
185 : 0 : static inline void z_mark_thread_as_sleeping(struct k_thread *thread)
186 : : {
187 : 0 : thread->base.thread_state |= _THREAD_SLEEPING;
188 : 0 : }
189 : :
190 : 56 : static inline void z_mark_thread_as_not_sleeping(struct k_thread *thread)
191 : : {
192 : 56 : thread->base.thread_state &= ~_THREAD_SLEEPING;
193 : 56 : }
194 : :
195 : : /*
196 : : * This function tags the current thread as essential to system operation.
197 : : * Exceptions raised by this thread will be treated as a fatal system error.
198 : : */
199 : : static inline void z_thread_essential_set(struct k_thread *thread)
200 : : {
201 : : thread->base.user_options |= K_ESSENTIAL;
202 : : }
203 : :
204 : : /*
205 : : * This function tags the current thread as not essential to system operation.
206 : : * Exceptions raised by this thread may be recoverable.
207 : : * (This is the default tag for a thread.)
208 : : */
209 : 0 : static inline void z_thread_essential_clear(struct k_thread *thread)
210 : : {
211 : 0 : thread->base.user_options &= ~K_ESSENTIAL;
212 : 0 : }
213 : :
214 : : /*
215 : : * This routine indicates if the current thread is an essential system thread.
216 : : *
217 : : * Returns true if current thread is essential, false if it is not.
218 : : */
219 : 158 : static inline bool z_is_thread_essential(const struct k_thread *thread)
220 : : {
221 : 158 : return (thread->base.user_options & K_ESSENTIAL) == K_ESSENTIAL;
222 : : }
223 : :
224 : :
225 : 130 : static ALWAYS_INLINE bool should_preempt(const struct k_thread *thread,
226 : : int preempt_ok)
227 : : {
228 : : /* Preemption is OK if it's being explicitly allowed by
229 : : * software state (e.g. the thread called k_yield())
230 : : */
231 [ + + ]: 130 : if (preempt_ok != 0) {
232 : : return true;
233 : : }
234 : :
235 [ - + ]: 66 : __ASSERT(_current != NULL, "");
236 : :
237 : : /* Or if we're pended/suspended/dummy (duh) */
238 [ + + ]: 66 : if (z_is_thread_prevented_from_running(_current)) {
239 : : return true;
240 : : }
241 : :
242 : : /* Otherwise we have to be running a preemptible thread or
243 : : * switching to a metairq
244 : : */
245 [ + + - + ]: 59 : if (thread_is_preemptible(_current) || thread_is_metairq(thread)) {
246 : 55 : return true;
247 : : }
248 : :
249 : : /* Edge case on ARM where a thread can be pended out of an
250 : : * interrupt handler before the "synchronous" swap starts
251 : : * context switching. Platforms with atomic swap can never
252 : : * hit this.
253 : : */
254 : : if (IS_ENABLED(CONFIG_SWAP_NONATOMIC)
255 : : && z_is_thread_timeout_active(thread)) {
256 : : return true;
257 : : }
258 : :
259 : : return false;
260 : : }
261 : :
262 : :
263 : 1 : static inline bool z_is_idle_thread_entry(k_thread_entry_t entry_point)
264 : : {
265 : 1 : return entry_point == idle;
266 : : }
267 : :
268 : 250 : static inline bool z_is_idle_thread_object(const struct k_thread *thread)
269 : : {
270 : : #ifdef CONFIG_MULTITHREADING
271 : : #ifdef CONFIG_SMP
272 : : return thread->base.is_idle;
273 : : #else
274 : 250 : return thread == &z_idle_threads[0];
275 : : #endif /* CONFIG_SMP */
276 : : #else
277 : : return false;
278 : : #endif /* CONFIG_MULTITHREADING */
279 : : }
280 : :
281 : :
282 : : #endif /* ZEPHYR_KERNEL_INCLUDE_THREAD_H_ */
|