Skip to content

Commit c2a777c

Browse files
jharris-intelcarlescufi
authored andcommitted
tests: kernel: sched: add z_yield under deadline
Add a test case for the error fixed by the previous commit. Signed-off-by: James Harris <[email protected]>
1 parent 3330ab1 commit c2a777c

File tree

1 file changed

+61
-3
lines changed
  • tests/kernel/sched/deadline/src

1 file changed

+61
-3
lines changed

tests/kernel/sched/deadline/src/main.c

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
#include <random/rand32.h>
99

1010
#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)
1215

1316
struct k_thread worker_threads[NUM_THREADS];
17+
k_tid_t worker_tids[NUM_THREADS];
1418

1519
K_THREAD_STACK_ARRAY_DEFINE(worker_stacks, NUM_THREADS, STACK_SIZE);
1620

@@ -53,7 +57,7 @@ void test_deadline(void)
5357
* were executed in the right order.
5458
*/
5559
for (i = 0; i < NUM_THREADS; i++) {
56-
k_thread_create(&worker_threads[i],
60+
worker_tids[i] = k_thread_create(&worker_threads[i],
5761
worker_stacks[i], STACK_SIZE,
5862
worker, INT_TO_POINTER(i), NULL, NULL,
5963
K_LOWEST_APPLICATION_THREAD_PRIO,
@@ -98,11 +102,65 @@ void test_deadline(void)
98102

99103
zassert_true(d0 <= d1, "threads ran in wrong order");
100104
}
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");
101158
}
102159

103160
void test_main(void)
104161
{
105162
ztest_test_suite(suite_deadline,
106-
ztest_unit_test(test_deadline));
163+
ztest_unit_test(test_deadline),
164+
ztest_unit_test(test_yield));
107165
ztest_run_test_suite(suite_deadline);
108166
}

0 commit comments

Comments
 (0)