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