[ifxmips]
[openwrt.git] / package / libtapi / src / events.c
1 #include <stdbool.h>
2 #include <sys/epoll.h>
3 #include <stdlib.h>
4 #include <errno.h>
5
6 #include <stdio.h>
7
8 #include "events.h"
9
10 struct event_callback *event_callbacks;
11
12 static int event_epoll_fd = -1;
13
14 int event_register(int fd, int events, struct event_callback *cb)
15 {
16 struct epoll_event ev;
17
18 if (event_epoll_fd == -1)
19 event_epoll_fd = epoll_create(1);
20
21 ev.events = events;
22 ev.data.ptr = cb;
23
24 cb->fd = fd;
25
26 return epoll_ctl(event_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
27 }
28
29 int event_unregister(int fd)
30 {
31 return epoll_ctl(event_epoll_fd, EPOLL_CTL_DEL, fd, NULL);
32 }
33
34 int tapi_mainloop(void)
35 {
36 struct epoll_event ev[10];
37 struct event_callback *cb;
38 int ret;
39 bool keep;
40 int i;
41
42 if (event_epoll_fd == -1)
43 event_epoll_fd = epoll_create(1);
44
45 while(true) {
46 ret = epoll_wait(event_epoll_fd, ev, 10, -1);
47 for(i = 0; i < ret; ++i) {
48 cb = ev[i].data.ptr;
49 keep = cb->callback(ev[i].events, cb->data);
50 if (!keep)
51 event_unregister(cb->fd);
52 }
53 if (ret < 0)
54 printf("epoll: %d\n", errno);
55 }
56
57 return 0;
58 }
This page took 0.041467 seconds and 5 git commands to generate.