Added timers.c demo for uc_timers
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <ucore/ucore_timers.h>
|
||||
#include <ucore/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 mystruct_cb(struct UCTimers *timers, struct UCTimer *timer)
|
||||
{
|
||||
struct MyStruct *mystruct = CONTAINER_OF(timer, struct MyStruct, timer);
|
||||
|
||||
TRACEF("MyStruct timer: %s\n", mystruct->str);
|
||||
uc_timer_add(timers, timer, 1, 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_struct = {
|
||||
.str = "Hello",
|
||||
.timer = {
|
||||
.callback = mystruct_cb,
|
||||
},
|
||||
};
|
||||
|
||||
uc_timers_init(&timers);
|
||||
uc_timer_add(&timers, &t1, 2, 0);
|
||||
uc_timer_add(&timers, &t2, 5, 0);
|
||||
uc_timer_add(&timers, &my_struct.timer, 10, 0);
|
||||
|
||||
for(;;) {
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
uc_timers_run(&timers, &now);
|
||||
#ifndef FULL_SPEED
|
||||
usleep(1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user