|
13 | 13 | */
|
14 | 14 | #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
|
15 | 15 |
|
| 16 | +#define MSEC_TO_CYCLES(msec) (int)(((uint64_t)(msec) * \ |
| 17 | + (uint64_t)CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC) / \ |
| 18 | + (uint64_t)MSEC_PER_SEC) |
| 19 | + |
16 | 20 | struct k_thread worker_threads[NUM_THREADS];
|
| 21 | +volatile struct k_thread *expected_thread; |
17 | 22 | k_tid_t worker_tids[NUM_THREADS];
|
18 | 23 |
|
19 | 24 | K_THREAD_STACK_ARRAY_DEFINE(worker_stacks, NUM_THREADS, STACK_SIZE);
|
@@ -215,4 +220,105 @@ ZTEST(suite_deadline, test_unqueued)
|
215 | 220 | }
|
216 | 221 | }
|
217 | 222 |
|
| 223 | +#if (CONFIG_MP_MAX_NUM_CPUS == 1) |
| 224 | +static void reschedule_wrapper(const void *param) |
| 225 | +{ |
| 226 | + ARG_UNUSED(param); |
| 227 | + |
| 228 | + k_reschedule(); |
| 229 | +} |
| 230 | + |
| 231 | +static void test_reschedule_helper0(void *p1, void *p2, void *p3) |
| 232 | +{ |
| 233 | + /* 4. Reschedule brings us here */ |
| 234 | + |
| 235 | + zassert_true(expected_thread == arch_current_thread(), ""); |
| 236 | + |
| 237 | + expected_thread = &worker_threads[1]; |
| 238 | +} |
| 239 | + |
| 240 | +static void test_reschedule_helper1(void *p1, void *p2, void *p3) |
| 241 | +{ |
| 242 | + void (*offload)(void (*f)(const void *p), const void *param) = p1; |
| 243 | + |
| 244 | + /* 1. First helper expected to execute */ |
| 245 | + |
| 246 | + zassert_true(expected_thread == arch_current_thread(), ""); |
| 247 | + |
| 248 | + offload(reschedule_wrapper, NULL); |
| 249 | + |
| 250 | + /* 2. Deadlines have not changed. Expected no changes */ |
| 251 | + |
| 252 | + zassert_true(expected_thread == arch_current_thread(), ""); |
| 253 | + |
| 254 | + k_thread_deadline_set(arch_current_thread(), MSEC_TO_CYCLES(1000)); |
| 255 | + |
| 256 | + /* 3. Deadline changed, but there was no reschedule */ |
| 257 | + |
| 258 | + zassert_true(expected_thread == arch_current_thread(), ""); |
| 259 | + |
| 260 | + expected_thread = &worker_threads[0]; |
| 261 | + offload(reschedule_wrapper, NULL); |
| 262 | + |
| 263 | + /* 5. test_thread_reschedule_helper0 executed */ |
| 264 | + |
| 265 | + zassert_true(expected_thread == arch_current_thread(), ""); |
| 266 | +} |
| 267 | + |
| 268 | +static void thread_offload(void (*f)(const void *p), const void *param) |
| 269 | +{ |
| 270 | + f(param); |
| 271 | +} |
| 272 | + |
| 273 | +ZTEST(suite_deadline, test_thread_reschedule) |
| 274 | +{ |
| 275 | + k_thread_create(&worker_threads[0], worker_stacks[0], STACK_SIZE, |
| 276 | + test_reschedule_helper0, |
| 277 | + thread_offload, NULL, NULL, |
| 278 | + K_LOWEST_APPLICATION_THREAD_PRIO, |
| 279 | + 0, K_NO_WAIT); |
| 280 | + |
| 281 | + k_thread_create(&worker_threads[1], worker_stacks[1], STACK_SIZE, |
| 282 | + test_reschedule_helper1, |
| 283 | + thread_offload, NULL, NULL, |
| 284 | + K_LOWEST_APPLICATION_THREAD_PRIO, |
| 285 | + 0, K_NO_WAIT); |
| 286 | + |
| 287 | + k_thread_deadline_set(&worker_threads[0], MSEC_TO_CYCLES(500)); |
| 288 | + k_thread_deadline_set(&worker_threads[1], MSEC_TO_CYCLES(10)); |
| 289 | + |
| 290 | + expected_thread = &worker_threads[1]; |
| 291 | + |
| 292 | + k_thread_join(&worker_threads[1], K_FOREVER); |
| 293 | + k_thread_join(&worker_threads[0], K_FOREVER); |
| 294 | + |
| 295 | +#ifndef CONFIG_SMP |
| 296 | + /* |
| 297 | + * When SMP is enabled, there is always a reschedule performed |
| 298 | + * at the end of the ISR. |
| 299 | + */ |
| 300 | + k_thread_create(&worker_threads[0], worker_stacks[0], STACK_SIZE, |
| 301 | + test_reschedule_helper0, |
| 302 | + irq_offload, NULL, NULL, |
| 303 | + K_LOWEST_APPLICATION_THREAD_PRIO, |
| 304 | + 0, K_NO_WAIT); |
| 305 | + |
| 306 | + k_thread_create(&worker_threads[1], worker_stacks[1], STACK_SIZE, |
| 307 | + test_reschedule_helper1, |
| 308 | + irq_offload, NULL, NULL, |
| 309 | + K_LOWEST_APPLICATION_THREAD_PRIO, |
| 310 | + 0, K_NO_WAIT); |
| 311 | + |
| 312 | + k_thread_deadline_set(&worker_threads[0], MSEC_TO_CYCLES(500)); |
| 313 | + k_thread_deadline_set(&worker_threads[1], MSEC_TO_CYCLES(10)); |
| 314 | + |
| 315 | + expected_thread = &worker_threads[1]; |
| 316 | + |
| 317 | + k_thread_join(&worker_threads[1], K_FOREVER); |
| 318 | + k_thread_join(&worker_threads[0], K_FOREVER); |
| 319 | + |
| 320 | +#endif /* !CONFIG_SMP */ |
| 321 | +} |
| 322 | +#endif /* CONFIG_MP_MAX_NUM_CPUS == 1 */ |
| 323 | + |
218 | 324 | ZTEST_SUITE(suite_deadline, NULL, NULL, NULL, NULL, NULL);
|
0 commit comments