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