You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.2 KiB
C
62 lines
1.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <eventloop.h>
|
|
|
|
int main() {
|
|
struct tlsl_event timer, pipe_ev;
|
|
EVENTLOOP_STACK_SCOPED(el);
|
|
tlsl_event_id_t id;
|
|
uint32_t flags;
|
|
|
|
int pfds[2];
|
|
if (pipe(pfds))
|
|
return 1;
|
|
|
|
pid_t pid = fork();
|
|
if (pid < 0)
|
|
return 1;
|
|
|
|
if (!pid) {
|
|
char buf[] = "hello!";
|
|
sleep(5);
|
|
write(pfds[1], buf, sizeof(buf));
|
|
exit(0);
|
|
}
|
|
|
|
if (tlsl_eventloop_init(&el))
|
|
return 1;
|
|
|
|
if (tlsl_eventloop_add_timer(&el, &timer, 2000, false))
|
|
return 1;
|
|
|
|
if (tlsl_eventloop_add_fd(&el, &pipe_ev, pfds[0], TLSL_EVENT_READABLE))
|
|
return 1;
|
|
|
|
for (;;) {
|
|
if (tlsl_eventloop_wait(&el, &id, &flags, -1)) {
|
|
fprintf(stderr, "failed to wait");
|
|
continue;
|
|
}
|
|
|
|
if (tlsl_event_id_eq(id, timer.id)) {
|
|
tlsl_eventloop_clear(&el, &timer);
|
|
printf("timer tick flags=%u\n", flags);
|
|
}
|
|
else if (tlsl_event_id_eq(id, pipe_ev.id)) {
|
|
char buf[1024] = { 0 };
|
|
read(pfds[0], buf, sizeof(buf) - 1);
|
|
printf("pipe event flags=%u buf=%s\n", flags, buf);
|
|
break;
|
|
}
|
|
else {
|
|
fprintf(stderr, "unknown id");
|
|
}
|
|
}
|
|
|
|
tlsl_eventloop_remove(&el, &timer);
|
|
tlsl_eventloop_remove(&el, &pipe_ev);
|
|
|
|
return 0;
|
|
}
|