Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2018 Intel Corporation |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5 | */ |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 6 | #include <zephyr/kernel.h> |
Benjamin Walsh | b4b108d | 2016-10-13 10:31:48 -0400 | [diff] [blame] | 7 | #include <ksched.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 8 | #include <zephyr/spinlock.h> |
| 9 | #include <zephyr/kernel/sched_priq.h> |
| 10 | #include <zephyr/wait_q.h> |
Andy Ross | 9c62cc6 | 2018-01-25 15:24:15 -0800 | [diff] [blame] | 11 | #include <kswap.h> |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 12 | #include <kernel_arch_func.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 13 | #include <zephyr/syscall_handler.h> |
| 14 | #include <zephyr/drivers/timer/system_timer.h> |
Flavio Ceolin | 8041860 | 2018-11-21 16:22:15 -0800 | [diff] [blame] | 15 | #include <stdbool.h> |
Andrew Boie | fe03161 | 2019-09-21 17:54:37 -0700 | [diff] [blame] | 16 | #include <kernel_internal.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 17 | #include <zephyr/logging/log.h> |
| 18 | #include <zephyr/sys/atomic.h> |
| 19 | #include <zephyr/sys/math_extras.h> |
| 20 | #include <zephyr/timing/timing.h> |
Andy Ross | 5235145 | 2021-09-28 09:38:43 -0700 | [diff] [blame] | 21 | |
Krzysztof Chruscinski | 3ed8083 | 2020-11-26 19:32:34 +0100 | [diff] [blame] | 22 | LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 23 | |
Andy Ross | 225c74b | 2018-06-27 11:20:50 -0700 | [diff] [blame] | 24 | #if defined(CONFIG_SCHED_DUMB) |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 25 | #define _priq_run_add z_priq_dumb_add |
| 26 | #define _priq_run_remove z_priq_dumb_remove |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 27 | # if defined(CONFIG_SCHED_CPU_MASK) |
| 28 | # define _priq_run_best _priq_dumb_mask_best |
| 29 | # else |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 30 | # define _priq_run_best z_priq_dumb_best |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 31 | # endif |
Andy Ross | 225c74b | 2018-06-27 11:20:50 -0700 | [diff] [blame] | 32 | #elif defined(CONFIG_SCHED_SCALABLE) |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 33 | #define _priq_run_add z_priq_rb_add |
| 34 | #define _priq_run_remove z_priq_rb_remove |
| 35 | #define _priq_run_best z_priq_rb_best |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 36 | #elif defined(CONFIG_SCHED_MULTIQ) |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 37 | #define _priq_run_add z_priq_mq_add |
| 38 | #define _priq_run_remove z_priq_mq_remove |
| 39 | #define _priq_run_best z_priq_mq_best |
Jeremy Bettis | fb1c36f | 2021-12-20 16:24:30 -0700 | [diff] [blame] | 40 | static ALWAYS_INLINE void z_priq_mq_add(struct _priq_mq *pq, |
| 41 | struct k_thread *thread); |
| 42 | static ALWAYS_INLINE void z_priq_mq_remove(struct _priq_mq *pq, |
| 43 | struct k_thread *thread); |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
Andy Ross | 225c74b | 2018-06-27 11:20:50 -0700 | [diff] [blame] | 46 | #if defined(CONFIG_WAITQ_SCALABLE) |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 47 | #define z_priq_wait_add z_priq_rb_add |
| 48 | #define _priq_wait_remove z_priq_rb_remove |
| 49 | #define _priq_wait_best z_priq_rb_best |
Andy Ross | 225c74b | 2018-06-27 11:20:50 -0700 | [diff] [blame] | 50 | #elif defined(CONFIG_WAITQ_DUMB) |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 51 | #define z_priq_wait_add z_priq_dumb_add |
| 52 | #define _priq_wait_remove z_priq_dumb_remove |
| 53 | #define _priq_wait_best z_priq_dumb_best |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 54 | #endif |
| 55 | |
Andy Ross | 6b84ab3 | 2021-02-18 10:15:23 -0800 | [diff] [blame] | 56 | struct k_spinlock sched_spinlock; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 57 | |
Maksim Masalski | 78ba2ec | 2021-06-01 15:44:45 +0800 | [diff] [blame] | 58 | static void update_cache(int preempt_ok); |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 59 | static void end_thread(struct k_thread *thread); |
Andrew Boie | 8e0f6a5 | 2020-09-05 11:50:18 -0700 | [diff] [blame] | 60 | |
Peter Mitsis | f8b76f3 | 2021-11-29 09:52:11 -0500 | [diff] [blame] | 61 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 62 | static inline int is_preempt(struct k_thread *thread) |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 63 | { |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 64 | /* explanation in kernel_struct.h */ |
| 65 | return thread->base.preempt <= _PREEMPT_THRESHOLD; |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Andy Ross | 7aa25fa | 2018-05-11 14:02:42 -0700 | [diff] [blame] | 68 | static inline int is_metairq(struct k_thread *thread) |
| 69 | { |
| 70 | #if CONFIG_NUM_METAIRQ_PRIORITIES > 0 |
| 71 | return (thread->base.prio - K_HIGHEST_THREAD_PRIO) |
| 72 | < CONFIG_NUM_METAIRQ_PRIORITIES; |
| 73 | #else |
| 74 | return 0; |
| 75 | #endif |
| 76 | } |
| 77 | |
Anas Nashif | 80e6a97 | 2018-06-23 08:20:34 -0500 | [diff] [blame] | 78 | #if CONFIG_ASSERT |
Flavio Ceolin | 2df02cc | 2019-03-14 14:32:45 -0700 | [diff] [blame] | 79 | static inline bool is_thread_dummy(struct k_thread *thread) |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 80 | { |
Patrik Flykt | 21358ba | 2019-03-28 14:57:54 -0600 | [diff] [blame] | 81 | return (thread->base.thread_state & _THREAD_DUMMY) != 0U; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 82 | } |
Anas Nashif | 80e6a97 | 2018-06-23 08:20:34 -0500 | [diff] [blame] | 83 | #endif |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 84 | |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 85 | /* |
| 86 | * Return value same as e.g. memcmp |
| 87 | * > 0 -> thread 1 priority > thread 2 priority |
| 88 | * = 0 -> thread 1 priority == thread 2 priority |
| 89 | * < 0 -> thread 1 priority < thread 2 priority |
| 90 | * Do not rely on the actual value returned aside from the above. |
| 91 | * (Again, like memcmp.) |
| 92 | */ |
| 93 | int32_t z_sched_prio_cmp(struct k_thread *thread_1, |
| 94 | struct k_thread *thread_2) |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 95 | { |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 96 | /* `prio` is <32b, so the below cannot overflow. */ |
| 97 | int32_t b1 = thread_1->base.prio; |
| 98 | int32_t b2 = thread_2->base.prio; |
| 99 | |
| 100 | if (b1 != b2) { |
| 101 | return b2 - b1; |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | #ifdef CONFIG_SCHED_DEADLINE |
Andy Ross | ef62657 | 2020-07-10 09:43:36 -0700 | [diff] [blame] | 105 | /* If we assume all deadlines live within the same "half" of |
| 106 | * the 32 bit modulus space (this is a documented API rule), |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 107 | * then the latest deadline in the queue minus the earliest is |
Andy Ross | ef62657 | 2020-07-10 09:43:36 -0700 | [diff] [blame] | 108 | * guaranteed to be (2's complement) non-negative. We can |
| 109 | * leverage that to compare the values without having to check |
| 110 | * the current time. |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 111 | */ |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 112 | uint32_t d1 = thread_1->base.prio_deadline; |
| 113 | uint32_t d2 = thread_2->base.prio_deadline; |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 114 | |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 115 | if (d1 != d2) { |
| 116 | /* Sooner deadline means higher effective priority. |
| 117 | * Doing the calculation with unsigned types and casting |
| 118 | * to signed isn't perfect, but at least reduces this |
| 119 | * from UB on overflow to impdef. |
| 120 | */ |
| 121 | return (int32_t) (d2 - d1); |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 122 | } |
| 123 | #endif |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 124 | return 0; |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 127 | static ALWAYS_INLINE bool should_preempt(struct k_thread *thread, |
| 128 | int preempt_ok) |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 129 | { |
Andy Ross | 43553da | 2018-05-31 11:13:49 -0700 | [diff] [blame] | 130 | /* Preemption is OK if it's being explicitly allowed by |
| 131 | * software state (e.g. the thread called k_yield()) |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 132 | */ |
Flavio Ceolin | 8041860 | 2018-11-21 16:22:15 -0800 | [diff] [blame] | 133 | if (preempt_ok != 0) { |
| 134 | return true; |
Andy Ross | 43553da | 2018-05-31 11:13:49 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Andy Ross | 1763a01 | 2019-01-28 10:59:41 -0800 | [diff] [blame] | 137 | __ASSERT(_current != NULL, ""); |
| 138 | |
Andy Ross | 43553da | 2018-05-31 11:13:49 -0700 | [diff] [blame] | 139 | /* Or if we're pended/suspended/dummy (duh) */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 140 | if (z_is_thread_prevented_from_running(_current)) { |
Andy Ross | 23c5a63 | 2019-01-04 12:52:17 -0800 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
| 144 | /* Edge case on ARM where a thread can be pended out of an |
| 145 | * interrupt handler before the "synchronous" swap starts |
| 146 | * context switching. Platforms with atomic swap can never |
| 147 | * hit this. |
| 148 | */ |
| 149 | if (IS_ENABLED(CONFIG_SWAP_NONATOMIC) |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 150 | && z_is_thread_timeout_active(thread)) { |
Flavio Ceolin | 8041860 | 2018-11-21 16:22:15 -0800 | [diff] [blame] | 151 | return true; |
Andy Ross | 43553da | 2018-05-31 11:13:49 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* Otherwise we have to be running a preemptible thread or |
| 155 | * switching to a metairq |
| 156 | */ |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 157 | if (is_preempt(_current) || is_metairq(thread)) { |
Flavio Ceolin | 8041860 | 2018-11-21 16:22:15 -0800 | [diff] [blame] | 158 | return true; |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Flavio Ceolin | 8041860 | 2018-11-21 16:22:15 -0800 | [diff] [blame] | 161 | return false; |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 164 | #ifdef CONFIG_SCHED_CPU_MASK |
| 165 | static ALWAYS_INLINE struct k_thread *_priq_dumb_mask_best(sys_dlist_t *pq) |
| 166 | { |
| 167 | /* With masks enabled we need to be prepared to walk the list |
| 168 | * looking for one we can run |
| 169 | */ |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 170 | struct k_thread *thread; |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 171 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 172 | SYS_DLIST_FOR_EACH_CONTAINER(pq, thread, base.qnode_dlist) { |
| 173 | if ((thread->base.cpu_mask & BIT(_current_cpu->id)) != 0) { |
| 174 | return thread; |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | return NULL; |
| 178 | } |
| 179 | #endif |
| 180 | |
Peter Mitsis | f8b76f3 | 2021-11-29 09:52:11 -0500 | [diff] [blame] | 181 | static ALWAYS_INLINE void z_priq_dumb_add(sys_dlist_t *pq, |
| 182 | struct k_thread *thread) |
Andy Ross | 0d763e0 | 2021-09-07 15:34:04 -0700 | [diff] [blame] | 183 | { |
| 184 | struct k_thread *t; |
| 185 | |
| 186 | __ASSERT_NO_MSG(!z_is_idle_thread_object(thread)); |
| 187 | |
| 188 | SYS_DLIST_FOR_EACH_CONTAINER(pq, t, base.qnode_dlist) { |
| 189 | if (z_sched_prio_cmp(thread, t) > 0) { |
| 190 | sys_dlist_insert(&t->base.qnode_dlist, |
| 191 | &thread->base.qnode_dlist); |
| 192 | return; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | sys_dlist_append(pq, &thread->base.qnode_dlist); |
| 197 | } |
| 198 | |
Andy Ross | b11e796 | 2021-09-24 10:57:39 -0700 | [diff] [blame] | 199 | static ALWAYS_INLINE void *thread_runq(struct k_thread *thread) |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 200 | { |
Andy Ross | b11e796 | 2021-09-24 10:57:39 -0700 | [diff] [blame] | 201 | #ifdef CONFIG_SCHED_CPU_MASK_PIN_ONLY |
| 202 | int cpu, m = thread->base.cpu_mask; |
| 203 | |
| 204 | /* Edge case: it's legal per the API to "make runnable" a |
| 205 | * thread with all CPUs masked off (i.e. one that isn't |
| 206 | * actually runnable!). Sort of a wart in the API and maybe |
| 207 | * we should address this in docs/assertions instead to avoid |
| 208 | * the extra test. |
| 209 | */ |
| 210 | cpu = m == 0 ? 0 : u32_count_trailing_zeros(m); |
| 211 | |
| 212 | return &_kernel.cpus[cpu].ready_q.runq; |
| 213 | #else |
| 214 | return &_kernel.ready_q.runq; |
| 215 | #endif |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Andy Ross | b11e796 | 2021-09-24 10:57:39 -0700 | [diff] [blame] | 218 | static ALWAYS_INLINE void *curr_cpu_runq(void) |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 219 | { |
Andy Ross | b11e796 | 2021-09-24 10:57:39 -0700 | [diff] [blame] | 220 | #ifdef CONFIG_SCHED_CPU_MASK_PIN_ONLY |
| 221 | return &arch_curr_cpu()->ready_q.runq; |
| 222 | #else |
| 223 | return &_kernel.ready_q.runq; |
| 224 | #endif |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Andy Ross | b11e796 | 2021-09-24 10:57:39 -0700 | [diff] [blame] | 227 | static ALWAYS_INLINE void runq_add(struct k_thread *thread) |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 228 | { |
Andy Ross | b11e796 | 2021-09-24 10:57:39 -0700 | [diff] [blame] | 229 | _priq_run_add(thread_runq(thread), thread); |
| 230 | } |
| 231 | |
| 232 | static ALWAYS_INLINE void runq_remove(struct k_thread *thread) |
| 233 | { |
| 234 | _priq_run_remove(thread_runq(thread), thread); |
| 235 | } |
| 236 | |
| 237 | static ALWAYS_INLINE struct k_thread *runq_best(void) |
| 238 | { |
| 239 | return _priq_run_best(curr_cpu_runq()); |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 242 | /* _current is never in the run queue until context switch on |
| 243 | * SMP configurations, see z_requeue_current() |
| 244 | */ |
| 245 | static inline bool should_queue_thread(struct k_thread *th) |
| 246 | { |
| 247 | return !IS_ENABLED(CONFIG_SMP) || th != _current; |
| 248 | } |
| 249 | |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 250 | static ALWAYS_INLINE void queue_thread(struct k_thread *thread) |
Andy Ross | 91946ef | 2021-02-07 13:03:09 -0800 | [diff] [blame] | 251 | { |
| 252 | thread->base.thread_state |= _THREAD_QUEUED; |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 253 | if (should_queue_thread(thread)) { |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 254 | runq_add(thread); |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 255 | } |
| 256 | #ifdef CONFIG_SMP |
| 257 | if (thread == _current) { |
| 258 | /* add current to end of queue means "yield" */ |
| 259 | _current_cpu->swap_ok = true; |
| 260 | } |
| 261 | #endif |
Andy Ross | 91946ef | 2021-02-07 13:03:09 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 264 | static ALWAYS_INLINE void dequeue_thread(struct k_thread *thread) |
Andy Ross | 91946ef | 2021-02-07 13:03:09 -0800 | [diff] [blame] | 265 | { |
| 266 | thread->base.thread_state &= ~_THREAD_QUEUED; |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 267 | if (should_queue_thread(thread)) { |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 268 | runq_remove(thread); |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 269 | } |
Andy Ross | 91946ef | 2021-02-07 13:03:09 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Andy Ross | b4e9ef0 | 2022-04-06 10:10:17 -0700 | [diff] [blame] | 272 | static void signal_pending_ipi(void) |
| 273 | { |
| 274 | /* Synchronization note: you might think we need to lock these |
| 275 | * two steps, but an IPI is idempotent. It's OK if we do it |
| 276 | * twice. All we require is that if a CPU sees the flag true, |
| 277 | * it is guaranteed to send the IPI, and if a core sets |
| 278 | * pending_ipi, the IPI will be sent the next time through |
| 279 | * this code. |
| 280 | */ |
| 281 | #if defined(CONFIG_SMP) && defined(CONFIG_SCHED_IPI_SUPPORTED) |
| 282 | if (CONFIG_MP_NUM_CPUS > 1) { |
| 283 | if (_kernel.pending_ipi) { |
| 284 | _kernel.pending_ipi = false; |
| 285 | arch_sched_ipi(); |
| 286 | } |
| 287 | } |
| 288 | #endif |
| 289 | } |
| 290 | |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 291 | #ifdef CONFIG_SMP |
| 292 | /* Called out of z_swap() when CONFIG_SMP. The current thread can |
| 293 | * never live in the run queue until we are inexorably on the context |
| 294 | * switch path on SMP, otherwise there is a deadlock condition where a |
| 295 | * set of CPUs pick a cycle of threads to run and wait for them all to |
| 296 | * context switch forever. |
| 297 | */ |
| 298 | void z_requeue_current(struct k_thread *curr) |
| 299 | { |
Andy Ross | 6b84ab3 | 2021-02-18 10:15:23 -0800 | [diff] [blame] | 300 | if (z_is_thread_queued(curr)) { |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 301 | runq_add(curr); |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 302 | } |
Andy Ross | b4e9ef0 | 2022-04-06 10:10:17 -0700 | [diff] [blame] | 303 | signal_pending_ipi(); |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 304 | } |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 305 | |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 306 | static inline bool is_aborting(struct k_thread *thread) |
| 307 | { |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 308 | return (thread->base.thread_state & _THREAD_ABORTING) != 0U; |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 309 | } |
Jeremy Bettis | 1e0a36c | 2021-12-06 10:56:33 -0700 | [diff] [blame] | 310 | #endif |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 311 | |
Andy Ross | b2791b0 | 2019-01-28 09:36:36 -0800 | [diff] [blame] | 312 | static ALWAYS_INLINE struct k_thread *next_up(void) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 313 | { |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 314 | struct k_thread *thread = runq_best(); |
Andy Ross | 11a050b | 2019-11-13 09:41:52 -0800 | [diff] [blame] | 315 | |
| 316 | #if (CONFIG_NUM_METAIRQ_PRIORITIES > 0) && (CONFIG_NUM_COOP_PRIORITIES > 0) |
| 317 | /* MetaIRQs must always attempt to return back to a |
| 318 | * cooperative thread they preempted and not whatever happens |
| 319 | * to be highest priority now. The cooperative thread was |
| 320 | * promised it wouldn't be preempted (by non-metairq threads)! |
| 321 | */ |
| 322 | struct k_thread *mirqp = _current_cpu->metairq_preempted; |
| 323 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 324 | if (mirqp != NULL && (thread == NULL || !is_metairq(thread))) { |
Andy Ross | 11a050b | 2019-11-13 09:41:52 -0800 | [diff] [blame] | 325 | if (!z_is_thread_prevented_from_running(mirqp)) { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 326 | thread = mirqp; |
Andy Ross | 11a050b | 2019-11-13 09:41:52 -0800 | [diff] [blame] | 327 | } else { |
| 328 | _current_cpu->metairq_preempted = NULL; |
| 329 | } |
| 330 | } |
| 331 | #endif |
| 332 | |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 333 | #ifndef CONFIG_SMP |
| 334 | /* In uniprocessor mode, we can leave the current thread in |
| 335 | * the queue (actually we have to, otherwise the assembly |
| 336 | * context switch code for all architectures would be |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 337 | * responsible for putting it back in z_swap and ISR return!), |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 338 | * which makes this choice simple. |
| 339 | */ |
Anas Nashif | 3f4f3f6 | 2021-03-29 17:13:47 -0400 | [diff] [blame] | 340 | return (thread != NULL) ? thread : _current_cpu->idle_thread; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 341 | #else |
| 342 | /* Under SMP, the "cache" mechanism for selecting the next |
| 343 | * thread doesn't work, so we have more work to do to test |
Andy Ross | 11a050b | 2019-11-13 09:41:52 -0800 | [diff] [blame] | 344 | * _current against the best choice from the queue. Here, the |
| 345 | * thread selected above represents "the best thread that is |
| 346 | * not current". |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 347 | * |
| 348 | * Subtle note on "queued": in SMP mode, _current does not |
| 349 | * live in the queue, so this isn't exactly the same thing as |
| 350 | * "ready", it means "is _current already added back to the |
| 351 | * queue such that we don't want to re-add it". |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 352 | */ |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 353 | if (is_aborting(_current)) { |
| 354 | end_thread(_current); |
| 355 | } |
| 356 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 357 | int queued = z_is_thread_queued(_current); |
| 358 | int active = !z_is_thread_prevented_from_running(_current); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 359 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 360 | if (thread == NULL) { |
| 361 | thread = _current_cpu->idle_thread; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 364 | if (active) { |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 365 | int32_t cmp = z_sched_prio_cmp(_current, thread); |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 366 | |
| 367 | /* Ties only switch if state says we yielded */ |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 368 | if ((cmp > 0) || ((cmp == 0) && !_current_cpu->swap_ok)) { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 369 | thread = _current; |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 372 | if (!should_preempt(thread, _current_cpu->swap_ok)) { |
| 373 | thread = _current; |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 374 | } |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 377 | /* Put _current back into the queue */ |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 378 | if (thread != _current && active && |
| 379 | !z_is_idle_thread_object(_current) && !queued) { |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 380 | queue_thread(_current); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 383 | /* Take the new _current out of the queue */ |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 384 | if (z_is_thread_queued(thread)) { |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 385 | dequeue_thread(thread); |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 386 | } |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 387 | |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 388 | _current_cpu->swap_ok = false; |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 389 | return thread; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 390 | #endif |
| 391 | } |
| 392 | |
Andrew Boie | 8e0f6a5 | 2020-09-05 11:50:18 -0700 | [diff] [blame] | 393 | static void move_thread_to_end_of_prio_q(struct k_thread *thread) |
| 394 | { |
| 395 | if (z_is_thread_queued(thread)) { |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 396 | dequeue_thread(thread); |
Andrew Boie | 8e0f6a5 | 2020-09-05 11:50:18 -0700 | [diff] [blame] | 397 | } |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 398 | queue_thread(thread); |
Andrew Boie | 8e0f6a5 | 2020-09-05 11:50:18 -0700 | [diff] [blame] | 399 | update_cache(thread == _current); |
| 400 | } |
| 401 | |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 402 | #ifdef CONFIG_TIMESLICING |
| 403 | |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 404 | static int slice_ticks; |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 405 | static int slice_max_prio; |
| 406 | |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 407 | static inline int slice_time(struct k_thread *curr) |
| 408 | { |
| 409 | int ret = slice_ticks; |
| 410 | |
| 411 | #ifdef CONFIG_TIMESLICE_PER_THREAD |
| 412 | if (curr->base.slice_ticks != 0) { |
| 413 | ret = curr->base.slice_ticks; |
| 414 | } |
| 415 | #endif |
| 416 | return ret; |
| 417 | } |
| 418 | |
Andy Ross | 7fb8eb5 | 2019-01-04 12:54:23 -0800 | [diff] [blame] | 419 | #ifdef CONFIG_SWAP_NONATOMIC |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 420 | /* If z_swap() isn't atomic, then it's possible for a timer interrupt |
Andy Ross | 7fb8eb5 | 2019-01-04 12:54:23 -0800 | [diff] [blame] | 421 | * to try to timeslice away _current after it has already pended |
| 422 | * itself but before the corresponding context switch. Treat that as |
| 423 | * a noop condition in z_time_slice(). |
| 424 | */ |
| 425 | static struct k_thread *pending_current; |
| 426 | #endif |
| 427 | |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 428 | void z_reset_time_slice(struct k_thread *curr) |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 429 | { |
Andy Ross | 7a035c0 | 2018-10-04 09:26:11 -0700 | [diff] [blame] | 430 | /* Add the elapsed time since the last announced tick to the |
| 431 | * slice count, as we'll see those "expired" ticks arrive in a |
| 432 | * FUTURE z_time_slice() call. |
| 433 | */ |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 434 | if (slice_time(curr) != 0) { |
| 435 | _current_cpu->slice_ticks = slice_time(curr) + sys_clock_elapsed(); |
| 436 | z_set_timeout_expiry(slice_time(curr), false); |
Andy Ross | ed7d863 | 2019-06-15 19:32:04 -0700 | [diff] [blame] | 437 | } |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 440 | void k_sched_time_slice_set(int32_t slice, int prio) |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 441 | { |
Patrik Flykt | cf2d579 | 2019-02-12 15:50:46 -0700 | [diff] [blame] | 442 | LOCKED(&sched_spinlock) { |
Andy Ross | 1c30514 | 2018-10-15 11:10:49 -0700 | [diff] [blame] | 443 | _current_cpu->slice_ticks = 0; |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 444 | slice_ticks = k_ms_to_ticks_ceil32(slice); |
Andy Ross | 419f370 | 2021-02-22 15:42:49 -0800 | [diff] [blame] | 445 | if (IS_ENABLED(CONFIG_TICKLESS_KERNEL) && slice > 0) { |
| 446 | /* It's not possible to reliably set a 1-tick |
| 447 | * timeout if ticks aren't regular. |
| 448 | */ |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 449 | slice_ticks = MAX(2, slice_ticks); |
Andy Ross | 419f370 | 2021-02-22 15:42:49 -0800 | [diff] [blame] | 450 | } |
Andy Ross | 1c30514 | 2018-10-15 11:10:49 -0700 | [diff] [blame] | 451 | slice_max_prio = prio; |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 452 | z_reset_time_slice(_current); |
Andy Ross | 1c30514 | 2018-10-15 11:10:49 -0700 | [diff] [blame] | 453 | } |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 456 | #ifdef CONFIG_TIMESLICE_PER_THREAD |
| 457 | void k_thread_time_slice_set(struct k_thread *th, int32_t slice_ticks, |
| 458 | k_thread_timeslice_fn_t expired, void *data) |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 459 | { |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 460 | LOCKED(&sched_spinlock) { |
| 461 | th->base.slice_ticks = slice_ticks; |
| 462 | th->base.slice_expired = expired; |
| 463 | th->base.slice_data = data; |
| 464 | } |
| 465 | } |
| 466 | #endif |
| 467 | |
| 468 | static inline bool sliceable(struct k_thread *thread) |
| 469 | { |
| 470 | bool ret = is_preempt(thread) |
Andrew Boie | 83d7770 | 2020-09-05 11:46:46 -0700 | [diff] [blame] | 471 | && !z_is_thread_prevented_from_running(thread) |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 472 | && !z_is_prio_higher(thread->base.prio, slice_max_prio) |
Andrew Boie | 83d7770 | 2020-09-05 11:46:46 -0700 | [diff] [blame] | 473 | && !z_is_idle_thread_object(thread); |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 474 | |
| 475 | #ifdef CONFIG_TIMESLICE_PER_THREAD |
| 476 | ret |= thread->base.slice_ticks != 0; |
| 477 | #endif |
| 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static k_spinlock_key_t slice_expired_locked(k_spinlock_key_t sched_lock_key) |
| 483 | { |
| 484 | struct k_thread *curr = _current; |
| 485 | |
| 486 | #ifdef CONFIG_TIMESLICE_PER_THREAD |
| 487 | if (curr->base.slice_expired) { |
| 488 | k_spin_unlock(&sched_spinlock, sched_lock_key); |
| 489 | curr->base.slice_expired(curr, curr->base.slice_data); |
| 490 | sched_lock_key = k_spin_lock(&sched_spinlock); |
| 491 | } |
| 492 | #endif |
| 493 | if (!z_is_thread_prevented_from_running(curr)) { |
| 494 | move_thread_to_end_of_prio_q(curr); |
| 495 | } |
| 496 | z_reset_time_slice(curr); |
| 497 | |
| 498 | return sched_lock_key; |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* Called out of each timer interrupt */ |
| 502 | void z_time_slice(int ticks) |
| 503 | { |
Andrew Boie | 8e0f6a5 | 2020-09-05 11:50:18 -0700 | [diff] [blame] | 504 | /* Hold sched_spinlock, so that activity on another CPU |
| 505 | * (like a call to k_thread_abort() at just the wrong time) |
| 506 | * won't affect the correctness of the decisions made here. |
| 507 | * Also prevents any nested interrupts from changing |
| 508 | * thread state to avoid similar issues, since this would |
| 509 | * normally run with IRQs enabled. |
| 510 | */ |
| 511 | k_spinlock_key_t key = k_spin_lock(&sched_spinlock); |
| 512 | |
Andy Ross | 7fb8eb5 | 2019-01-04 12:54:23 -0800 | [diff] [blame] | 513 | #ifdef CONFIG_SWAP_NONATOMIC |
| 514 | if (pending_current == _current) { |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 515 | z_reset_time_slice(_current); |
Andrew Boie | 8e0f6a5 | 2020-09-05 11:50:18 -0700 | [diff] [blame] | 516 | k_spin_unlock(&sched_spinlock, key); |
Andy Ross | 7fb8eb5 | 2019-01-04 12:54:23 -0800 | [diff] [blame] | 517 | return; |
| 518 | } |
| 519 | pending_current = NULL; |
| 520 | #endif |
| 521 | |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 522 | if (slice_time(_current) && sliceable(_current)) { |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 523 | if (ticks >= _current_cpu->slice_ticks) { |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 524 | /* Note: this will (if so enabled) internally |
| 525 | * drop and reacquire the scheduler lock |
| 526 | * around the callback! Don't put anything |
| 527 | * after this line that requires |
| 528 | * synchronization. |
| 529 | */ |
| 530 | key = slice_expired_locked(key); |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 531 | } else { |
| 532 | _current_cpu->slice_ticks -= ticks; |
| 533 | } |
Wentong Wu | 2463ded | 2019-07-24 17:17:33 +0800 | [diff] [blame] | 534 | } else { |
| 535 | _current_cpu->slice_ticks = 0; |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 536 | } |
Andrew Boie | 8e0f6a5 | 2020-09-05 11:50:18 -0700 | [diff] [blame] | 537 | k_spin_unlock(&sched_spinlock, key); |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 538 | } |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 539 | #endif |
| 540 | |
Andy Ross | 11a050b | 2019-11-13 09:41:52 -0800 | [diff] [blame] | 541 | /* Track cooperative threads preempted by metairqs so we can return to |
| 542 | * them specifically. Called at the moment a new thread has been |
| 543 | * selected to run. |
| 544 | */ |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 545 | static void update_metairq_preempt(struct k_thread *thread) |
Andy Ross | 11a050b | 2019-11-13 09:41:52 -0800 | [diff] [blame] | 546 | { |
| 547 | #if (CONFIG_NUM_METAIRQ_PRIORITIES > 0) && (CONFIG_NUM_COOP_PRIORITIES > 0) |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 548 | if (is_metairq(thread) && !is_metairq(_current) && |
| 549 | !is_preempt(_current)) { |
Andy Ross | 11a050b | 2019-11-13 09:41:52 -0800 | [diff] [blame] | 550 | /* Record new preemption */ |
| 551 | _current_cpu->metairq_preempted = _current; |
Andrew Boie | f5a7e1a | 2020-09-02 09:20:38 -0700 | [diff] [blame] | 552 | } else if (!is_metairq(thread) && !z_is_idle_thread_object(thread)) { |
Andy Ross | 11a050b | 2019-11-13 09:41:52 -0800 | [diff] [blame] | 553 | /* Returning from existing preemption */ |
| 554 | _current_cpu->metairq_preempted = NULL; |
| 555 | } |
| 556 | #endif |
| 557 | } |
| 558 | |
Andy Ross | 1856e22 | 2018-05-21 11:48:35 -0700 | [diff] [blame] | 559 | static void update_cache(int preempt_ok) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 560 | { |
| 561 | #ifndef CONFIG_SMP |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 562 | struct k_thread *thread = next_up(); |
Andy Ross | 1856e22 | 2018-05-21 11:48:35 -0700 | [diff] [blame] | 563 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 564 | if (should_preempt(thread, preempt_ok)) { |
Andy Ross | cb3964f | 2019-08-16 21:29:26 -0700 | [diff] [blame] | 565 | #ifdef CONFIG_TIMESLICING |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 566 | if (thread != _current) { |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 567 | z_reset_time_slice(thread); |
Andy Ross | 9098a45 | 2018-09-25 10:56:09 -0700 | [diff] [blame] | 568 | } |
Andy Ross | cb3964f | 2019-08-16 21:29:26 -0700 | [diff] [blame] | 569 | #endif |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 570 | update_metairq_preempt(thread); |
| 571 | _kernel.ready_q.cache = thread; |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 572 | } else { |
| 573 | _kernel.ready_q.cache = _current; |
Andy Ross | 1856e22 | 2018-05-21 11:48:35 -0700 | [diff] [blame] | 574 | } |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 575 | |
| 576 | #else |
| 577 | /* The way this works is that the CPU record keeps its |
| 578 | * "cooperative swapping is OK" flag until the next reschedule |
| 579 | * call or context switch. It doesn't need to be tracked per |
| 580 | * thread because if the thread gets preempted for whatever |
| 581 | * reason the scheduler will make the same decision anyway. |
| 582 | */ |
| 583 | _current_cpu->swap_ok = preempt_ok; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 584 | #endif |
| 585 | } |
| 586 | |
Andy Ross | 05c468f | 2021-02-19 15:24:24 -0800 | [diff] [blame] | 587 | static bool thread_active_elsewhere(struct k_thread *thread) |
| 588 | { |
| 589 | /* True if the thread is currently running on another CPU. |
| 590 | * There are more scalable designs to answer this question in |
| 591 | * constant time, but this is fine for now. |
| 592 | */ |
| 593 | #ifdef CONFIG_SMP |
| 594 | int currcpu = _current_cpu->id; |
| 595 | |
| 596 | for (int i = 0; i < CONFIG_MP_NUM_CPUS; i++) { |
| 597 | if ((i != currcpu) && |
| 598 | (_kernel.cpus[i].current == thread)) { |
| 599 | return true; |
| 600 | } |
| 601 | } |
| 602 | #endif |
| 603 | return false; |
| 604 | } |
| 605 | |
Andy Ross | 3267cd3 | 2022-04-06 09:58:20 -0700 | [diff] [blame] | 606 | static void flag_ipi(void) |
| 607 | { |
Andy Ross | b4e9ef0 | 2022-04-06 10:10:17 -0700 | [diff] [blame] | 608 | #if defined(CONFIG_SMP) && defined(CONFIG_SCHED_IPI_SUPPORTED) |
| 609 | if (CONFIG_MP_NUM_CPUS > 1) { |
| 610 | _kernel.pending_ipi = true; |
| 611 | } |
Andy Ross | 3267cd3 | 2022-04-06 09:58:20 -0700 | [diff] [blame] | 612 | #endif |
| 613 | } |
| 614 | |
Andy Ross | 96ccc46 | 2020-01-23 13:28:30 -0800 | [diff] [blame] | 615 | static void ready_thread(struct k_thread *thread) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 616 | { |
Anas Nashif | 39f632e | 2020-12-07 13:15:42 -0500 | [diff] [blame] | 617 | #ifdef CONFIG_KERNEL_COHERENCE |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 618 | __ASSERT_NO_MSG(arch_mem_coherent(thread)); |
| 619 | #endif |
| 620 | |
Anas Nashif | 081605e | 2020-10-16 20:00:17 -0400 | [diff] [blame] | 621 | /* If thread is queued already, do not try and added it to the |
| 622 | * run queue again |
| 623 | */ |
| 624 | if (!z_is_thread_queued(thread) && z_is_thread_ready(thread)) { |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 625 | SYS_PORT_TRACING_OBJ_FUNC(k_thread, sched_ready, thread); |
| 626 | |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 627 | queue_thread(thread); |
Andy Ross | 1856e22 | 2018-05-21 11:48:35 -0700 | [diff] [blame] | 628 | update_cache(0); |
Andy Ross | 3267cd3 | 2022-04-06 09:58:20 -0700 | [diff] [blame] | 629 | flag_ipi(); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Andy Ross | 96ccc46 | 2020-01-23 13:28:30 -0800 | [diff] [blame] | 633 | void z_ready_thread(struct k_thread *thread) |
| 634 | { |
| 635 | LOCKED(&sched_spinlock) { |
Andy Ross | 05c468f | 2021-02-19 15:24:24 -0800 | [diff] [blame] | 636 | if (!thread_active_elsewhere(thread)) { |
| 637 | ready_thread(thread); |
| 638 | } |
Andy Ross | 96ccc46 | 2020-01-23 13:28:30 -0800 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 642 | void z_move_thread_to_end_of_prio_q(struct k_thread *thread) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 643 | { |
Patrik Flykt | cf2d579 | 2019-02-12 15:50:46 -0700 | [diff] [blame] | 644 | LOCKED(&sched_spinlock) { |
Andrew Boie | 8e0f6a5 | 2020-09-05 11:50:18 -0700 | [diff] [blame] | 645 | move_thread_to_end_of_prio_q(thread); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Andy Ross | 96ccc46 | 2020-01-23 13:28:30 -0800 | [diff] [blame] | 649 | void z_sched_start(struct k_thread *thread) |
| 650 | { |
| 651 | k_spinlock_key_t key = k_spin_lock(&sched_spinlock); |
| 652 | |
| 653 | if (z_has_thread_started(thread)) { |
| 654 | k_spin_unlock(&sched_spinlock, key); |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | z_mark_thread_as_started(thread); |
| 659 | ready_thread(thread); |
| 660 | z_reschedule(&sched_spinlock, key); |
| 661 | } |
| 662 | |
Andrew Boie | 6cf496f | 2020-02-14 10:52:49 -0800 | [diff] [blame] | 663 | void z_impl_k_thread_suspend(struct k_thread *thread) |
Andy Ross | 8bdabcc | 2020-01-07 09:58:46 -0800 | [diff] [blame] | 664 | { |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 665 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_thread, suspend, thread); |
| 666 | |
Andy Ross | 8bdabcc | 2020-01-07 09:58:46 -0800 | [diff] [blame] | 667 | (void)z_abort_thread_timeout(thread); |
| 668 | |
| 669 | LOCKED(&sched_spinlock) { |
| 670 | if (z_is_thread_queued(thread)) { |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 671 | dequeue_thread(thread); |
Andy Ross | 8bdabcc | 2020-01-07 09:58:46 -0800 | [diff] [blame] | 672 | } |
| 673 | z_mark_thread_as_suspended(thread); |
| 674 | update_cache(thread == _current); |
| 675 | } |
| 676 | |
| 677 | if (thread == _current) { |
| 678 | z_reschedule_unlocked(); |
| 679 | } |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 680 | |
| 681 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_thread, suspend, thread); |
Andy Ross | 8bdabcc | 2020-01-07 09:58:46 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Andrew Boie | 6cf496f | 2020-02-14 10:52:49 -0800 | [diff] [blame] | 684 | #ifdef CONFIG_USERSPACE |
| 685 | static inline void z_vrfy_k_thread_suspend(struct k_thread *thread) |
| 686 | { |
| 687 | Z_OOPS(Z_SYSCALL_OBJ(thread, K_OBJ_THREAD)); |
| 688 | z_impl_k_thread_suspend(thread); |
| 689 | } |
| 690 | #include <syscalls/k_thread_suspend_mrsh.c> |
| 691 | #endif |
| 692 | |
| 693 | void z_impl_k_thread_resume(struct k_thread *thread) |
| 694 | { |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 695 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_thread, resume, thread); |
| 696 | |
Andrew Boie | 6cf496f | 2020-02-14 10:52:49 -0800 | [diff] [blame] | 697 | k_spinlock_key_t key = k_spin_lock(&sched_spinlock); |
| 698 | |
Anas Nashif | bf69afc | 2020-10-16 19:53:56 -0400 | [diff] [blame] | 699 | /* Do not try to resume a thread that was not suspended */ |
| 700 | if (!z_is_thread_suspended(thread)) { |
| 701 | k_spin_unlock(&sched_spinlock, key); |
| 702 | return; |
| 703 | } |
| 704 | |
Andrew Boie | 6cf496f | 2020-02-14 10:52:49 -0800 | [diff] [blame] | 705 | z_mark_thread_as_not_suspended(thread); |
| 706 | ready_thread(thread); |
| 707 | |
| 708 | z_reschedule(&sched_spinlock, key); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 709 | |
| 710 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_thread, resume, thread); |
Andrew Boie | 6cf496f | 2020-02-14 10:52:49 -0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | #ifdef CONFIG_USERSPACE |
| 714 | static inline void z_vrfy_k_thread_resume(struct k_thread *thread) |
| 715 | { |
| 716 | Z_OOPS(Z_SYSCALL_OBJ(thread, K_OBJ_THREAD)); |
| 717 | z_impl_k_thread_resume(thread); |
| 718 | } |
| 719 | #include <syscalls/k_thread_resume_mrsh.c> |
| 720 | #endif |
| 721 | |
Maksim Masalski | 970820e | 2021-05-25 14:40:14 +0800 | [diff] [blame] | 722 | static _wait_q_t *pended_on_thread(struct k_thread *thread) |
Andy Ross | 8bdabcc | 2020-01-07 09:58:46 -0800 | [diff] [blame] | 723 | { |
| 724 | __ASSERT_NO_MSG(thread->base.pended_on); |
| 725 | |
| 726 | return thread->base.pended_on; |
| 727 | } |
| 728 | |
Andy Ross | ed6b4fb | 2020-01-23 13:04:15 -0800 | [diff] [blame] | 729 | static void unready_thread(struct k_thread *thread) |
| 730 | { |
| 731 | if (z_is_thread_queued(thread)) { |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 732 | dequeue_thread(thread); |
Andy Ross | ed6b4fb | 2020-01-23 13:04:15 -0800 | [diff] [blame] | 733 | } |
| 734 | update_cache(thread == _current); |
| 735 | } |
| 736 | |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 737 | /* sched_spinlock must be held */ |
| 738 | static void add_to_waitq_locked(struct k_thread *thread, _wait_q_t *wait_q) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 739 | { |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 740 | unready_thread(thread); |
| 741 | z_mark_thread_as_pending(thread); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 742 | |
| 743 | SYS_PORT_TRACING_FUNC(k_thread, sched_pend, thread); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 744 | |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 745 | if (wait_q != NULL) { |
| 746 | thread->base.pended_on = wait_q; |
| 747 | z_priq_wait_add(&wait_q->waitq, thread); |
Andy Ross | 15d5208 | 2018-09-26 13:19:31 -0700 | [diff] [blame] | 748 | } |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 749 | } |
Andy Ross | 15d5208 | 2018-09-26 13:19:31 -0700 | [diff] [blame] | 750 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 751 | static void add_thread_timeout(struct k_thread *thread, k_timeout_t timeout) |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 752 | { |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 753 | if (!K_TIMEOUT_EQ(timeout, K_FOREVER)) { |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 754 | z_add_thread_timeout(thread, timeout); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 755 | } |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 758 | static void pend(struct k_thread *thread, _wait_q_t *wait_q, |
| 759 | k_timeout_t timeout) |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 760 | { |
Anas Nashif | 39f632e | 2020-12-07 13:15:42 -0500 | [diff] [blame] | 761 | #ifdef CONFIG_KERNEL_COHERENCE |
Andy Ross | 1ba7414 | 2021-02-09 13:48:25 -0800 | [diff] [blame] | 762 | __ASSERT_NO_MSG(wait_q == NULL || arch_mem_coherent(wait_q)); |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 763 | #endif |
| 764 | |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 765 | LOCKED(&sched_spinlock) { |
| 766 | add_to_waitq_locked(thread, wait_q); |
| 767 | } |
| 768 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 769 | add_thread_timeout(thread, timeout); |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 770 | } |
| 771 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 772 | void z_pend_thread(struct k_thread *thread, _wait_q_t *wait_q, |
| 773 | k_timeout_t timeout) |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 774 | { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 775 | __ASSERT_NO_MSG(thread == _current || is_thread_dummy(thread)); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 776 | pend(thread, wait_q, timeout); |
| 777 | } |
| 778 | |
Andrew Boie | ffc5bdf | 2020-09-05 11:44:01 -0700 | [diff] [blame] | 779 | static inline void unpend_thread_no_timeout(struct k_thread *thread) |
| 780 | { |
Maksim Masalski | 970820e | 2021-05-25 14:40:14 +0800 | [diff] [blame] | 781 | _priq_wait_remove(&pended_on_thread(thread)->waitq, thread); |
Andrew Boie | ffc5bdf | 2020-09-05 11:44:01 -0700 | [diff] [blame] | 782 | z_mark_thread_as_not_pending(thread); |
| 783 | thread->base.pended_on = NULL; |
| 784 | } |
| 785 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 786 | ALWAYS_INLINE void z_unpend_thread_no_timeout(struct k_thread *thread) |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 787 | { |
Patrik Flykt | cf2d579 | 2019-02-12 15:50:46 -0700 | [diff] [blame] | 788 | LOCKED(&sched_spinlock) { |
Andrew Boie | ffc5bdf | 2020-09-05 11:44:01 -0700 | [diff] [blame] | 789 | unpend_thread_no_timeout(thread); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 790 | } |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 793 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
| 794 | /* Timeout handler for *_thread_timeout() APIs */ |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 795 | void z_thread_timeout(struct _timeout *timeout) |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 796 | { |
Andy Ross | 3786633 | 2021-02-17 10:12:36 -0800 | [diff] [blame] | 797 | struct k_thread *thread = CONTAINER_OF(timeout, |
| 798 | struct k_thread, base.timeout); |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 799 | |
Andy Ross | 3786633 | 2021-02-17 10:12:36 -0800 | [diff] [blame] | 800 | LOCKED(&sched_spinlock) { |
| 801 | bool killed = ((thread->base.thread_state & _THREAD_DEAD) || |
| 802 | (thread->base.thread_state & _THREAD_ABORTING)); |
| 803 | |
| 804 | if (!killed) { |
| 805 | if (thread->base.pended_on != NULL) { |
| 806 | unpend_thread_no_timeout(thread); |
| 807 | } |
| 808 | z_mark_thread_as_started(thread); |
| 809 | z_mark_thread_as_not_suspended(thread); |
| 810 | ready_thread(thread); |
Andrew Boie | ffc5bdf | 2020-09-05 11:44:01 -0700 | [diff] [blame] | 811 | } |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 812 | } |
Andy Ross | 987c0e5 | 2018-09-27 16:50:00 -0700 | [diff] [blame] | 813 | } |
| 814 | #endif |
| 815 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 816 | int z_pend_curr_irqlock(uint32_t key, _wait_q_t *wait_q, k_timeout_t timeout) |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 817 | { |
Andy Ross | 722aeea | 2019-03-14 13:50:16 -0700 | [diff] [blame] | 818 | pend(_current, wait_q, timeout); |
| 819 | |
Andy Ross | 7fb8eb5 | 2019-01-04 12:54:23 -0800 | [diff] [blame] | 820 | #if defined(CONFIG_TIMESLICING) && defined(CONFIG_SWAP_NONATOMIC) |
| 821 | pending_current = _current; |
Andy Ross | 722aeea | 2019-03-14 13:50:16 -0700 | [diff] [blame] | 822 | |
| 823 | int ret = z_swap_irqlock(key); |
| 824 | LOCKED(&sched_spinlock) { |
| 825 | if (pending_current == _current) { |
| 826 | pending_current = NULL; |
| 827 | } |
| 828 | } |
| 829 | return ret; |
| 830 | #else |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 831 | return z_swap_irqlock(key); |
Andy Ross | 722aeea | 2019-03-14 13:50:16 -0700 | [diff] [blame] | 832 | #endif |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 835 | int z_pend_curr(struct k_spinlock *lock, k_spinlock_key_t key, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 836 | _wait_q_t *wait_q, k_timeout_t timeout) |
Andy Ross | ec554f4 | 2018-07-24 13:37:59 -0700 | [diff] [blame] | 837 | { |
| 838 | #if defined(CONFIG_TIMESLICING) && defined(CONFIG_SWAP_NONATOMIC) |
| 839 | pending_current = _current; |
| 840 | #endif |
| 841 | pend(_current, wait_q, timeout); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 842 | return z_swap(lock, key); |
Andy Ross | ec554f4 | 2018-07-24 13:37:59 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Andy Ross | 604f0f4 | 2021-02-09 16:47:47 -0800 | [diff] [blame] | 845 | struct k_thread *z_unpend1_no_timeout(_wait_q_t *wait_q) |
| 846 | { |
| 847 | struct k_thread *thread = NULL; |
| 848 | |
| 849 | LOCKED(&sched_spinlock) { |
| 850 | thread = _priq_wait_best(&wait_q->waitq); |
| 851 | |
| 852 | if (thread != NULL) { |
| 853 | unpend_thread_no_timeout(thread); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | return thread; |
| 858 | } |
| 859 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 860 | struct k_thread *z_unpend_first_thread(_wait_q_t *wait_q) |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 861 | { |
Andy Ross | 604f0f4 | 2021-02-09 16:47:47 -0800 | [diff] [blame] | 862 | struct k_thread *thread = NULL; |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 863 | |
Andy Ross | 604f0f4 | 2021-02-09 16:47:47 -0800 | [diff] [blame] | 864 | LOCKED(&sched_spinlock) { |
| 865 | thread = _priq_wait_best(&wait_q->waitq); |
| 866 | |
| 867 | if (thread != NULL) { |
| 868 | unpend_thread_no_timeout(thread); |
| 869 | (void)z_abort_thread_timeout(thread); |
| 870 | } |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 871 | } |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 872 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 873 | return thread; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 874 | } |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 875 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 876 | void z_unpend_thread(struct k_thread *thread) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 877 | { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 878 | z_unpend_thread_no_timeout(thread); |
| 879 | (void)z_abort_thread_timeout(thread); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Andy Ross | 6f13980 | 2019-08-20 11:21:28 -0700 | [diff] [blame] | 882 | /* Priority set utility that does no rescheduling, it just changes the |
| 883 | * run queue state, returning true if a reschedule is needed later. |
| 884 | */ |
| 885 | bool z_set_prio(struct k_thread *thread, int prio) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 886 | { |
Flavio Ceolin | 02ed85b | 2018-09-20 15:43:57 -0700 | [diff] [blame] | 887 | bool need_sched = 0; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 888 | |
Patrik Flykt | cf2d579 | 2019-02-12 15:50:46 -0700 | [diff] [blame] | 889 | LOCKED(&sched_spinlock) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 890 | need_sched = z_is_thread_ready(thread); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 891 | |
| 892 | if (need_sched) { |
Andy Ross | 4d8e1f2 | 2019-07-01 10:25:55 -0700 | [diff] [blame] | 893 | /* Don't requeue on SMP if it's the running thread */ |
| 894 | if (!IS_ENABLED(CONFIG_SMP) || z_is_thread_queued(thread)) { |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 895 | dequeue_thread(thread); |
Andy Ross | 4d8e1f2 | 2019-07-01 10:25:55 -0700 | [diff] [blame] | 896 | thread->base.prio = prio; |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 897 | queue_thread(thread); |
Andy Ross | 4d8e1f2 | 2019-07-01 10:25:55 -0700 | [diff] [blame] | 898 | } else { |
| 899 | thread->base.prio = prio; |
| 900 | } |
Andy Ross | 1856e22 | 2018-05-21 11:48:35 -0700 | [diff] [blame] | 901 | update_cache(1); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 902 | } else { |
| 903 | thread->base.prio = prio; |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 904 | } |
| 905 | } |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 906 | |
| 907 | SYS_PORT_TRACING_OBJ_FUNC(k_thread, sched_priority_set, thread, prio); |
Andy Ross | e7ded11 | 2018-04-11 14:52:47 -0700 | [diff] [blame] | 908 | |
Andy Ross | 6f13980 | 2019-08-20 11:21:28 -0700 | [diff] [blame] | 909 | return need_sched; |
| 910 | } |
| 911 | |
| 912 | void z_thread_priority_set(struct k_thread *thread, int prio) |
| 913 | { |
| 914 | bool need_sched = z_set_prio(thread, prio); |
| 915 | |
Andy Ross | 3267cd3 | 2022-04-06 09:58:20 -0700 | [diff] [blame] | 916 | flag_ipi(); |
Andy Ross | 5737b5c | 2020-02-04 13:52:09 -0800 | [diff] [blame] | 917 | |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 918 | if (need_sched && _current->base.sched_locked == 0U) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 919 | z_reschedule_unlocked(); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
Anas Nashif | 3f4f3f6 | 2021-03-29 17:13:47 -0400 | [diff] [blame] | 923 | static inline bool resched(uint32_t key) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 924 | { |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 925 | #ifdef CONFIG_SMP |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 926 | _current_cpu->swap_ok = 0; |
| 927 | #endif |
| 928 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 929 | return arch_irq_unlocked(key) && !arch_is_in_isr(); |
Andy Ross | ec554f4 | 2018-07-24 13:37:59 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Anas Nashif | 379b93f | 2020-08-10 15:47:02 -0400 | [diff] [blame] | 932 | /* |
| 933 | * Check if the next ready thread is the same as the current thread |
| 934 | * and save the trip if true. |
| 935 | */ |
| 936 | static inline bool need_swap(void) |
| 937 | { |
| 938 | /* the SMP case will be handled in C based z_swap() */ |
| 939 | #ifdef CONFIG_SMP |
| 940 | return true; |
| 941 | #else |
| 942 | struct k_thread *new_thread; |
| 943 | |
| 944 | /* Check if the next ready thread is the same as the current thread */ |
Andy Ross | 6b84ab3 | 2021-02-18 10:15:23 -0800 | [diff] [blame] | 945 | new_thread = _kernel.ready_q.cache; |
Anas Nashif | 379b93f | 2020-08-10 15:47:02 -0400 | [diff] [blame] | 946 | return new_thread != _current; |
| 947 | #endif |
| 948 | } |
| 949 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 950 | void z_reschedule(struct k_spinlock *lock, k_spinlock_key_t key) |
Andy Ross | ec554f4 | 2018-07-24 13:37:59 -0700 | [diff] [blame] | 951 | { |
Anas Nashif | 379b93f | 2020-08-10 15:47:02 -0400 | [diff] [blame] | 952 | if (resched(key.key) && need_swap()) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 953 | z_swap(lock, key); |
Andy Ross | ec554f4 | 2018-07-24 13:37:59 -0700 | [diff] [blame] | 954 | } else { |
| 955 | k_spin_unlock(lock, key); |
Andy Ross | b4e9ef0 | 2022-04-06 10:10:17 -0700 | [diff] [blame] | 956 | signal_pending_ipi(); |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 957 | } |
Andy Ross | ec554f4 | 2018-07-24 13:37:59 -0700 | [diff] [blame] | 958 | } |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 959 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 960 | void z_reschedule_irqlock(uint32_t key) |
Andy Ross | ec554f4 | 2018-07-24 13:37:59 -0700 | [diff] [blame] | 961 | { |
Andy Ross | 312b43f | 2019-05-24 10:09:13 -0700 | [diff] [blame] | 962 | if (resched(key)) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 963 | z_swap_irqlock(key); |
Andy Ross | ec554f4 | 2018-07-24 13:37:59 -0700 | [diff] [blame] | 964 | } else { |
| 965 | irq_unlock(key); |
Andy Ross | b4e9ef0 | 2022-04-06 10:10:17 -0700 | [diff] [blame] | 966 | signal_pending_ipi(); |
Andy Ross | ec554f4 | 2018-07-24 13:37:59 -0700 | [diff] [blame] | 967 | } |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 970 | void k_sched_lock(void) |
| 971 | { |
Patrik Flykt | cf2d579 | 2019-02-12 15:50:46 -0700 | [diff] [blame] | 972 | LOCKED(&sched_spinlock) { |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 973 | SYS_PORT_TRACING_FUNC(k_thread, sched_lock); |
| 974 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 975 | z_sched_lock(); |
Andy Ross | 1856e22 | 2018-05-21 11:48:35 -0700 | [diff] [blame] | 976 | } |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 977 | } |
| 978 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 979 | void k_sched_unlock(void) |
| 980 | { |
Patrik Flykt | cf2d579 | 2019-02-12 15:50:46 -0700 | [diff] [blame] | 981 | LOCKED(&sched_spinlock) { |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 982 | __ASSERT(_current->base.sched_locked != 0U, ""); |
Andy Ross | eefd3da | 2020-02-06 13:39:52 -0800 | [diff] [blame] | 983 | __ASSERT(!arch_is_in_isr(), ""); |
| 984 | |
Andy Ross | 1856e22 | 2018-05-21 11:48:35 -0700 | [diff] [blame] | 985 | ++_current->base.sched_locked; |
Yasushi SHOJI | 20d0724 | 2019-07-31 11:19:08 +0900 | [diff] [blame] | 986 | update_cache(0); |
Andy Ross | 1856e22 | 2018-05-21 11:48:35 -0700 | [diff] [blame] | 987 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 988 | |
Anas Nashif | 2c5d404 | 2019-12-02 10:24:08 -0500 | [diff] [blame] | 989 | LOG_DBG("scheduler unlocked (%p:%d)", |
Benjamin Walsh | a4e033f | 2016-11-18 16:08:24 -0500 | [diff] [blame] | 990 | _current, _current->base.sched_locked); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 991 | |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 992 | SYS_PORT_TRACING_FUNC(k_thread, sched_unlock); |
| 993 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 994 | z_reschedule_unlocked(); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 995 | } |
| 996 | |
Andy Ross | 6b84ab3 | 2021-02-18 10:15:23 -0800 | [diff] [blame] | 997 | struct k_thread *z_swap_next_thread(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 998 | { |
Andy Ross | 6b84ab3 | 2021-02-18 10:15:23 -0800 | [diff] [blame] | 999 | #ifdef CONFIG_SMP |
Andy Ross | b4e9ef0 | 2022-04-06 10:10:17 -0700 | [diff] [blame] | 1000 | struct k_thread *ret = next_up(); |
| 1001 | |
| 1002 | if (ret == _current) { |
| 1003 | /* When not swapping, have to signal IPIs here. In |
| 1004 | * the context switch case it must happen later, after |
| 1005 | * _current gets requeued. |
| 1006 | */ |
| 1007 | signal_pending_ipi(); |
| 1008 | } |
| 1009 | return ret; |
Andy Ross | 6b84ab3 | 2021-02-18 10:15:23 -0800 | [diff] [blame] | 1010 | #else |
| 1011 | return _kernel.ready_q.cache; |
Benjamin Walsh | 6209218 | 2016-12-20 14:39:08 -0500 | [diff] [blame] | 1012 | #endif |
Andy Ross | 6b84ab3 | 2021-02-18 10:15:23 -0800 | [diff] [blame] | 1013 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1014 | |
Jeremy Bettis | 1e0a36c | 2021-12-06 10:56:33 -0700 | [diff] [blame] | 1015 | #ifdef CONFIG_USE_SWITCH |
Andy Ross | b18685b | 2019-02-19 17:24:30 -0800 | [diff] [blame] | 1016 | /* Just a wrapper around _current = xxx with tracing */ |
| 1017 | static inline void set_current(struct k_thread *new_thread) |
| 1018 | { |
Daniel Leung | 11e6b43 | 2020-08-27 16:12:01 -0700 | [diff] [blame] | 1019 | z_thread_mark_switched_out(); |
Andy Ross | eefd3da | 2020-02-06 13:39:52 -0800 | [diff] [blame] | 1020 | _current_cpu->current = new_thread; |
Andy Ross | b18685b | 2019-02-19 17:24:30 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
Nicolas Pitre | c9e3e0d | 2022-03-15 22:36:20 -0400 | [diff] [blame] | 1023 | /** |
| 1024 | * @brief Determine next thread to execute upon completion of an interrupt |
| 1025 | * |
| 1026 | * Thread preemption is performed by context switching after the completion |
| 1027 | * of a non-recursed interrupt. This function determines which thread to |
| 1028 | * switch to if any. This function accepts as @p interrupted either: |
| 1029 | * |
| 1030 | * - The handle for the interrupted thread in which case the thread's context |
| 1031 | * must already be fully saved and ready to be picked up by a different CPU. |
| 1032 | * |
| 1033 | * - NULL if more work is required to fully save the thread's state after |
| 1034 | * it is known that a new thread is to be scheduled. It is up to the caller |
| 1035 | * to store the handle resulting from the thread that is being switched out |
| 1036 | * in that thread's "switch_handle" field after its |
| 1037 | * context has fully been saved, following the same requirements as with |
| 1038 | * the @ref arch_switch() function. |
| 1039 | * |
| 1040 | * If a new thread needs to be scheduled then its handle is returned. |
| 1041 | * Otherwise the same value provided as @p interrupted is returned back. |
| 1042 | * Those handles are the same opaque types used by the @ref arch_switch() |
| 1043 | * function. |
| 1044 | * |
| 1045 | * @warning |
| 1046 | * The @ref _current value may have changed after this call and not refer |
| 1047 | * to the interrupted thread anymore. It might be necessary to make a local |
| 1048 | * copy before calling this function. |
| 1049 | * |
| 1050 | * @param interrupted Handle for the thread that was interrupted or NULL. |
| 1051 | * @retval Handle for the next thread to execute, or @p interrupted when |
| 1052 | * no new thread is to be scheduled. |
| 1053 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1054 | void *z_get_next_switch_handle(void *interrupted) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1055 | { |
Andrew Boie | ae0d1b2 | 2019-03-29 16:25:27 -0700 | [diff] [blame] | 1056 | z_check_stack_sentinel(); |
| 1057 | |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 1058 | #ifdef CONFIG_SMP |
Andy Ross | dd43221 | 2021-02-05 08:15:02 -0800 | [diff] [blame] | 1059 | void *ret = NULL; |
| 1060 | |
Patrik Flykt | cf2d579 | 2019-02-12 15:50:46 -0700 | [diff] [blame] | 1061 | LOCKED(&sched_spinlock) { |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 1062 | struct k_thread *old_thread = _current, *new_thread; |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 1063 | |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 1064 | if (IS_ENABLED(CONFIG_SMP)) { |
| 1065 | old_thread->switch_handle = NULL; |
| 1066 | } |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 1067 | new_thread = next_up(); |
| 1068 | |
Andy Ross | 40d12c1 | 2021-09-27 08:22:43 -0700 | [diff] [blame] | 1069 | z_sched_usage_switch(new_thread); |
| 1070 | |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 1071 | if (old_thread != new_thread) { |
| 1072 | update_metairq_preempt(new_thread); |
| 1073 | wait_for_switch(new_thread); |
| 1074 | arch_cohere_stacks(old_thread, interrupted, new_thread); |
Andy Ross | 11a050b | 2019-11-13 09:41:52 -0800 | [diff] [blame] | 1075 | |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 1076 | _current_cpu->swap_ok = 0; |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 1077 | set_current(new_thread); |
| 1078 | |
Andy Ross | 3e69689 | 2021-11-30 18:26:26 -0800 | [diff] [blame] | 1079 | #ifdef CONFIG_TIMESLICING |
| 1080 | z_reset_time_slice(new_thread); |
| 1081 | #endif |
| 1082 | |
Danny Oerndrup | c9d7840 | 2019-12-13 11:24:56 +0100 | [diff] [blame] | 1083 | #ifdef CONFIG_SPIN_VALIDATE |
Andy Ross | 8c1bdda | 2019-02-20 10:07:31 -0800 | [diff] [blame] | 1084 | /* Changed _current! Update the spinlock |
Anas Nashif | 6df4405 | 2021-04-30 09:58:20 -0400 | [diff] [blame] | 1085 | * bookkeeping so the validation doesn't get |
Andy Ross | 8c1bdda | 2019-02-20 10:07:31 -0800 | [diff] [blame] | 1086 | * confused when the "wrong" thread tries to |
| 1087 | * release the lock. |
| 1088 | */ |
| 1089 | z_spin_lock_set_owner(&sched_spinlock); |
| 1090 | #endif |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 1091 | |
| 1092 | /* A queued (runnable) old/current thread |
| 1093 | * needs to be added back to the run queue |
| 1094 | * here, and atomically with its switch handle |
| 1095 | * being set below. This is safe now, as we |
| 1096 | * will not return into it. |
| 1097 | */ |
| 1098 | if (z_is_thread_queued(old_thread)) { |
Andy Ross | 387fdd2 | 2021-09-23 18:44:40 -0700 | [diff] [blame] | 1099 | runq_add(old_thread); |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 1100 | } |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 1101 | } |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 1102 | old_thread->switch_handle = interrupted; |
Andy Ross | dd43221 | 2021-02-05 08:15:02 -0800 | [diff] [blame] | 1103 | ret = new_thread->switch_handle; |
Andy Ross | 4ff4571 | 2021-02-08 08:28:54 -0800 | [diff] [blame] | 1104 | if (IS_ENABLED(CONFIG_SMP)) { |
| 1105 | /* Active threads MUST have a null here */ |
| 1106 | new_thread->switch_handle = NULL; |
| 1107 | } |
Benjamin Walsh | b8c2160 | 2016-12-23 19:34:41 -0500 | [diff] [blame] | 1108 | } |
Andy Ross | b4e9ef0 | 2022-04-06 10:10:17 -0700 | [diff] [blame] | 1109 | signal_pending_ipi(); |
Andy Ross | dd43221 | 2021-02-05 08:15:02 -0800 | [diff] [blame] | 1110 | return ret; |
Andy Ross | eace1df | 2018-05-30 11:23:02 -0700 | [diff] [blame] | 1111 | #else |
Andy Ross | 40d12c1 | 2021-09-27 08:22:43 -0700 | [diff] [blame] | 1112 | z_sched_usage_switch(_kernel.ready_q.cache); |
Andy Ross | f6d32ab | 2020-05-13 15:34:04 +0000 | [diff] [blame] | 1113 | _current->switch_handle = interrupted; |
Andy Ross | 6b84ab3 | 2021-02-18 10:15:23 -0800 | [diff] [blame] | 1114 | set_current(_kernel.ready_q.cache); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1115 | return _current->switch_handle; |
Andy Ross | dd43221 | 2021-02-05 08:15:02 -0800 | [diff] [blame] | 1116 | #endif |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1117 | } |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 1118 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1119 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1120 | void z_priq_dumb_remove(sys_dlist_t *pq, struct k_thread *thread) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1121 | { |
Andrew Boie | 8f0bb6a | 2019-09-21 18:36:23 -0700 | [diff] [blame] | 1122 | __ASSERT_NO_MSG(!z_is_idle_thread_object(thread)); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1123 | |
| 1124 | sys_dlist_remove(&thread->base.qnode_dlist); |
| 1125 | } |
| 1126 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1127 | struct k_thread *z_priq_dumb_best(sys_dlist_t *pq) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1128 | { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1129 | struct k_thread *thread = NULL; |
Flavio Ceolin | 26be335 | 2018-11-15 15:03:32 -0800 | [diff] [blame] | 1130 | sys_dnode_t *n = sys_dlist_peek_head(pq); |
| 1131 | |
Peter A. Bigot | 692e103 | 2019-01-03 23:36:28 -0600 | [diff] [blame] | 1132 | if (n != NULL) { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1133 | thread = CONTAINER_OF(n, struct k_thread, base.qnode_dlist); |
Peter A. Bigot | 692e103 | 2019-01-03 23:36:28 -0600 | [diff] [blame] | 1134 | } |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1135 | return thread; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1138 | bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1139 | { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1140 | struct k_thread *thread_a, *thread_b; |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 1141 | int32_t cmp; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1142 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1143 | thread_a = CONTAINER_OF(a, struct k_thread, base.qnode_rb); |
| 1144 | thread_b = CONTAINER_OF(b, struct k_thread, base.qnode_rb); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1145 | |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 1146 | cmp = z_sched_prio_cmp(thread_a, thread_b); |
| 1147 | |
| 1148 | if (cmp > 0) { |
Flavio Ceolin | 02ed85b | 2018-09-20 15:43:57 -0700 | [diff] [blame] | 1149 | return true; |
James Harris | 2cd0f66 | 2021-03-01 09:19:57 -0800 | [diff] [blame] | 1150 | } else if (cmp < 0) { |
Flavio Ceolin | 02ed85b | 2018-09-20 15:43:57 -0700 | [diff] [blame] | 1151 | return false; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1152 | } else { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1153 | return thread_a->base.order_key < thread_b->base.order_key |
| 1154 | ? 1 : 0; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1158 | void z_priq_rb_add(struct _priq_rb *pq, struct k_thread *thread) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1159 | { |
| 1160 | struct k_thread *t; |
| 1161 | |
Andrew Boie | 8f0bb6a | 2019-09-21 18:36:23 -0700 | [diff] [blame] | 1162 | __ASSERT_NO_MSG(!z_is_idle_thread_object(thread)); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1163 | |
| 1164 | thread->base.order_key = pq->next_order_key++; |
| 1165 | |
| 1166 | /* Renumber at wraparound. This is tiny code, and in practice |
| 1167 | * will almost never be hit on real systems. BUT on very |
| 1168 | * long-running systems where a priq never completely empties |
| 1169 | * AND that contains very large numbers of threads, it can be |
| 1170 | * a latency glitch to loop over all the threads like this. |
| 1171 | */ |
| 1172 | if (!pq->next_order_key) { |
| 1173 | RB_FOR_EACH_CONTAINER(&pq->tree, t, base.qnode_rb) { |
| 1174 | t->base.order_key = pq->next_order_key++; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | rb_insert(&pq->tree, &thread->base.qnode_rb); |
| 1179 | } |
| 1180 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1181 | void z_priq_rb_remove(struct _priq_rb *pq, struct k_thread *thread) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1182 | { |
Andrew Boie | 8f0bb6a | 2019-09-21 18:36:23 -0700 | [diff] [blame] | 1183 | __ASSERT_NO_MSG(!z_is_idle_thread_object(thread)); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1184 | |
| 1185 | rb_remove(&pq->tree, &thread->base.qnode_rb); |
| 1186 | |
| 1187 | if (!pq->tree.root) { |
| 1188 | pq->next_order_key = 0; |
| 1189 | } |
| 1190 | } |
| 1191 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1192 | struct k_thread *z_priq_rb_best(struct _priq_rb *pq) |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1193 | { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1194 | struct k_thread *thread = NULL; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1195 | struct rbnode *n = rb_get_min(&pq->tree); |
| 1196 | |
Peter A. Bigot | 692e103 | 2019-01-03 23:36:28 -0600 | [diff] [blame] | 1197 | if (n != NULL) { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1198 | thread = CONTAINER_OF(n, struct k_thread, base.qnode_rb); |
Peter A. Bigot | 692e103 | 2019-01-03 23:36:28 -0600 | [diff] [blame] | 1199 | } |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1200 | return thread; |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1203 | #ifdef CONFIG_SCHED_MULTIQ |
| 1204 | # if (K_LOWEST_THREAD_PRIO - K_HIGHEST_THREAD_PRIO) > 31 |
| 1205 | # error Too many priorities for multiqueue scheduler (max 32) |
| 1206 | # endif |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1207 | |
Peter Mitsis | f8b76f3 | 2021-11-29 09:52:11 -0500 | [diff] [blame] | 1208 | static ALWAYS_INLINE void z_priq_mq_add(struct _priq_mq *pq, |
| 1209 | struct k_thread *thread) |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1210 | { |
| 1211 | int priority_bit = thread->base.prio - K_HIGHEST_THREAD_PRIO; |
| 1212 | |
| 1213 | sys_dlist_append(&pq->queues[priority_bit], &thread->base.qnode_dlist); |
Flavio Ceolin | a996203 | 2019-02-26 10:14:04 -0800 | [diff] [blame] | 1214 | pq->bitmask |= BIT(priority_bit); |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Peter Mitsis | f8b76f3 | 2021-11-29 09:52:11 -0500 | [diff] [blame] | 1217 | static ALWAYS_INLINE void z_priq_mq_remove(struct _priq_mq *pq, |
| 1218 | struct k_thread *thread) |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1219 | { |
| 1220 | int priority_bit = thread->base.prio - K_HIGHEST_THREAD_PRIO; |
| 1221 | |
| 1222 | sys_dlist_remove(&thread->base.qnode_dlist); |
| 1223 | if (sys_dlist_is_empty(&pq->queues[priority_bit])) { |
Flavio Ceolin | a996203 | 2019-02-26 10:14:04 -0800 | [diff] [blame] | 1224 | pq->bitmask &= ~BIT(priority_bit); |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1225 | } |
| 1226 | } |
Jeremy Bettis | fb1c36f | 2021-12-20 16:24:30 -0700 | [diff] [blame] | 1227 | #endif |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1228 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1229 | struct k_thread *z_priq_mq_best(struct _priq_mq *pq) |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1230 | { |
| 1231 | if (!pq->bitmask) { |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1235 | struct k_thread *thread = NULL; |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1236 | sys_dlist_t *l = &pq->queues[__builtin_ctz(pq->bitmask)]; |
Flavio Ceolin | 26be335 | 2018-11-15 15:03:32 -0800 | [diff] [blame] | 1237 | sys_dnode_t *n = sys_dlist_peek_head(l); |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1238 | |
Peter A. Bigot | 692e103 | 2019-01-03 23:36:28 -0600 | [diff] [blame] | 1239 | if (n != NULL) { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1240 | thread = CONTAINER_OF(n, struct k_thread, base.qnode_dlist); |
Peter A. Bigot | 692e103 | 2019-01-03 23:36:28 -0600 | [diff] [blame] | 1241 | } |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1242 | return thread; |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1245 | int z_unpend_all(_wait_q_t *wait_q) |
Andy Ross | 4ca0e07 | 2018-05-10 09:45:42 -0700 | [diff] [blame] | 1246 | { |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame] | 1247 | int need_sched = 0; |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1248 | struct k_thread *thread; |
Andy Ross | 4ca0e07 | 2018-05-10 09:45:42 -0700 | [diff] [blame] | 1249 | |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1250 | while ((thread = z_waitq_head(wait_q)) != NULL) { |
| 1251 | z_unpend_thread(thread); |
| 1252 | z_ready_thread(thread); |
Andy Ross | 4ca0e07 | 2018-05-10 09:45:42 -0700 | [diff] [blame] | 1253 | need_sched = 1; |
| 1254 | } |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame] | 1255 | |
| 1256 | return need_sched; |
Andy Ross | 4ca0e07 | 2018-05-10 09:45:42 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Andy Ross | b155d06 | 2021-09-24 13:49:14 -0700 | [diff] [blame] | 1259 | void init_ready_q(struct _ready_q *rq) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1260 | { |
Andy Ross | b155d06 | 2021-09-24 13:49:14 -0700 | [diff] [blame] | 1261 | #if defined(CONFIG_SCHED_SCALABLE) |
| 1262 | rq->runq = (struct _priq_rb) { |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1263 | .tree = { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1264 | .lessthan_fn = z_priq_rb_lessthan, |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1265 | } |
| 1266 | }; |
Andy Ross | b155d06 | 2021-09-24 13:49:14 -0700 | [diff] [blame] | 1267 | #elif defined(CONFIG_SCHED_MULTIQ) |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1268 | for (int i = 0; i < ARRAY_SIZE(_kernel.ready_q.runq.queues); i++) { |
Andy Ross | b155d06 | 2021-09-24 13:49:14 -0700 | [diff] [blame] | 1269 | sys_dlist_init(&rq->runq.queues[i]); |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1270 | } |
Andy Ross | b155d06 | 2021-09-24 13:49:14 -0700 | [diff] [blame] | 1271 | #else |
| 1272 | sys_dlist_init(&rq->runq); |
Andy Ross | 9f06a35 | 2018-06-28 10:38:14 -0700 | [diff] [blame] | 1273 | #endif |
Andy Ross | b155d06 | 2021-09-24 13:49:14 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | void z_sched_init(void) |
| 1277 | { |
Andy Ross | b11e796 | 2021-09-24 10:57:39 -0700 | [diff] [blame] | 1278 | #ifdef CONFIG_SCHED_CPU_MASK_PIN_ONLY |
| 1279 | for (int i = 0; i < CONFIG_MP_NUM_CPUS; i++) { |
| 1280 | init_ready_q(&_kernel.cpus[i].ready_q); |
| 1281 | } |
| 1282 | #else |
Andy Ross | b155d06 | 2021-09-24 13:49:14 -0700 | [diff] [blame] | 1283 | init_ready_q(&_kernel.ready_q); |
Andy Ross | b11e796 | 2021-09-24 10:57:39 -0700 | [diff] [blame] | 1284 | #endif |
Piotr Zięcik | 4a39b9e | 2018-07-26 14:56:39 +0200 | [diff] [blame] | 1285 | |
| 1286 | #ifdef CONFIG_TIMESLICING |
| 1287 | k_sched_time_slice_set(CONFIG_TIMESLICE_SIZE, |
| 1288 | CONFIG_TIMESLICE_PRIORITY); |
| 1289 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1290 | } |
| 1291 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1292 | int z_impl_k_thread_priority_get(k_tid_t thread) |
Allan Stephens | 399d0ad | 2016-10-07 13:41:34 -0500 | [diff] [blame] | 1293 | { |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 1294 | return thread->base.prio; |
Allan Stephens | 399d0ad | 2016-10-07 13:41:34 -0500 | [diff] [blame] | 1295 | } |
| 1296 | |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1297 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1298 | static inline int z_vrfy_k_thread_priority_get(k_tid_t thread) |
| 1299 | { |
| 1300 | Z_OOPS(Z_SYSCALL_OBJ(thread, K_OBJ_THREAD)); |
| 1301 | return z_impl_k_thread_priority_get(thread); |
| 1302 | } |
| 1303 | #include <syscalls/k_thread_priority_get_mrsh.c> |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1304 | #endif |
| 1305 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 1306 | void z_impl_k_thread_priority_set(k_tid_t thread, int prio) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1307 | { |
Benjamin Walsh | 3cc2ba9 | 2016-11-08 15:44:05 -0500 | [diff] [blame] | 1308 | /* |
| 1309 | * Use NULL, since we cannot know what the entry point is (we do not |
| 1310 | * keep track of it) and idle cannot change its priority. |
| 1311 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1312 | Z_ASSERT_VALID_PRIO(prio, NULL); |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 1313 | __ASSERT(!arch_is_in_isr(), ""); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1314 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 1315 | struct k_thread *th = (struct k_thread *)thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1316 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 1317 | z_thread_priority_set(th, prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1318 | } |
| 1319 | |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1320 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1321 | static inline void z_vrfy_k_thread_priority_set(k_tid_t thread, int prio) |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1322 | { |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 1323 | Z_OOPS(Z_SYSCALL_OBJ(thread, K_OBJ_THREAD)); |
| 1324 | Z_OOPS(Z_SYSCALL_VERIFY_MSG(_is_valid_prio(prio, NULL), |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1325 | "invalid thread priority %d", prio)); |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 1326 | Z_OOPS(Z_SYSCALL_VERIFY_MSG((int8_t)prio >= thread->base.prio, |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 1327 | "thread priority may only be downgraded (%d < %d)", |
| 1328 | prio, thread->base.prio)); |
Andrew Boie | 5008fed | 2017-10-08 10:11:24 -0700 | [diff] [blame] | 1329 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1330 | z_impl_k_thread_priority_set(thread, prio); |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1331 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1332 | #include <syscalls/k_thread_priority_set_mrsh.c> |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1333 | #endif |
| 1334 | |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1335 | #ifdef CONFIG_SCHED_DEADLINE |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1336 | void z_impl_k_thread_deadline_set(k_tid_t tid, int deadline) |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1337 | { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1338 | struct k_thread *thread = tid; |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1339 | |
Patrik Flykt | cf2d579 | 2019-02-12 15:50:46 -0700 | [diff] [blame] | 1340 | LOCKED(&sched_spinlock) { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1341 | thread->base.prio_deadline = k_cycle_get_32() + deadline; |
| 1342 | if (z_is_thread_queued(thread)) { |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 1343 | dequeue_thread(thread); |
| 1344 | queue_thread(thread); |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | #ifdef CONFIG_USERSPACE |
Andy Ross | 075c94f | 2019-08-13 11:34:34 -0700 | [diff] [blame] | 1350 | static inline void z_vrfy_k_thread_deadline_set(k_tid_t tid, int deadline) |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1351 | { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1352 | struct k_thread *thread = tid; |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1353 | |
| 1354 | Z_OOPS(Z_SYSCALL_OBJ(thread, K_OBJ_THREAD)); |
| 1355 | Z_OOPS(Z_SYSCALL_VERIFY_MSG(deadline > 0, |
| 1356 | "invalid thread deadline %d", |
| 1357 | (int)deadline)); |
| 1358 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1359 | z_impl_k_thread_deadline_set((k_tid_t)thread, deadline); |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1360 | } |
Andy Ross | 075c94f | 2019-08-13 11:34:34 -0700 | [diff] [blame] | 1361 | #include <syscalls/k_thread_deadline_set_mrsh.c> |
Andy Ross | 4a2e50f | 2018-05-15 11:06:25 -0700 | [diff] [blame] | 1362 | #endif |
| 1363 | #endif |
| 1364 | |
Jordan Yates | 1ef647f | 2022-03-26 09:55:23 +1000 | [diff] [blame] | 1365 | bool k_can_yield(void) |
| 1366 | { |
| 1367 | return !(k_is_pre_kernel() || k_is_in_isr() || |
| 1368 | z_is_idle_thread_object(_current)); |
| 1369 | } |
| 1370 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1371 | void z_impl_k_yield(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1372 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 1373 | __ASSERT(!arch_is_in_isr(), ""); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1374 | |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1375 | SYS_PORT_TRACING_FUNC(k_thread, yield); |
| 1376 | |
Andy Ross | 851d14a | 2021-05-13 15:46:43 -0700 | [diff] [blame] | 1377 | k_spinlock_key_t key = k_spin_lock(&sched_spinlock); |
James Harris | 6543e06 | 2021-03-01 10:14:13 -0800 | [diff] [blame] | 1378 | |
Andy Ross | 851d14a | 2021-05-13 15:46:43 -0700 | [diff] [blame] | 1379 | if (!IS_ENABLED(CONFIG_SMP) || |
| 1380 | z_is_thread_queued(_current)) { |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 1381 | dequeue_thread(_current); |
Andy Ross | 1acd8c2 | 2018-05-03 14:51:49 -0700 | [diff] [blame] | 1382 | } |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 1383 | queue_thread(_current); |
Andy Ross | 851d14a | 2021-05-13 15:46:43 -0700 | [diff] [blame] | 1384 | update_cache(1); |
| 1385 | z_swap(&sched_spinlock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1386 | } |
| 1387 | |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1388 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1389 | static inline void z_vrfy_k_yield(void) |
| 1390 | { |
| 1391 | z_impl_k_yield(); |
| 1392 | } |
| 1393 | #include <syscalls/k_yield_mrsh.c> |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1394 | #endif |
| 1395 | |
Flavio Ceolin | 7a815d5 | 2020-10-19 21:37:22 -0700 | [diff] [blame] | 1396 | static int32_t z_tick_sleep(k_ticks_t ticks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1397 | { |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 1398 | #ifdef CONFIG_MULTITHREADING |
Flavio Ceolin | 9a16097 | 2020-11-16 10:40:46 -0800 | [diff] [blame] | 1399 | uint32_t expected_wakeup_ticks; |
Carles Cufi | 9849df8 | 2016-12-02 15:31:08 +0100 | [diff] [blame] | 1400 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 1401 | __ASSERT(!arch_is_in_isr(), ""); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1402 | |
Flavio Ceolin | 7a815d5 | 2020-10-19 21:37:22 -0700 | [diff] [blame] | 1403 | #ifndef CONFIG_TIMEOUT_64BIT |
| 1404 | /* LOG subsys does not handle 64-bit values |
| 1405 | * https://github.com/zephyrproject-rtos/zephyr/issues/26246 |
| 1406 | */ |
| 1407 | LOG_DBG("thread %p for %u ticks", _current, ticks); |
| 1408 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1409 | |
Benjamin Walsh | 5596f78 | 2016-12-09 19:57:17 -0500 | [diff] [blame] | 1410 | /* wait of 0 ms is treated as a 'yield' */ |
Charles E. Youse | b186303 | 2019-05-08 13:22:46 -0700 | [diff] [blame] | 1411 | if (ticks == 0) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1412 | k_yield(); |
Piotr Zięcik | 7700eb2 | 2018-10-25 17:45:08 +0200 | [diff] [blame] | 1413 | return 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1414 | } |
| 1415 | |
Andy Ross | e956639 | 2020-12-18 11:12:39 -0800 | [diff] [blame] | 1416 | k_timeout_t timeout = Z_TIMEOUT_TICKS(ticks); |
Lauren Murphy | 4c85b46 | 2021-05-25 17:49:28 -0500 | [diff] [blame] | 1417 | if (Z_TICK_ABS(ticks) <= 0) { |
| 1418 | expected_wakeup_ticks = ticks + sys_clock_tick_get_32(); |
| 1419 | } else { |
| 1420 | expected_wakeup_ticks = Z_TICK_ABS(ticks); |
| 1421 | } |
Andy Ross | d27d4e6 | 2019-02-05 15:36:01 -0800 | [diff] [blame] | 1422 | |
Andrew Boie | a8775ab | 2020-09-05 12:53:42 -0700 | [diff] [blame] | 1423 | k_spinlock_key_t key = k_spin_lock(&sched_spinlock); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1424 | |
Andy Ross | dff6b71 | 2019-02-25 21:17:29 -0800 | [diff] [blame] | 1425 | #if defined(CONFIG_TIMESLICING) && defined(CONFIG_SWAP_NONATOMIC) |
| 1426 | pending_current = _current; |
| 1427 | #endif |
Andrew Boie | a8775ab | 2020-09-05 12:53:42 -0700 | [diff] [blame] | 1428 | unready_thread(_current); |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1429 | z_add_thread_timeout(_current, timeout); |
Andy Ross | 4521e0c | 2019-03-22 10:30:19 -0700 | [diff] [blame] | 1430 | z_mark_thread_as_suspended(_current); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1431 | |
Andrew Boie | a8775ab | 2020-09-05 12:53:42 -0700 | [diff] [blame] | 1432 | (void)z_swap(&sched_spinlock, key); |
Piotr Zięcik | 7700eb2 | 2018-10-25 17:45:08 +0200 | [diff] [blame] | 1433 | |
Andy Ross | 4521e0c | 2019-03-22 10:30:19 -0700 | [diff] [blame] | 1434 | __ASSERT(!z_is_thread_state_set(_current, _THREAD_SUSPENDED), ""); |
| 1435 | |
Anas Nashif | 5c90ceb | 2021-03-13 08:19:53 -0500 | [diff] [blame] | 1436 | ticks = (k_ticks_t)expected_wakeup_ticks - sys_clock_tick_get_32(); |
Piotr Zięcik | 7700eb2 | 2018-10-25 17:45:08 +0200 | [diff] [blame] | 1437 | if (ticks > 0) { |
Charles E. Youse | b186303 | 2019-05-08 13:22:46 -0700 | [diff] [blame] | 1438 | return ticks; |
Piotr Zięcik | 7700eb2 | 2018-10-25 17:45:08 +0200 | [diff] [blame] | 1439 | } |
Benjamin Walsh | b12a8e0 | 2016-12-14 15:24:12 -0500 | [diff] [blame] | 1440 | #endif |
Piotr Zięcik | 7700eb2 | 2018-10-25 17:45:08 +0200 | [diff] [blame] | 1441 | |
| 1442 | return 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1443 | } |
| 1444 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 1445 | int32_t z_impl_k_sleep(k_timeout_t timeout) |
Charles E. Youse | b186303 | 2019-05-08 13:22:46 -0700 | [diff] [blame] | 1446 | { |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1447 | k_ticks_t ticks; |
Charles E. Youse | b186303 | 2019-05-08 13:22:46 -0700 | [diff] [blame] | 1448 | |
Peter Bigot | 8162e58 | 2019-12-12 16:07:07 -0600 | [diff] [blame] | 1449 | __ASSERT(!arch_is_in_isr(), ""); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1450 | |
| 1451 | SYS_PORT_TRACING_FUNC_ENTER(k_thread, sleep, timeout); |
Peter Bigot | 8162e58 | 2019-12-12 16:07:07 -0600 | [diff] [blame] | 1452 | |
Anas Nashif | d2c7179 | 2020-10-17 07:52:17 -0400 | [diff] [blame] | 1453 | /* in case of K_FOREVER, we suspend */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1454 | if (K_TIMEOUT_EQ(timeout, K_FOREVER)) { |
Andrew Boie | d2b8922 | 2019-11-08 10:44:22 -0800 | [diff] [blame] | 1455 | k_thread_suspend(_current); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1456 | |
| 1457 | SYS_PORT_TRACING_FUNC_EXIT(k_thread, sleep, timeout, (int32_t) K_TICKS_FOREVER); |
| 1458 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 1459 | return (int32_t) K_TICKS_FOREVER; |
Andrew Boie | d2b8922 | 2019-11-08 10:44:22 -0800 | [diff] [blame] | 1460 | } |
| 1461 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1462 | ticks = timeout.ticks; |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1463 | |
Charles E. Youse | b186303 | 2019-05-08 13:22:46 -0700 | [diff] [blame] | 1464 | ticks = z_tick_sleep(ticks); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1465 | |
| 1466 | int32_t ret = k_ticks_to_ms_floor64(ticks); |
| 1467 | |
| 1468 | SYS_PORT_TRACING_FUNC_EXIT(k_thread, sleep, timeout, ret); |
| 1469 | |
| 1470 | return ret; |
Charles E. Youse | b186303 | 2019-05-08 13:22:46 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1473 | #ifdef CONFIG_USERSPACE |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 1474 | static inline int32_t z_vrfy_k_sleep(k_timeout_t timeout) |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1475 | { |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1476 | return z_impl_k_sleep(timeout); |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 1477 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1478 | #include <syscalls/k_sleep_mrsh.c> |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 1479 | #endif |
| 1480 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 1481 | int32_t z_impl_k_usleep(int us) |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 1482 | { |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 1483 | int32_t ticks; |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 1484 | |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1485 | SYS_PORT_TRACING_FUNC_ENTER(k_thread, usleep, us); |
| 1486 | |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 1487 | ticks = k_us_to_ticks_ceil64(us); |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 1488 | ticks = z_tick_sleep(ticks); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1489 | |
| 1490 | SYS_PORT_TRACING_FUNC_EXIT(k_thread, usleep, us, k_ticks_to_us_floor64(ticks)); |
| 1491 | |
Andy Ross | 8892406 | 2019-10-03 11:43:10 -0700 | [diff] [blame] | 1492 | return k_ticks_to_us_floor64(ticks); |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | #ifdef CONFIG_USERSPACE |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 1496 | static inline int32_t z_vrfy_k_usleep(int us) |
Charles E. Youse | a567831 | 2019-05-09 16:46:46 -0700 | [diff] [blame] | 1497 | { |
| 1498 | return z_impl_k_usleep(us); |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1499 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1500 | #include <syscalls/k_usleep_mrsh.c> |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1501 | #endif |
| 1502 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1503 | void z_impl_k_wakeup(k_tid_t thread) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1504 | { |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1505 | SYS_PORT_TRACING_OBJ_FUNC(k_thread, wakeup, thread); |
| 1506 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1507 | if (z_is_thread_pending(thread)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1508 | return; |
| 1509 | } |
| 1510 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1511 | if (z_abort_thread_timeout(thread) < 0) { |
Andrew Boie | d2b8922 | 2019-11-08 10:44:22 -0800 | [diff] [blame] | 1512 | /* Might have just been sleeping forever */ |
| 1513 | if (thread->base.thread_state != _THREAD_SUSPENDED) { |
| 1514 | return; |
| 1515 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1516 | } |
| 1517 | |
Andy Ross | 4521e0c | 2019-03-22 10:30:19 -0700 | [diff] [blame] | 1518 | z_mark_thread_as_not_suspended(thread); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1519 | z_ready_thread(thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1520 | |
Andy Ross | 3267cd3 | 2022-04-06 09:58:20 -0700 | [diff] [blame] | 1521 | flag_ipi(); |
Andy Ross | 5737b5c | 2020-02-04 13:52:09 -0800 | [diff] [blame] | 1522 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 1523 | if (!arch_is_in_isr()) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1524 | z_reschedule_unlocked(); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | |
Enjia Mai | 7ac40aa | 2020-05-28 11:29:50 +0800 | [diff] [blame] | 1528 | #ifdef CONFIG_TRACE_SCHED_IPI |
| 1529 | extern void z_trace_sched_ipi(void); |
| 1530 | #endif |
| 1531 | |
Andy Ross | 42ed12a | 2019-02-19 16:03:39 -0800 | [diff] [blame] | 1532 | #ifdef CONFIG_SMP |
Andy Ross | 42ed12a | 2019-02-19 16:03:39 -0800 | [diff] [blame] | 1533 | void z_sched_ipi(void) |
| 1534 | { |
Daniel Leung | adac4cb | 2020-01-09 18:55:07 -0800 | [diff] [blame] | 1535 | /* NOTE: When adding code to this, make sure this is called |
| 1536 | * at appropriate location when !CONFIG_SCHED_IPI_SUPPORTED. |
| 1537 | */ |
Enjia Mai | 7ac40aa | 2020-05-28 11:29:50 +0800 | [diff] [blame] | 1538 | #ifdef CONFIG_TRACE_SCHED_IPI |
| 1539 | z_trace_sched_ipi(); |
| 1540 | #endif |
Andy Ross | 42ed12a | 2019-02-19 16:03:39 -0800 | [diff] [blame] | 1541 | } |
Andy Ross | 42ed12a | 2019-02-19 16:03:39 -0800 | [diff] [blame] | 1542 | #endif |
| 1543 | |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1544 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1545 | static inline void z_vrfy_k_wakeup(k_tid_t thread) |
| 1546 | { |
| 1547 | Z_OOPS(Z_SYSCALL_OBJ(thread, K_OBJ_THREAD)); |
| 1548 | z_impl_k_wakeup(thread); |
| 1549 | } |
| 1550 | #include <syscalls/k_wakeup_mrsh.c> |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1551 | #endif |
| 1552 | |
Andrew Boie | f07df42 | 2020-11-06 13:11:12 -0800 | [diff] [blame] | 1553 | k_tid_t z_impl_z_current_get(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1554 | { |
Andy Ross | eefd3da | 2020-02-06 13:39:52 -0800 | [diff] [blame] | 1555 | #ifdef CONFIG_SMP |
| 1556 | /* In SMP, _current is a field read from _current_cpu, which |
| 1557 | * can race with preemption before it is read. We must lock |
| 1558 | * local interrupts when reading it. |
| 1559 | */ |
| 1560 | unsigned int k = arch_irq_lock(); |
| 1561 | #endif |
| 1562 | |
| 1563 | k_tid_t ret = _current_cpu->current; |
| 1564 | |
| 1565 | #ifdef CONFIG_SMP |
| 1566 | arch_irq_unlock(k); |
| 1567 | #endif |
| 1568 | return ret; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1569 | } |
| 1570 | |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1571 | #ifdef CONFIG_USERSPACE |
Andrew Boie | f07df42 | 2020-11-06 13:11:12 -0800 | [diff] [blame] | 1572 | static inline k_tid_t z_vrfy_z_current_get(void) |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1573 | { |
Andrew Boie | f07df42 | 2020-11-06 13:11:12 -0800 | [diff] [blame] | 1574 | return z_impl_z_current_get(); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1575 | } |
Andrew Boie | f07df42 | 2020-11-06 13:11:12 -0800 | [diff] [blame] | 1576 | #include <syscalls/z_current_get_mrsh.c> |
Andrew Boie | 76c04a2 | 2017-09-27 14:45:10 -0700 | [diff] [blame] | 1577 | #endif |
| 1578 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 1579 | int z_impl_k_is_preempt_thread(void) |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1580 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 1581 | return !arch_is_in_isr() && is_preempt(_current); |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 1582 | } |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1583 | |
| 1584 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 1585 | static inline int z_vrfy_k_is_preempt_thread(void) |
| 1586 | { |
| 1587 | return z_impl_k_is_preempt_thread(); |
| 1588 | } |
| 1589 | #include <syscalls/k_is_preempt_thread_mrsh.c> |
Andrew Boie | 468190a | 2017-09-29 14:00:48 -0700 | [diff] [blame] | 1590 | #endif |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1591 | |
| 1592 | #ifdef CONFIG_SCHED_CPU_MASK |
| 1593 | # ifdef CONFIG_SMP |
| 1594 | /* Right now we use a single byte for this mask */ |
Oleg Zhurakivskyy | b1e1f64 | 2020-03-12 17:16:00 +0200 | [diff] [blame] | 1595 | BUILD_ASSERT(CONFIG_MP_NUM_CPUS <= 8, "Too many CPUs for mask word"); |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1596 | # endif |
| 1597 | |
| 1598 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 1599 | static int cpu_mask_mod(k_tid_t thread, uint32_t enable_mask, uint32_t disable_mask) |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1600 | { |
| 1601 | int ret = 0; |
| 1602 | |
Flavio Ceolin | 551038e | 2022-05-02 14:31:04 -0700 | [diff] [blame] | 1603 | #ifdef CONFIG_SCHED_CPU_MASK_PIN_ONLY |
| 1604 | __ASSERT((thread->base.thread_state != _THREAD_PRESTART), |
| 1605 | "Only PRESTARTED threads can change CPU pin"); |
| 1606 | #endif |
| 1607 | |
Patrik Flykt | cf2d579 | 2019-02-12 15:50:46 -0700 | [diff] [blame] | 1608 | LOCKED(&sched_spinlock) { |
Anas Nashif | 9e3e7f6 | 2019-12-19 08:19:45 -0500 | [diff] [blame] | 1609 | if (z_is_thread_prevented_from_running(thread)) { |
| 1610 | thread->base.cpu_mask |= enable_mask; |
| 1611 | thread->base.cpu_mask &= ~disable_mask; |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1612 | } else { |
| 1613 | ret = -EINVAL; |
| 1614 | } |
| 1615 | } |
Andy Ross | b11e796 | 2021-09-24 10:57:39 -0700 | [diff] [blame] | 1616 | |
| 1617 | #if defined(CONFIG_ASSERT) && defined(CONFIG_SCHED_CPU_MASK_PIN_ONLY) |
| 1618 | int m = thread->base.cpu_mask; |
| 1619 | |
| 1620 | __ASSERT((m == 0) || ((m & (m - 1)) == 0), |
| 1621 | "Only one CPU allowed in mask when PIN_ONLY"); |
| 1622 | #endif |
| 1623 | |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1624 | return ret; |
| 1625 | } |
| 1626 | |
| 1627 | int k_thread_cpu_mask_clear(k_tid_t thread) |
| 1628 | { |
| 1629 | return cpu_mask_mod(thread, 0, 0xffffffff); |
| 1630 | } |
| 1631 | |
| 1632 | int k_thread_cpu_mask_enable_all(k_tid_t thread) |
| 1633 | { |
| 1634 | return cpu_mask_mod(thread, 0xffffffff, 0); |
| 1635 | } |
| 1636 | |
| 1637 | int k_thread_cpu_mask_enable(k_tid_t thread, int cpu) |
| 1638 | { |
| 1639 | return cpu_mask_mod(thread, BIT(cpu), 0); |
| 1640 | } |
| 1641 | |
| 1642 | int k_thread_cpu_mask_disable(k_tid_t thread, int cpu) |
| 1643 | { |
| 1644 | return cpu_mask_mod(thread, 0, BIT(cpu)); |
| 1645 | } |
| 1646 | |
Anas Nashif | c9d0248 | 2022-04-15 08:27:15 -0400 | [diff] [blame] | 1647 | int k_thread_cpu_pin(k_tid_t thread, int cpu) |
| 1648 | { |
| 1649 | int ret; |
| 1650 | |
| 1651 | ret = k_thread_cpu_mask_clear(thread); |
| 1652 | if (ret == 0) { |
| 1653 | return k_thread_cpu_mask_enable(thread, cpu); |
| 1654 | } |
| 1655 | return ret; |
| 1656 | } |
| 1657 | |
Andy Ross | ab46b1b | 2019-01-30 15:00:42 -0800 | [diff] [blame] | 1658 | #endif /* CONFIG_SCHED_CPU_MASK */ |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 1659 | |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1660 | static inline void unpend_all(_wait_q_t *wait_q) |
| 1661 | { |
| 1662 | struct k_thread *thread; |
| 1663 | |
| 1664 | while ((thread = z_waitq_head(wait_q)) != NULL) { |
| 1665 | unpend_thread_no_timeout(thread); |
| 1666 | (void)z_abort_thread_timeout(thread); |
| 1667 | arch_thread_return_value_set(thread, 0); |
| 1668 | ready_thread(thread); |
| 1669 | } |
| 1670 | } |
| 1671 | |
Chen Peng1 | 0f63d11 | 2021-09-06 13:59:40 +0800 | [diff] [blame] | 1672 | #ifdef CONFIG_CMSIS_RTOS_V1 |
| 1673 | extern void z_thread_cmsis_status_mask_clear(struct k_thread *thread); |
| 1674 | #endif |
| 1675 | |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1676 | static void end_thread(struct k_thread *thread) |
| 1677 | { |
| 1678 | /* We hold the lock, and the thread is known not to be running |
| 1679 | * anywhere. |
| 1680 | */ |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 1681 | if ((thread->base.thread_state & _THREAD_DEAD) == 0U) { |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1682 | thread->base.thread_state |= _THREAD_DEAD; |
| 1683 | thread->base.thread_state &= ~_THREAD_ABORTING; |
| 1684 | if (z_is_thread_queued(thread)) { |
Andy Ross | c230fb3 | 2021-09-23 16:41:30 -0700 | [diff] [blame] | 1685 | dequeue_thread(thread); |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1686 | } |
| 1687 | if (thread->base.pended_on != NULL) { |
| 1688 | unpend_thread_no_timeout(thread); |
| 1689 | } |
| 1690 | (void)z_abort_thread_timeout(thread); |
| 1691 | unpend_all(&thread->join_queue); |
| 1692 | update_cache(1); |
| 1693 | |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1694 | SYS_PORT_TRACING_FUNC(k_thread, sched_abort, thread); |
| 1695 | |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1696 | z_thread_monitor_exit(thread); |
| 1697 | |
Chen Peng1 | 0f63d11 | 2021-09-06 13:59:40 +0800 | [diff] [blame] | 1698 | #ifdef CONFIG_CMSIS_RTOS_V1 |
| 1699 | z_thread_cmsis_status_mask_clear(thread); |
| 1700 | #endif |
| 1701 | |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1702 | #ifdef CONFIG_USERSPACE |
| 1703 | z_mem_domain_exit_thread(thread); |
| 1704 | z_thread_perms_all_clear(thread); |
| 1705 | z_object_uninit(thread->stack_obj); |
| 1706 | z_object_uninit(thread); |
| 1707 | #endif |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | void z_thread_abort(struct k_thread *thread) |
| 1712 | { |
| 1713 | k_spinlock_key_t key = k_spin_lock(&sched_spinlock); |
| 1714 | |
Andy Ross | fb61359 | 2022-05-19 12:55:28 -0700 | [diff] [blame] | 1715 | if ((thread->base.user_options & K_ESSENTIAL) != 0) { |
| 1716 | k_spin_unlock(&sched_spinlock, key); |
| 1717 | __ASSERT(false, "aborting essential thread %p", thread); |
| 1718 | k_panic(); |
| 1719 | return; |
| 1720 | } |
| 1721 | |
Anas Nashif | 3f4f3f6 | 2021-03-29 17:13:47 -0400 | [diff] [blame] | 1722 | if ((thread->base.thread_state & _THREAD_DEAD) != 0U) { |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1723 | k_spin_unlock(&sched_spinlock, key); |
| 1724 | return; |
| 1725 | } |
| 1726 | |
| 1727 | #ifdef CONFIG_SMP |
| 1728 | if (is_aborting(thread) && thread == _current && arch_is_in_isr()) { |
| 1729 | /* Another CPU is spinning for us, don't deadlock */ |
| 1730 | end_thread(thread); |
| 1731 | } |
| 1732 | |
| 1733 | bool active = thread_active_elsewhere(thread); |
| 1734 | |
| 1735 | if (active) { |
| 1736 | /* It's running somewhere else, flag and poke */ |
| 1737 | thread->base.thread_state |= _THREAD_ABORTING; |
Lauren Murphy | d88ce65 | 2021-03-09 16:41:43 -0600 | [diff] [blame] | 1738 | |
Andy Ross | b4e9ef0 | 2022-04-06 10:10:17 -0700 | [diff] [blame] | 1739 | /* We're going to spin, so need a true synchronous IPI |
| 1740 | * here, not deferred! |
| 1741 | */ |
Lauren Murphy | d88ce65 | 2021-03-09 16:41:43 -0600 | [diff] [blame] | 1742 | #ifdef CONFIG_SCHED_IPI_SUPPORTED |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1743 | arch_sched_ipi(); |
Lauren Murphy | d88ce65 | 2021-03-09 16:41:43 -0600 | [diff] [blame] | 1744 | #endif |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | if (is_aborting(thread) && thread != _current) { |
| 1748 | if (arch_is_in_isr()) { |
| 1749 | /* ISRs can only spin waiting another CPU */ |
| 1750 | k_spin_unlock(&sched_spinlock, key); |
| 1751 | while (is_aborting(thread)) { |
| 1752 | } |
| 1753 | } else if (active) { |
| 1754 | /* Threads can join */ |
| 1755 | add_to_waitq_locked(_current, &thread->join_queue); |
| 1756 | z_swap(&sched_spinlock, key); |
| 1757 | } |
| 1758 | return; /* lock has been released */ |
| 1759 | } |
| 1760 | #endif |
| 1761 | end_thread(thread); |
| 1762 | if (thread == _current && !arch_is_in_isr()) { |
| 1763 | z_swap(&sched_spinlock, key); |
| 1764 | __ASSERT(false, "aborted _current back from dead"); |
| 1765 | } |
| 1766 | k_spin_unlock(&sched_spinlock, key); |
| 1767 | } |
| 1768 | |
| 1769 | #if !defined(CONFIG_ARCH_HAS_THREAD_ABORT) |
| 1770 | void z_impl_k_thread_abort(struct k_thread *thread) |
| 1771 | { |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1772 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_thread, abort, thread); |
| 1773 | |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1774 | z_thread_abort(thread); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1775 | |
| 1776 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_thread, abort, thread); |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1777 | } |
| 1778 | #endif |
| 1779 | |
| 1780 | int z_impl_k_thread_join(struct k_thread *thread, k_timeout_t timeout) |
| 1781 | { |
| 1782 | k_spinlock_key_t key = k_spin_lock(&sched_spinlock); |
| 1783 | int ret = 0; |
| 1784 | |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1785 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_thread, join, thread, timeout); |
| 1786 | |
Anas Nashif | 3f4f3f6 | 2021-03-29 17:13:47 -0400 | [diff] [blame] | 1787 | if ((thread->base.thread_state & _THREAD_DEAD) != 0U) { |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1788 | ret = 0; |
| 1789 | } else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
| 1790 | ret = -EBUSY; |
Anas Nashif | 3f4f3f6 | 2021-03-29 17:13:47 -0400 | [diff] [blame] | 1791 | } else if ((thread == _current) || |
| 1792 | (thread->base.pended_on == &_current->join_queue)) { |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1793 | ret = -EDEADLK; |
| 1794 | } else { |
| 1795 | __ASSERT(!arch_is_in_isr(), "cannot join in ISR"); |
| 1796 | add_to_waitq_locked(_current, &thread->join_queue); |
| 1797 | add_thread_timeout(_current, timeout); |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1798 | |
| 1799 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_thread, join, thread, timeout); |
| 1800 | ret = z_swap(&sched_spinlock, key); |
| 1801 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_thread, join, thread, timeout, ret); |
| 1802 | |
| 1803 | return ret; |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1804 | } |
| 1805 | |
Torbjörn Leksell | f171443 | 2021-03-26 10:59:08 +0100 | [diff] [blame] | 1806 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_thread, join, thread, timeout, ret); |
| 1807 | |
Andy Ross | 6fb6d3c | 2021-02-19 15:32:19 -0800 | [diff] [blame] | 1808 | k_spin_unlock(&sched_spinlock, key); |
| 1809 | return ret; |
| 1810 | } |
| 1811 | |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 1812 | #ifdef CONFIG_USERSPACE |
| 1813 | /* Special case: don't oops if the thread is uninitialized. This is because |
| 1814 | * the initialization bit does double-duty for thread objects; if false, means |
| 1815 | * the thread object is truly uninitialized, or the thread ran and exited for |
| 1816 | * some reason. |
| 1817 | * |
| 1818 | * Return true in this case indicating we should just do nothing and return |
| 1819 | * success to the caller. |
| 1820 | */ |
| 1821 | static bool thread_obj_validate(struct k_thread *thread) |
| 1822 | { |
Andrew Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 1823 | struct z_object *ko = z_object_find(thread); |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 1824 | int ret = z_object_validate(ko, K_OBJ_THREAD, _OBJ_INIT_TRUE); |
| 1825 | |
| 1826 | switch (ret) { |
| 1827 | case 0: |
| 1828 | return false; |
| 1829 | case -EINVAL: |
| 1830 | return true; |
| 1831 | default: |
| 1832 | #ifdef CONFIG_LOG |
| 1833 | z_dump_object_error(ret, thread, ko, K_OBJ_THREAD); |
| 1834 | #endif |
| 1835 | Z_OOPS(Z_SYSCALL_VERIFY_MSG(ret, "access denied")); |
| 1836 | } |
Enjia Mai | 53ca709 | 2021-01-15 17:09:58 +0800 | [diff] [blame] | 1837 | CODE_UNREACHABLE; /* LCOV_EXCL_LINE */ |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 1838 | } |
| 1839 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 1840 | static inline int z_vrfy_k_thread_join(struct k_thread *thread, |
| 1841 | k_timeout_t timeout) |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 1842 | { |
| 1843 | if (thread_obj_validate(thread)) { |
| 1844 | return 0; |
| 1845 | } |
| 1846 | |
| 1847 | return z_impl_k_thread_join(thread, timeout); |
| 1848 | } |
| 1849 | #include <syscalls/k_thread_join_mrsh.c> |
Andrew Boie | a4c9190 | 2020-03-24 16:09:24 -0700 | [diff] [blame] | 1850 | |
| 1851 | static inline void z_vrfy_k_thread_abort(k_tid_t thread) |
| 1852 | { |
| 1853 | if (thread_obj_validate(thread)) { |
| 1854 | return; |
| 1855 | } |
| 1856 | |
| 1857 | Z_OOPS(Z_SYSCALL_VERIFY_MSG(!(thread->base.user_options & K_ESSENTIAL), |
| 1858 | "aborting essential thread %p", thread)); |
| 1859 | |
| 1860 | z_impl_k_thread_abort((struct k_thread *)thread); |
| 1861 | } |
| 1862 | #include <syscalls/k_thread_abort_mrsh.c> |
Andrew Boie | 322816e | 2020-02-20 16:33:06 -0800 | [diff] [blame] | 1863 | #endif /* CONFIG_USERSPACE */ |
Peter Bigot | 0259c86 | 2021-01-12 13:45:32 -0600 | [diff] [blame] | 1864 | |
| 1865 | /* |
| 1866 | * future scheduler.h API implementations |
| 1867 | */ |
| 1868 | bool z_sched_wake(_wait_q_t *wait_q, int swap_retval, void *swap_data) |
| 1869 | { |
| 1870 | struct k_thread *thread; |
| 1871 | bool ret = false; |
| 1872 | |
| 1873 | LOCKED(&sched_spinlock) { |
| 1874 | thread = _priq_wait_best(&wait_q->waitq); |
| 1875 | |
| 1876 | if (thread != NULL) { |
| 1877 | z_thread_return_value_set_with_data(thread, |
| 1878 | swap_retval, |
| 1879 | swap_data); |
| 1880 | unpend_thread_no_timeout(thread); |
| 1881 | (void)z_abort_thread_timeout(thread); |
| 1882 | ready_thread(thread); |
| 1883 | ret = true; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | return ret; |
| 1888 | } |
| 1889 | |
| 1890 | int z_sched_wait(struct k_spinlock *lock, k_spinlock_key_t key, |
| 1891 | _wait_q_t *wait_q, k_timeout_t timeout, void **data) |
| 1892 | { |
| 1893 | int ret = z_pend_curr(lock, key, wait_q, timeout); |
| 1894 | |
| 1895 | if (data != NULL) { |
| 1896 | *data = _current->base.swap_data; |
| 1897 | } |
| 1898 | return ret; |
| 1899 | } |