1 |
root |
1.1 |
#include <sys/epoll.h> |
2 |
|
|
|
3 |
|
|
static int epoll_fd = -1; |
4 |
|
|
|
5 |
|
|
static void |
6 |
|
|
epoll_reify_fd (int fd) |
7 |
|
|
{ |
8 |
|
|
ANFD *anfd = anfds + fd; |
9 |
|
|
struct ev_io *w; |
10 |
|
|
|
11 |
|
|
int wev = 0; |
12 |
|
|
|
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) |
24 |
|
|
);//D |
25 |
|
|
anfd->wev = wev; |
26 |
|
|
} |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
void epoll_postfork_child (void) |
30 |
|
|
{ |
31 |
|
|
int i; |
32 |
|
|
|
33 |
|
|
epoll_fd = epoll_create (256); |
34 |
|
|
|
35 |
|
|
for (i = 0; i < anfdmax; ++i) |
36 |
|
|
epoll_reify_fd (i); |
37 |
|
|
} |
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 |
|
|
} |
45 |
|
|
|
46 |
|
|
static struct epoll_event *events; |
47 |
|
|
static int eventmax; |
48 |
|
|
|
49 |
|
|
static void epoll_poll (ev_tstamp timeout) |
50 |
|
|
{ |
51 |
|
|
int eventcnt = epoll_wait (epoll_fd, events, eventmax, ceil (timeout * 1000.)); |
52 |
|
|
int i; |
53 |
|
|
|
54 |
|
|
if (eventcnt < 0) |
55 |
|
|
return; |
56 |
|
|
|
57 |
|
|
for (i = 0; i < eventcnt; ++i) |
58 |
|
|
fd_event ( |
59 |
|
|
events [i].data.fd, |
60 |
|
|
(events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP | EPOLLPRI) ? EV_WRITE : 0) |
61 |
|
|
| (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0) |
62 |
|
|
); |
63 |
|
|
|
64 |
|
|
/* if the receive array was full, increase its size */ |
65 |
|
|
if (eventcnt == eventmax) |
66 |
|
|
{ |
67 |
|
|
free (events); |
68 |
|
|
eventmax += eventmax >> 1; |
69 |
|
|
events = malloc (sizeof (struct epoll_event) * eventmax); |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
int epoll_init (int flags) |
74 |
|
|
{ |
75 |
|
|
epoll_fd = epoll_create (256); |
76 |
|
|
|
77 |
|
|
if (epoll_fd < 0) |
78 |
|
|
return 0; |
79 |
|
|
|
80 |
|
|
ev_method = EVMETHOD_EPOLL; |
81 |
|
|
method_fudge = 1e-3; /* needed to compensate fro epoll returning early */ |
82 |
|
|
method_reify = epoll_reify; |
83 |
|
|
method_poll = epoll_poll; |
84 |
|
|
|
85 |
|
|
eventmax = 64; /* intiial number of events receivable per poll */ |
86 |
|
|
events = malloc (sizeof (struct epoll_event) * eventmax); |
87 |
|
|
|
88 |
|
|
return 1; |
89 |
|
|
} |