1#include "sch/tests/test_internal.h"
2
3TEST_GROUP_DEFINE(sched, .intensity_desc = {
4 .curve = SCALE_PIECEWISE_LOG,
5 .unit = "iters",
6 });
7
8static void sleepy_entry(void *arg) {
9 cc_ignore(arg);
10 thread_sleep_for_ms(ms: 50);
11}
12
13TEST_DEFINE_INTEGRATION(sched, sleep_ms) {
14 struct thread *t =
15 thread_spawn("sched_sleepy_test", sleepy_entry, .joinable = true);
16 TEST_ASSERT_NONNULL(t);
17 thread_join(t);
18 return TEST_SUCCESS;
19}
20
21static atomic_bool slept_for_us = false;
22
23static void micro_sleep_entry(void *arg) {
24 cc_ignore(arg);
25 thread_sleep_for_us(us: 200);
26 atomic_store(&slept_for_us, true);
27}
28
29TEST_DEFINE_INTEGRATION(sched, sleep_us) {
30 atomic_store(&slept_for_us, false);
31 struct thread *t = thread_spawn("sched_micro_sleep_test", micro_sleep_entry,
32 .joinable = true);
33 TEST_ASSERT_NONNULL(t);
34 thread_join(t);
35 TEST_ASSERT(atomic_load(&slept_for_us));
36 return TEST_SUCCESS;
37}
38
39static atomic_bool short_sleep_stop = false;
40static atomic_size_t short_sleep_count = 0;
41
42static void short_sleep_entry(void *arg) {
43 cc_ignore(arg);
44 for (size_t i = 0; i < 200 && !atomic_load(&short_sleep_stop); i++) {
45 thread_sleep_for_us(us: 1);
46 atomic_inc(&short_sleep_count);
47 }
48}
49
50TEST_DEFINE_INTEGRATION(sched, short_sleep_lost_wake) {
51 atomic_store(&short_sleep_stop, false);
52 atomic_store(&short_sleep_count, 0);
53
54 struct thread *t = thread_spawn("sched_short_sleep_lost_wake_test",
55 short_sleep_entry, .joinable = true);
56 TEST_ASSERT_NONNULL(t);
57
58 bool joined = thread_join_timeout(t, timeout_ms: 1000, NULL);
59 if (!joined) {
60 atomic_store(&short_sleep_stop, true);
61 /* alert must not rescue a uninterruptible wait */
62 enforce(joined, "short timed sleep lost its object completion");
63 thread_join(t);
64 }
65
66 if (!joined)
67 test_info("short timed sleep lost its wake after %zu cycles",
68 atomic_load(&short_sleep_count));
69 TEST_ASSERT(joined);
70 TEST_ASSERT_EQ(200, atomic_load(&short_sleep_count));
71 return TEST_SUCCESS;
72}
73
74static struct thread_wait_header si_wait;
75static atomic_bool si_apc_ran = false;
76static struct thread *si_t;
77static atomic_bool si_ok = false;
78static atomic_bool si_started = false;
79
80static void apc_si(struct apc_context *ac, void *apc) {
81 cc_ignore(ac);
82 cc_ignore(apc);
83 atomic_store(&si_apc_ran, true);
84}
85
86static void apc_enqueue_thread(void *arg) {
87 cc_ignore(arg);
88 struct apc *apc = apc_create();
89 apc_init(a: apc, fn: apc_si, NULL, destroy: apc_destroy_free);
90
91 while (!atomic_load(&si_started))
92 cpu_pause();
93
94 rcu_read_lock();
95 bool got = thread_getref_rcu(t: si_t);
96 rcu_read_unlock();
97
98 if (got) {
99 apc_enqueue(t: si_t, a: apc, type: APC_TYPE_KERNEL);
100 thread_putref(t: si_t);
101 }
102 apc_putref(a: apc);
103}
104
105static void sleeping_thread(void *arg) {
106 cc_ignore(arg);
107 thread_wait_prepare_to_sleep(header: &si_wait, object: &si_wait, type: THREAD_WAIT_INTERRUPTIBLE);
108 atomic_store(&si_started, true);
109 thread_wait_complete();
110
111 atomic_store(&si_ok, true);
112}
113
114static void waking_thread(void *arg) {
115 cc_ignore(arg);
116 while (!atomic_load(&si_apc_ran))
117 scheduler_yield();
118
119 thread_wait_header_satisfy(hdr: &si_wait, reason: THREAD_WAKE_REASON_SLEEP_MANUAL, NULL);
120}
121
122TEST_DEFINE_INTEGRATION(sched, sleep_interruptible_apc, .min_cores = 4) {
123 thread_wait_header_init(header: &si_wait);
124 atomic_store(&si_apc_ran, false);
125 atomic_store(&si_ok, false);
126 atomic_store(&si_started, false);
127
128 si_t = thread_spawn("si_thread", sleeping_thread, .on_cpu = 1,
129 .joinable = true);
130 struct thread *waker =
131 thread_spawn("si_wake", waking_thread, .on_cpu = 2, .joinable = true);
132 struct thread *enq = thread_spawn("si_apc_e", apc_enqueue_thread,
133 .on_cpu = 3, .joinable = true);
134
135 TEST_ASSERT_NONNULL(si_t);
136 TEST_ASSERT_NONNULL(waker);
137 TEST_ASSERT_NONNULL(enq);
138
139 thread_join(t: si_t);
140 thread_join(t: waker);
141 thread_join(t: enq);
142
143 TEST_ASSERT(atomic_load(&si_ok));
144
145 return TEST_SUCCESS;
146}
147
148static atomic_bool sub_apc_ran = false;
149static struct thread *sub_t;
150static atomic_bool sub_interrupted = false;
151static atomic_bool sub_started = false;
152
153static void apc_sub(struct apc_context *ac, void *apc) {
154 cc_ignore(ac);
155 cc_ignore(apc);
156 atomic_store(&sub_apc_ran, true);
157}
158
159static void apc_sub_enq_thread(void *arg) {
160 cc_ignore(arg);
161 struct apc *apc = apc_create();
162 apc_init(a: apc, fn: apc_sub, NULL, destroy: apc_destroy_free);
163
164 while (!atomic_load(&sub_started))
165 cpu_pause();
166
167 rcu_read_lock();
168 bool got = thread_getref_rcu(t: sub_t);
169 rcu_read_unlock();
170
171 if (got) {
172 apc_enqueue(t: sub_t, a: apc, type: APC_TYPE_KERNEL);
173 while (!atomic_load(&sub_apc_ran))
174 scheduler_yield();
175 thread_alert(thread: sub_t);
176 thread_putref(t: sub_t);
177 }
178 apc_putref(a: apc);
179}
180
181static void sleeping_sub_thread(void *arg) {
182 cc_ignore(arg);
183 atomic_store(&sub_started, true);
184
185 thread_park();
186 atomic_store(&sub_interrupted, true);
187}
188
189TEST_DEFINE_INTEGRATION(sched, wait_interruptible_substrate, .min_cores = 3) {
190 atomic_store(&sub_apc_ran, false);
191 atomic_store(&sub_interrupted, false);
192 atomic_store(&sub_started, false);
193
194 sub_t = thread_spawn("sub_th", sleeping_sub_thread, .on_cpu = 1,
195 .joinable = true);
196 struct thread *enq = thread_spawn("sub_enq", apc_sub_enq_thread,
197 .on_cpu = 2, .joinable = true);
198
199 TEST_ASSERT_NONNULL(sub_t);
200 TEST_ASSERT_NONNULL(enq);
201
202 thread_join(t: sub_t);
203 thread_join(t: enq);
204
205 TEST_ASSERT(atomic_load(&sub_apc_ran));
206 TEST_ASSERT(atomic_load(&sub_interrupted));
207
208 return TEST_SUCCESS;
209}
210
211static struct thread *arb_t;
212static atomic_bool arb_started = false;
213static atomic_bool arb_matched = false;
214
215static void arbitrary_sleeping_thread(void *arg) {
216 cc_ignore(arg);
217 atomic_store(&arb_started, true);
218
219 thread_park();
220 atomic_store(&arb_matched, true);
221}
222
223static void arbitrary_waking_thread(void *arg) {
224 cc_ignore(arg);
225 while (!atomic_load(&arb_started))
226 cpu_pause();
227
228 thread_sleep_for_ms(ms: 5);
229
230 thread_alert(thread: arb_t);
231}
232
233TEST_DEFINE_INTEGRATION(sched, park_alert, .min_cores = 3) {
234 atomic_store(&arb_started, false);
235 atomic_store(&arb_matched, false);
236
237 arb_t = thread_spawn("arb_th", arbitrary_sleeping_thread, .on_cpu = 1,
238 .joinable = true);
239 struct thread *waker = thread_spawn("arb_waker", arbitrary_waking_thread,
240 .on_cpu = 2, .joinable = true);
241
242 TEST_ASSERT_NONNULL(arb_t);
243 TEST_ASSERT_NONNULL(waker);
244
245 thread_join(t: arb_t);
246 thread_join(t: waker);
247
248 TEST_ASSERT(atomic_load(&arb_matched));
249
250 return TEST_SUCCESS;
251}
252