1 |
root |
1.5 |
/* |
2 |
root |
1.9 |
* libev epoll fd activity backend |
3 |
|
|
* |
4 |
root |
1.5 |
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
5 |
|
|
* All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without |
8 |
|
|
* modification, are permitted provided that the following conditions are |
9 |
|
|
* met: |
10 |
|
|
* |
11 |
|
|
* * Redistributions of source code must retain the above copyright |
12 |
|
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
|
* |
14 |
|
|
* * Redistributions in binary form must reproduce the above |
15 |
|
|
* copyright notice, this list of conditions and the following |
16 |
|
|
* disclaimer in the documentation and/or other materials provided |
17 |
|
|
* with the distribution. |
18 |
|
|
* |
19 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 |
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 |
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 |
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 |
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 |
|
|
*/ |
31 |
|
|
|
32 |
root |
1.32 |
/* |
33 |
|
|
* general notes about epoll: |
34 |
|
|
* |
35 |
|
|
* a) epoll silently removes fds from the fd set. as nothing tells us |
36 |
|
|
* that an fd has been removed otherwise, we have to continually |
37 |
|
|
* "rearm" fds that we suspect *might* have changed (same |
38 |
|
|
* problem with kqueue, but much less costly there). |
39 |
|
|
* b) the fact that ADD != MOD creates a lot of extra syscalls due to a) |
40 |
|
|
* and seems not to have any advantage. |
41 |
|
|
* c) the inability to handle fork or file descriptors (think dup) |
42 |
|
|
* limits the applicability over poll, so this is not a generic |
43 |
|
|
* poll replacement. |
44 |
|
|
* |
45 |
|
|
* lots of "weird code" and complication handling in this file is due |
46 |
|
|
* to these design problems with epoll, as we try very hard to avoid |
47 |
|
|
* epoll_ctl syscalls for common usage patterns. |
48 |
|
|
*/ |
49 |
|
|
|
50 |
root |
1.1 |
#include <sys/epoll.h> |
51 |
|
|
|
52 |
|
|
static void |
53 |
root |
1.13 |
epoll_modify (EV_P_ int fd, int oev, int nev) |
54 |
root |
1.1 |
{ |
55 |
root |
1.32 |
struct epoll_event ev; |
56 |
|
|
|
57 |
|
|
/* |
58 |
|
|
* we handle EPOLL_CTL_DEL by ignoring it here |
59 |
|
|
* on the assumption that the fd is gone anyways |
60 |
|
|
* if that is wrong, we have to handle the spurious |
61 |
|
|
* event in epoll_poll. |
62 |
|
|
*/ |
63 |
|
|
if (!nev) |
64 |
|
|
return; |
65 |
root |
1.1 |
|
66 |
root |
1.11 |
ev.data.u64 = fd; /* use u64 to fully initialise the struct, for nicer strace etc. */ |
67 |
root |
1.32 |
ev.events = (nev & EV_READ ? EPOLLIN : 0) |
68 |
|
|
| (nev & EV_WRITE ? EPOLLOUT : 0); |
69 |
|
|
|
70 |
|
|
if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev))) |
71 |
|
|
return; |
72 |
|
|
|
73 |
|
|
if (expect_true (errno == ENOENT)) |
74 |
|
|
{ |
75 |
|
|
/* on ENOENT the fd went away, so try to do the right thing */ |
76 |
|
|
if (!nev) |
77 |
|
|
return; |
78 |
|
|
|
79 |
|
|
if (!epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev)) |
80 |
|
|
return; |
81 |
|
|
} |
82 |
|
|
else if (expect_true (errno == EEXIST)) |
83 |
|
|
{ |
84 |
|
|
/* on EEXIST we ignored a previous DEL */ |
85 |
|
|
if (!epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev)) |
86 |
|
|
return; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
fd_kill (EV_A_ fd); |
90 |
root |
1.1 |
} |
91 |
|
|
|
92 |
root |
1.6 |
static void |
93 |
root |
1.13 |
epoll_poll (EV_P_ ev_tstamp timeout) |
94 |
root |
1.1 |
{ |
95 |
root |
1.20 |
int i; |
96 |
root |
1.26 |
int eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.)); |
97 |
root |
1.1 |
|
98 |
root |
1.29 |
if (expect_false (eventcnt < 0)) |
99 |
root |
1.20 |
{ |
100 |
|
|
if (errno != EINTR) |
101 |
root |
1.21 |
syserr ("(libev) epoll_wait"); |
102 |
root |
1.20 |
|
103 |
|
|
return; |
104 |
|
|
} |
105 |
root |
1.1 |
|
106 |
|
|
for (i = 0; i < eventcnt; ++i) |
107 |
root |
1.32 |
{ |
108 |
|
|
struct epoll_event *ev = epoll_events + i; |
109 |
|
|
|
110 |
|
|
int fd = ev->data.u64; |
111 |
|
|
int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) |
112 |
|
|
| (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0); |
113 |
|
|
int want = anfds [fd].events; |
114 |
|
|
|
115 |
|
|
if (expect_false (got & ~want)) |
116 |
|
|
{ |
117 |
|
|
/* we received an event but are not interested in it, try mod or del */ |
118 |
|
|
ev->events = (want & EV_READ ? EPOLLIN : 0) |
119 |
|
|
| (want & EV_WRITE ? EPOLLOUT : 0); |
120 |
|
|
|
121 |
|
|
epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
fd_event (EV_A_ fd, got); |
125 |
|
|
} |
126 |
root |
1.1 |
|
127 |
|
|
/* if the receive array was full, increase its size */ |
128 |
root |
1.14 |
if (expect_false (eventcnt == epoll_eventmax)) |
129 |
root |
1.1 |
{ |
130 |
root |
1.20 |
ev_free (epoll_events); |
131 |
root |
1.28 |
epoll_eventmax = array_nextsize (sizeof (struct epoll_event), epoll_eventmax, epoll_eventmax + 1); |
132 |
root |
1.23 |
epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax); |
133 |
root |
1.1 |
} |
134 |
|
|
} |
135 |
|
|
|
136 |
root |
1.27 |
int inline_size |
137 |
root |
1.13 |
epoll_init (EV_P_ int flags) |
138 |
root |
1.1 |
{ |
139 |
root |
1.26 |
backend_fd = epoll_create (256); |
140 |
root |
1.1 |
|
141 |
root |
1.26 |
if (backend_fd < 0) |
142 |
root |
1.13 |
return 0; |
143 |
root |
1.3 |
|
144 |
root |
1.26 |
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
145 |
root |
1.1 |
|
146 |
root |
1.31 |
backend_fudge = 0.; /* kernel sources seem to indicate this to be zero */ |
147 |
root |
1.25 |
backend_modify = epoll_modify; |
148 |
|
|
backend_poll = epoll_poll; |
149 |
root |
1.1 |
|
150 |
root |
1.14 |
epoll_eventmax = 64; /* intiial number of events receivable per poll */ |
151 |
root |
1.23 |
epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax); |
152 |
root |
1.13 |
|
153 |
root |
1.24 |
return EVBACKEND_EPOLL; |
154 |
root |
1.1 |
} |
155 |
root |
1.6 |
|
156 |
root |
1.27 |
void inline_size |
157 |
root |
1.15 |
epoll_destroy (EV_P) |
158 |
|
|
{ |
159 |
root |
1.20 |
ev_free (epoll_events); |
160 |
root |
1.15 |
} |
161 |
|
|
|
162 |
root |
1.27 |
void inline_size |
163 |
root |
1.15 |
epoll_fork (EV_P) |
164 |
|
|
{ |
165 |
root |
1.26 |
close (backend_fd); |
166 |
root |
1.21 |
|
167 |
root |
1.26 |
while ((backend_fd = epoll_create (256)) < 0) |
168 |
root |
1.22 |
syserr ("(libev) epoll_create"); |
169 |
root |
1.21 |
|
170 |
root |
1.26 |
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
171 |
root |
1.15 |
|
172 |
root |
1.16 |
fd_rearm_all (EV_A); |
173 |
root |
1.15 |
} |
174 |
|
|
|