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