Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2018 Intel Corporation
3 : : * Copyright The Zephyr Project Contributors
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : /**
8 : : * @file
9 : : * @brief MetaIRQ scheduling helpers (CONFIG_NUM_METAIRQ_PRIORITIES)
10 : : *
11 : : * MetaIRQ threads are a special class of cooperative threads that run at higher
12 : : * priority than any other thread and must be returned to after preemption.
13 : : * This header encapsulates all of that bookkeeping so that sched.c itself
14 : : * stays focused on run-queue management.
15 : : */
16 : :
17 : : #ifndef ZEPHYR_KERNEL_INCLUDE_METAIRQ_H_
18 : : #define ZEPHYR_KERNEL_INCLUDE_METAIRQ_H_
19 : :
20 : : #include <zephyr/kernel.h>
21 : : #include <kthread.h>
22 : :
23 : : BUILD_ASSERT(CONFIG_NUM_COOP_PRIORITIES >= CONFIG_NUM_METAIRQ_PRIORITIES,
24 : : "You need to provide at least as many CONFIG_NUM_COOP_PRIORITIES as "
25 : : "CONFIG_NUM_METAIRQ_PRIORITIES as Meta IRQs are just a special class of cooperative "
26 : : "threads.");
27 : :
28 : : /**
29 : : * @brief Update the per-CPU metairq preemption record.
30 : : *
31 : : * Must be called (with the scheduler spinlock held) whenever a new thread is
32 : : * selected to run. Records which cooperative thread a metairq preempted so
33 : : * that next_up() can return to it once the metairq finishes.
34 : : *
35 : : * @param thread The thread about to become _current.
36 : : */
37 : : static inline void update_metairq_preempt(struct k_thread *thread)
38 : : {
39 : : #if (CONFIG_NUM_METAIRQ_PRIORITIES > 0)
40 : : if (thread_is_metairq(thread) && !thread_is_metairq(_current) &&
41 : : !thread_is_preemptible(_current)) {
42 : : /* Record new preemption */
43 : : _current_cpu->metairq_preempted = _current;
44 : : } else if (!thread_is_metairq(thread)) {
45 : : /* Returning from existing preemption */
46 : : _current_cpu->metairq_preempted = NULL;
47 : : }
48 : : #else
49 : : ARG_UNUSED(thread);
50 : : #endif /* CONFIG_NUM_METAIRQ_PRIORITIES > 0 */
51 : : }
52 : :
53 : : /**
54 : : * @brief Recover a preempted cooperative thread after a metairq completes.
55 : : *
56 : : * After the run-queue winner (@p thread) has been chosen, check whether a
57 : : * cooperative thread was preempted by a metairq and should be resumed instead.
58 : : * Returns the adjusted thread pointer (unchanged when metairq logic is
59 : : * disabled or no recovery is needed).
60 : : *
61 : : * @param thread Best candidate chosen from the run queue.
62 : : * @return Thread to run next.
63 : : */
64 : 130 : static inline struct k_thread *metairq_preempt_recover(struct k_thread *thread)
65 : : {
66 : : #if (CONFIG_NUM_METAIRQ_PRIORITIES > 0)
67 : : /* MetaIRQs must always attempt to return back to a cooperative thread
68 : : * they preempted and not whatever happens to be highest priority now.
69 : : * The cooperative thread was promised it wouldn't be preempted (by
70 : : * non-metairq threads)!
71 : : */
72 : : struct k_thread *mirqp = _current_cpu->metairq_preempted;
73 : :
74 : : if (mirqp != NULL && (thread == NULL || !thread_is_metairq(thread))) {
75 : : if (z_is_thread_ready(mirqp)) {
76 : : return mirqp;
77 : : }
78 : : _current_cpu->metairq_preempted = NULL;
79 : : }
80 : : #else
81 : 130 : ARG_UNUSED(thread);
82 : : #endif /* CONFIG_NUM_METAIRQ_PRIORITIES > 0 */
83 : 130 : return thread;
84 : : }
85 : :
86 : : /**
87 : : * @brief Return true if _current may be re-queued after being context-switched.
88 : : *
89 : : * When a metairq thread preempts a cooperative thread, the preempted thread
90 : : * must not be put back on the run queue until the metairq completes;
91 : : * otherwise a lower-priority thread might steal it.
92 : : *
93 : : * @return true if it is safe to re-queue _current, false otherwise.
94 : : */
95 : : static inline bool metairq_current_requeue_allowed(void)
96 : : {
97 : : #if (CONFIG_NUM_METAIRQ_PRIORITIES > 0)
98 : : return _current != _current_cpu->metairq_preempted;
99 : : #else
100 : : return true;
101 : : #endif /* CONFIG_NUM_METAIRQ_PRIORITIES > 0 */
102 : : }
103 : :
104 : : /**
105 : : * @brief Clear the metairq preemption record for a thread being halted.
106 : : *
107 : : * If @p thread is the cooperative thread recorded as having been preempted by
108 : : * a metairq, clear that record so that the stale pointer is not used after the
109 : : * thread is suspended or aborted.
110 : : *
111 : : * @param thread Thread that is being halted.
112 : : */
113 : : static inline void z_metairq_preempted_clear(struct k_thread *thread)
114 : : {
115 : : #if (CONFIG_NUM_METAIRQ_PRIORITIES > 0)
116 : : unsigned int cpu_id = 0;
117 : :
118 : : #if defined(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
119 : : cpu_id = thread->base.cpu;
120 : : #endif
121 : : if (_kernel.cpus[cpu_id].metairq_preempted == thread) {
122 : : _kernel.cpus[cpu_id].metairq_preempted = NULL;
123 : : }
124 : : #else
125 : : ARG_UNUSED(thread);
126 : : #endif /* CONFIG_NUM_METAIRQ_PRIORITIES > 0 */
127 : : }
128 : :
129 : : #endif /* ZEPHYR_KERNEL_INCLUDE_METAIRQ_H_ */
|