Branch data Line data Source code
1 : : /*
2 : : * Copyright The Zephyr Project Contributors
3 : : * SPDX-License-Identifier: Apache-2.0
4 : : */
5 : : #include <zephyr/kernel.h>
6 : : #include <kspinlock.h>
7 : : #include <ksched.h>
8 : : #include <zephyr/sys/__assert.h>
9 : : #include <zephyr/spinlock.h>
10 : : #include <zephyr/internal/syscall_handler.h>
11 : : #include <run_q.h>
12 : : #include <wait_q.h>
13 : : #include <scheduler.h>
14 : :
15 : :
16 : 1 : void z_sched_init(void)
17 : : {
18 : : #ifdef CONFIG_SCHED_CPU_MASK_PIN_ONLY
19 : : for (int i = 0; i < CONFIG_MP_MAX_NUM_CPUS; i++) {
20 : : _priq_run_init(&_kernel.cpus[i].ready_q.runq);
21 : : }
22 : : #else
23 : 1 : _priq_run_init(&_kernel.ready_q.runq);
24 : : #endif /* CONFIG_SCHED_CPU_MASK_PIN_ONLY */
25 : 1 : }
26 : :
27 : 1 : void k_sched_lock(void)
28 : : {
29 [ + + ]: 2 : Z_SCHED_SPINLOCK {
30 : 1 : SYS_PORT_TRACING_FUNC(k_thread, sched_lock);
31 : :
32 [ - + ]: 1 : __ASSERT(!arch_is_in_isr(), "");
33 [ - + ]: 1 : __ASSERT(_current->base.sched_locked != 1U, "");
34 : :
35 : 1 : --_current->base.sched_locked;
36 : :
37 : 1 : compiler_barrier();
38 : : }
39 : 1 : }
40 : :
41 : 1 : void k_sched_unlock(void)
42 : : {
43 : 1 : SYS_PORT_TRACING_FUNC(k_thread, sched_unlock);
44 : :
45 : 1 : k_spinlock_key_t key = z_sched_spinlock_lock();
46 : :
47 [ - + ]: 1 : __ASSERT(_current->base.sched_locked != 0U, "");
48 [ - + ]: 1 : __ASSERT(!arch_is_in_isr(), "");
49 : 1 : ++_current->base.sched_locked;
50 : 1 : z_sched_lock_reschedule(key);
51 : 1 : }
52 : :
53 : 0 : void z_impl_k_reschedule(void)
54 : : {
55 : 0 : k_spinlock_key_t key;
56 : :
57 : 0 : key = z_sched_spinlock_lock();
58 : :
59 : 0 : z_sched_lock_reschedule(key);
60 : 0 : }
61 : :
62 : : #ifdef CONFIG_USERSPACE
63 : : static inline void z_vrfy_k_reschedule(void)
64 : : {
65 : : z_impl_k_reschedule();
66 : : }
67 : : #include <zephyr/syscalls/k_reschedule_mrsh.c>
68 : : #endif /* CONFIG_USERSPACE */
69 : :
70 : 54 : k_tid_t z_impl_k_sched_current_thread_query(void)
71 : : {
72 : 54 : return _current;
73 : : }
74 : :
75 : : #ifdef CONFIG_USERSPACE
76 : : static inline k_tid_t z_vrfy_k_sched_current_thread_query(void)
77 : : {
78 : : return z_impl_k_sched_current_thread_query();
79 : : }
80 : : #include <zephyr/syscalls/k_sched_current_thread_query_mrsh.c>
81 : : #endif /* CONFIG_USERSPACE */
82 : :
83 : :
84 : 0 : int z_sched_waitq_walk(_wait_q_t *wait_q, _waitq_walk_cb_t walk_func,
85 : : _waitq_post_walk_cb_t post_func, void *data)
86 : : {
87 : 0 : struct k_thread *thread;
88 : 0 : int status = 0;
89 : :
90 [ # # ]: 0 : Z_SCHED_SPINLOCK {
91 : : #ifndef CONFIG_WAITQ_SCALABLE
92 : 0 : struct k_thread *tmp;
93 : :
94 [ # # # # : 0 : _WAIT_Q_FOR_EACH_SAFE(wait_q, thread, tmp)
# # ]
95 : : #else /* !CONFIG_WAITQ_SCALABLE */
96 : : _WAIT_Q_FOR_EACH(wait_q, thread)
97 : : #endif /* !CONFIG_WAITQ_SCALABLE */
98 : : {
99 : :
100 : : /*
101 : : * Invoke the callback function on each waiting thread
102 : : * for as long as there are both waiting threads AND
103 : : * it returns 0.
104 : : */
105 : :
106 : 0 : status = walk_func(thread, data);
107 [ # # ]: 0 : if (status != 0) {
108 : : break;
109 : : }
110 : : }
111 : :
112 : : /*
113 : : * Invoke post-walk callback. This is done while
114 : : * still holding the scheduler spinlock to enable atomic
115 : : * operations (from the scheduler's point of view).
116 : : */
117 [ # # ]: 0 : if (post_func != NULL) {
118 : 0 : post_func(status, data);
119 : : }
120 : : }
121 : :
122 : 0 : return status;
123 : : }
124 : :
125 : :
126 : : /*
127 : : * future scheduler.h API implementations
128 : : */
129 : :
130 : 0 : int z_sched_wait(struct k_spinlock *lock, k_spinlock_key_t key,
131 : : _wait_q_t *wait_q, k_timeout_t timeout, void **data)
132 : : {
133 : 0 : int ret = z_pend_curr(lock, key, wait_q, timeout);
134 : :
135 [ # # ]: 0 : if (data != NULL) {
136 : 0 : *data = _current->base.swap_data;
137 : : }
138 : 0 : return ret;
139 : : }
|