|
8 | 8 | #include <random/rand32.h> |
9 | 9 |
|
10 | 10 | #define NUM_THREADS 8 |
11 | | -#define STACK_SIZE (256 + CONFIG_TEST_EXTRA_STACKSIZE) |
| 11 | +/* this should be large enough for us |
| 12 | + * to print a failing assert if necessary |
| 13 | + */ |
| 14 | +#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE) |
12 | 15 |
|
13 | 16 | struct k_thread worker_threads[NUM_THREADS]; |
| 17 | +k_tid_t worker_tids[NUM_THREADS]; |
14 | 18 |
|
15 | 19 | K_THREAD_STACK_ARRAY_DEFINE(worker_stacks, NUM_THREADS, STACK_SIZE); |
16 | 20 |
|
@@ -53,7 +57,7 @@ void test_deadline(void) |
53 | 57 | * were executed in the right order. |
54 | 58 | */ |
55 | 59 | for (i = 0; i < NUM_THREADS; i++) { |
56 | | - k_thread_create(&worker_threads[i], |
| 60 | + worker_tids[i] = k_thread_create(&worker_threads[i], |
57 | 61 | worker_stacks[i], STACK_SIZE, |
58 | 62 | worker, INT_TO_POINTER(i), NULL, NULL, |
59 | 63 | K_LOWEST_APPLICATION_THREAD_PRIO, |
@@ -98,11 +102,65 @@ void test_deadline(void) |
98 | 102 |
|
99 | 103 | zassert_true(d0 <= d1, "threads ran in wrong order"); |
100 | 104 | } |
| 105 | + for (i = 0; i < NUM_THREADS; i++) { |
| 106 | + k_thread_abort(worker_tids[i]); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +void yield_worker(void *p1, void *p2, void *p3) |
| 111 | +{ |
| 112 | + ARG_UNUSED(p1); |
| 113 | + ARG_UNUSED(p2); |
| 114 | + ARG_UNUSED(p3); |
| 115 | + |
| 116 | + zassert_true(n_exec >= 0 && n_exec < NUM_THREADS, ""); |
| 117 | + |
| 118 | + n_exec += 1; |
| 119 | + |
| 120 | + k_yield(); |
| 121 | + |
| 122 | + /* should not get here until all threads have started */ |
| 123 | + zassert_true(n_exec == NUM_THREADS, ""); |
| 124 | + |
| 125 | + k_thread_abort(k_current_get()); |
| 126 | +} |
| 127 | + |
| 128 | +void test_yield(void) |
| 129 | +{ |
| 130 | + /* Test that yield works across threads with the |
| 131 | + * same deadline and priority. This currently works by |
| 132 | + * simply not setting a deadline, which results in a |
| 133 | + * deadline of 0. |
| 134 | + */ |
| 135 | + |
| 136 | + int i; |
| 137 | + |
| 138 | + n_exec = 0; |
| 139 | + |
| 140 | + /* Create a bunch of threads at a single lower priority |
| 141 | + * and deadline. |
| 142 | + * Each thread increments its own variable, then yields |
| 143 | + * to the next. Sleep. Check that all threads ran. |
| 144 | + */ |
| 145 | + for (i = 0; i < NUM_THREADS; i++) { |
| 146 | + k_thread_create(&worker_threads[i], |
| 147 | + worker_stacks[i], STACK_SIZE, |
| 148 | + yield_worker, NULL, NULL, NULL, |
| 149 | + K_LOWEST_APPLICATION_THREAD_PRIO, |
| 150 | + 0, K_NO_WAIT); |
| 151 | + } |
| 152 | + |
| 153 | + zassert_true(n_exec == 0, "threads ran too soon"); |
| 154 | + |
| 155 | + k_sleep(K_MSEC(100)); |
| 156 | + |
| 157 | + zassert_true(n_exec == NUM_THREADS, "not enough threads ran"); |
101 | 158 | } |
102 | 159 |
|
103 | 160 | void test_main(void) |
104 | 161 | { |
105 | 162 | ztest_test_suite(suite_deadline, |
106 | | - ztest_unit_test(test_deadline)); |
| 163 | + ztest_unit_test(test_deadline), |
| 164 | + ztest_unit_test(test_yield)); |
107 | 165 | ztest_run_test_suite(suite_deadline); |
108 | 166 | } |
0 commit comments