|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <zephyr.h> |
| 7 | +#include <ztest.h> |
| 8 | + |
| 9 | +#define NUM_THREADS 8 |
| 10 | +#define STACK_SIZE (256 + CONFIG_TEST_EXTRA_STACKSIZE) |
| 11 | + |
| 12 | +struct k_thread worker_threads[NUM_THREADS]; |
| 13 | + |
| 14 | +K_THREAD_STACK_ARRAY_DEFINE(worker_stacks, NUM_THREADS, STACK_SIZE); |
| 15 | + |
| 16 | +int thread_deadlines[NUM_THREADS]; |
| 17 | + |
| 18 | +/* The number of worker threads that ran, and and array of their |
| 19 | + * indices in execution order |
| 20 | + */ |
| 21 | +int n_exec; |
| 22 | +int exec_order[NUM_THREADS]; |
| 23 | + |
| 24 | +void worker(void *p1, void *p2, void *p3) |
| 25 | +{ |
| 26 | + int tidx = (int) p1; |
| 27 | + |
| 28 | + ARG_UNUSED(p2); |
| 29 | + ARG_UNUSED(p3); |
| 30 | + |
| 31 | + zassert_true(tidx >= 0 && tidx < NUM_THREADS, ""); |
| 32 | + zassert_true(n_exec >= 0 && n_exec < NUM_THREADS, ""); |
| 33 | + |
| 34 | + exec_order[n_exec++] = tidx; |
| 35 | + |
| 36 | + /* Sleep, don't exit. It's not implausible that some |
| 37 | + * platforms implement a thread-based cleanup step for threads |
| 38 | + * that exit (pthreads does this already) which might muck |
| 39 | + * with the scheduling. |
| 40 | + */ |
| 41 | + while (1) { |
| 42 | + k_sleep(1000000); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void test_deadline(void) |
| 47 | +{ |
| 48 | + int i; |
| 49 | + |
| 50 | + /* Create a bunch of threads at a single lower priority. Give |
| 51 | + * them each a random deadline. Sleep, and check that they |
| 52 | + * were executed in the right order. |
| 53 | + */ |
| 54 | + for (i = 0; i < NUM_THREADS; i++) { |
| 55 | + k_thread_create(&worker_threads[i], |
| 56 | + worker_stacks[i], STACK_SIZE, |
| 57 | + worker, (void *)i, NULL, NULL, |
| 58 | + K_LOWEST_APPLICATION_THREAD_PRIO, |
| 59 | + 0, 0); |
| 60 | + |
| 61 | + /* Positive-definite number with the bottom 8 bits |
| 62 | + * masked off to prevent aliasing where "very close" |
| 63 | + * deadlines end up in the opposite order due to the |
| 64 | + * changing "now" between calls to |
| 65 | + * k_thread_deadline_set(). |
| 66 | + */ |
| 67 | + thread_deadlines[i] = sys_rand32_get() & 0x7fffff00; |
| 68 | + } |
| 69 | + |
| 70 | + zassert_true(n_exec == 0, "threads ran too soon"); |
| 71 | + |
| 72 | + /* Similarly do the deadline setting in one quick pass to |
| 73 | + * minimize aliasing with "now" |
| 74 | + */ |
| 75 | + for (i = 0; i < NUM_THREADS; i++) { |
| 76 | + k_thread_deadline_set(&worker_threads[i], thread_deadlines[i]); |
| 77 | + } |
| 78 | + |
| 79 | + zassert_true(n_exec == 0, "threads ran too soon"); |
| 80 | + |
| 81 | + k_sleep(100); |
| 82 | + |
| 83 | + zassert_true(n_exec == NUM_THREADS, "not enough threads ran"); |
| 84 | + |
| 85 | + for (i = 1; i < NUM_THREADS; i++) { |
| 86 | + int d0 = thread_deadlines[exec_order[i-1]]; |
| 87 | + int d1 = thread_deadlines[exec_order[i]]; |
| 88 | + |
| 89 | + zassert_true(d0 <= d1, "threads ran in wrong order"); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +void test_main(void) |
| 94 | +{ |
| 95 | + ztest_test_suite(suite_deadline, |
| 96 | + ztest_unit_test(test_deadline)); |
| 97 | + ztest_run_test_suite(suite_deadline); |
| 98 | +} |
0 commit comments