Files
libucore/test/timers.c
T
2014-09-12 01:11:41 +02:00

80 lines
1.6 KiB
C

#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include "ucore/timers.h"
#include "ucore/utils.h"
struct MyStruct {
char *str;
struct UCTimer timer;
};
void t1_cb(struct UCTimers *timers, struct UCTimer *timer)
{
TRACEF("T1 expired\n");
}
void t2_cb(struct UCTimers *timers, struct UCTimer *timer)
{
TRACEF("T2 expired\n");
}
void mystruct1_cb(struct UCTimers *timers, struct UCTimer *timer)
{
struct MyStruct *mystruct = UC_CONTAINER_OF(timer, struct MyStruct, timer);
TRACEF("MyStruct1 timer: %s\n", mystruct->str);
uc_timers_add(timers, timer, 1, 0);
}
void mystruct2_cb(struct UCTimers *timers, struct UCTimer *timer)
{
struct MyStruct *mystruct = timer->cookie_ptr;
TRACEF("MyStruct2 timer: %s\n", mystruct->str);
uc_timers_add(timers, timer, 5, 0);
}
int main(int argc, char *argv[])
{
struct UCTimers timers;
struct UCTimer t1 = {
.callback = t1_cb,
};
struct UCTimer t2 = {
.callback = t2_cb,
};
struct MyStruct my_struct1 = {
.str = "Hello",
.timer = {
.callback = mystruct1_cb,
},
};
struct MyStruct my_struct2 = {
.str = "Hello again",
.timer = {
.callback = mystruct2_cb,
.cookie_ptr = &my_struct2,
},
};
uc_timers_init(&timers);
uc_timers_add(&timers, &t1, 2, 0);
uc_timers_add(&timers, &t2, 5, 0);
uc_timers_add(&timers, &my_struct1.timer, 10, 0);
uc_timers_add(&timers, &my_struct2.timer, 10, 0);
for(;;) {
uc_timers_run(&timers);
#ifndef FULL_SPEED
usleep(1000);
#endif
}
return 0;
}