1 | #include <sys/epoll.h> |
1 | #include <sys/epoll.h> |
2 | |
2 | |
3 | static int epoll_fd = -1; |
3 | static int epoll_fd = -1; |
4 | |
4 | |
5 | static void |
5 | static void |
6 | epoll_reify_fd (int fd) |
6 | epoll_modify (int fd, int oev, int nev) |
7 | { |
7 | { |
8 | ANFD *anfd = anfds + fd; |
8 | int mode = nev ? oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD : EPOLL_CTL_DEL; |
9 | struct ev_io *w; |
|
|
10 | |
9 | |
11 | int wev = 0; |
10 | struct epoll_event ev; |
|
|
11 | ev.data.fd = fd; |
|
|
12 | ev.events = |
|
|
13 | (nev & EV_READ ? EPOLLIN : 0) |
|
|
14 | | (nev & EV_WRITE ? EPOLLOUT : 0); |
12 | |
15 | |
13 | for (w = anfd->head; w; w = w->next) |
|
|
14 | wev |= w->events; |
|
|
15 | |
|
|
16 | if (anfd->wev != wev) |
|
|
17 | { |
|
|
18 | int mode = wev ? anfd->wev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD : EPOLL_CTL_DEL; |
|
|
19 | struct epoll_event ev; |
|
|
20 | ev.events = wev; |
|
|
21 | ev.data.fd = fd; |
|
|
22 | fprintf (stderr, "reify %d,%d,%d m%d (r=%d)\n", fd, anfd->wev, wev, mode,//D |
|
|
23 | epoll_ctl (epoll_fd, mode, fd, &ev) |
16 | epoll_ctl (epoll_fd, mode, fd, &ev); |
24 | );//D |
|
|
25 | anfd->wev = wev; |
|
|
26 | } |
|
|
27 | } |
17 | } |
28 | |
18 | |
29 | void epoll_postfork_child (void) |
19 | void epoll_postfork_child (void) |
30 | { |
20 | { |
31 | int i; |
21 | int fd; |
32 | |
22 | |
33 | epoll_fd = epoll_create (256); |
23 | epoll_fd = epoll_create (256); |
|
|
24 | fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); |
34 | |
25 | |
|
|
26 | /* re-register interest in fds */ |
35 | for (i = 0; i < anfdmax; ++i) |
27 | for (fd = 0; fd < anfdmax; ++fd) |
36 | epoll_reify_fd (i); |
28 | if (anfds [fd].wev) |
37 | } |
29 | epoll_modify (fd, EV_NONE, anfds [fd].wev); |
38 | |
|
|
39 | static void epoll_reify (void) |
|
|
40 | { |
|
|
41 | int i; |
|
|
42 | for (i = 0; i < fdchangecnt; ++i) |
|
|
43 | epoll_reify_fd (fdchanges [i]); |
|
|
44 | } |
30 | } |
45 | |
31 | |
46 | static struct epoll_event *events; |
32 | static struct epoll_event *events; |
47 | static int eventmax; |
33 | static int eventmax; |
48 | |
34 | |
… | |
… | |
55 | return; |
41 | return; |
56 | |
42 | |
57 | for (i = 0; i < eventcnt; ++i) |
43 | for (i = 0; i < eventcnt; ++i) |
58 | fd_event ( |
44 | fd_event ( |
59 | events [i].data.fd, |
45 | events [i].data.fd, |
60 | (events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP | EPOLLPRI) ? EV_WRITE : 0) |
46 | (events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) |
61 | | (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0) |
47 | | (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0) |
62 | ); |
48 | ); |
63 | |
49 | |
64 | /* if the receive array was full, increase its size */ |
50 | /* if the receive array was full, increase its size */ |
65 | if (eventcnt == eventmax) |
51 | if (eventcnt == eventmax) |
66 | { |
52 | { |
… | |
… | |
68 | eventmax += eventmax >> 1; |
54 | eventmax += eventmax >> 1; |
69 | events = malloc (sizeof (struct epoll_event) * eventmax); |
55 | events = malloc (sizeof (struct epoll_event) * eventmax); |
70 | } |
56 | } |
71 | } |
57 | } |
72 | |
58 | |
73 | int epoll_init (int flags) |
59 | void epoll_init (int flags) |
74 | { |
60 | { |
75 | epoll_fd = epoll_create (256); |
61 | epoll_fd = epoll_create (256); |
76 | |
62 | |
77 | if (epoll_fd < 0) |
63 | if (epoll_fd < 0) |
78 | return 0; |
64 | return; |
79 | |
65 | |
|
|
66 | fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); |
|
|
67 | |
80 | ev_method = EVMETHOD_EPOLL; |
68 | ev_method = EVMETHOD_EPOLL; |
81 | method_fudge = 1e-3; /* needed to compensate fro epoll returning early */ |
69 | method_fudge = 1e-3; /* needed to compensate for epoll returning early */ |
82 | method_reify = epoll_reify; |
70 | method_modify = epoll_modify; |
83 | method_poll = epoll_poll; |
71 | method_poll = epoll_poll; |
84 | |
72 | |
85 | eventmax = 64; /* intiial number of events receivable per poll */ |
73 | eventmax = 64; /* intiial number of events receivable per poll */ |
86 | events = malloc (sizeof (struct epoll_event) * eventmax); |
74 | events = malloc (sizeof (struct epoll_event) * eventmax); |
87 | |
|
|
88 | return 1; |
|
|
89 | } |
75 | } |