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