Skip to content

tests/kernel/preempt: Add yield and sleep cases #8133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions tests/kernel/sched/preempt/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,18 @@ int do_lock;
/* Command to worker: use irq_offload() to indirect the wakeup? */
int do_irq;

/* Command to worker: sleep after wakeup? */
int do_sleep;

/* Command to worker: yield after wakeup? */
int do_yield;

K_SEM_DEFINE(main_sem, 0, 1);

void wakeup_src_thread(int id)
{
zassert_true(k_current_get() == &manager_thread, "");

/* irq_offload() on ARM appears not to do what we want. It
* doesn't appear to go through the normal exception return
* path and always returns back into the calling context, so
Expand All @@ -86,6 +94,8 @@ void wakeup_src_thread(int id)
return;
}

last_thread = NULL;

/* A little bit of white-box inspection: check that all the
* worker threads are pending.
*/
Expand All @@ -96,17 +106,21 @@ void wakeup_src_thread(int id)
"worker thread %d not pending?", i);
}

/* Wake the src worker up */
last_thread = NULL;
k_sem_give(&worker_sems[id]);

while (do_sleep
&& !(worker_threads[id].base.thread_state & _THREAD_PENDING)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this test immediately break when you submit the k_sleep re-write for v1.13?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely, yes. :) But I'm whiteboxing the thread states here anyway and this is the easiest way to make sure the thread is blocked on the semaphore again before going to the next test case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Removing my -1.

/* spin, waiting on the sleep timeout */
}

/* We are lowest priority, SOMEONE must have run */
zassert_true(!!last_thread, "");
}

void manager(void *p1, void *p2, void *p3)
{
zassert_true(k_current_get() == &manager_thread, "");

for (int src = 0; src < NUM_THREADS; src++) {
for (target = 0; target < NUM_THREADS; target++) {

Expand All @@ -116,6 +130,16 @@ void manager(void *p1, void *p2, void *p3)

for (do_lock = 0; do_lock < 2; do_lock++) {
for (do_irq = 0; do_irq < 2; do_irq++) {
do_yield = 0;
do_sleep = 0;
wakeup_src_thread(src);

do_yield = 1;
do_sleep = 0;
wakeup_src_thread(src);

do_yield = 0;
do_sleep = 1;
wakeup_src_thread(src);
}
}
Expand All @@ -140,6 +164,25 @@ void validate_wakeup(int src, int target, k_tid_t last_thread)
int target_wins = PRI(target) < PRI(src);
int tie = PRI(src) == PRI(target);

if (do_sleep) {
zassert_true(preempted, "sleeping must let any worker run");
return;
}

if (do_yield) {
if (preempted) {
zassert_false(src_wins,
"src (pri %d) should not have yielded to tgt (%d)",
PRI(src), PRI(target));
} else {
zassert_true(src_wins,
"src (pri %d) should have yielded to tgt (%d)",
PRI(src), PRI(target));
}

return;
}

if (preempted) {
zassert_true(target_wins, "preemption must raise priority");
}
Expand Down Expand Up @@ -228,6 +271,21 @@ void worker(void *p1, void *p2, void *p3)
k_sched_unlock();
}

if (do_yield) {
k_yield();
prev = last_thread;
}

if (do_sleep) {
u64_t start = k_uptime_get();

k_sleep(1);

zassert_true(k_uptime_get() - start > 0,
"didn't sleep");
prev = last_thread;
}

validate_wakeup(id, target, prev);
}
}
Expand Down