rt/examples/sleep.c

32 lines
761 B
C

#include <rt/assert.h>
#include <rt/atomic.h>
#include <rt/log.h>
#include <rt/sem.h>
#include <rt/task.h>
#include <rt/tick.h>
#include <rt/trap.h>
static const int nloops = 5;
static void sleep_periodic(uintptr_t period)
{
rt_task_drop_privilege();
unsigned long last_wake_tick = 0;
for (int i = 0; i < nloops; ++i)
{
rt_task_sleep_periodic(&last_wake_tick, period);
rt_assert(rt_tick_count() == last_wake_tick,
"woke up at the wrong tick");
}
// Only the second task to finish will call rt_trap.
static RT_SEM(trap_sem, 1);
if (!rt_sem_trywait(&trap_sem))
{
rt_trap();
}
}
RT_TASK_ARG(sleep_periodic, 5, RT_STACK_MIN, 0);
RT_TASK_ARG(sleep_periodic, 10, RT_STACK_MIN, 1);