ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_epoll.c
(Generate patch)

Comparing libev/ev_epoll.c (file contents):
Revision 1.7 by root, Wed Oct 31 22:16:37 2007 UTC vs.
Revision 1.32 by root, Thu Dec 13 16:52:50 2007 UTC

1/* 1/*
2 * libev epoll fd activity backend
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 30 */
29 31
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
30#include <sys/epoll.h> 50#include <sys/epoll.h>
31 51
32static int epoll_fd = -1; 52static void
53epoll_modify (EV_P_ int fd, int oev, int nev)
54{
55 struct epoll_event ev;
33 56
34static void 57 /*
35epoll_modify (int fd, int oev, int nev) 58 * we handle EPOLL_CTL_DEL by ignoring it here
36{ 59 * on the assumption that the fd is gone anyways
37 int mode = nev ? oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD : EPOLL_CTL_DEL; 60 * if that is wrong, we have to handle the spurious
61 * event in epoll_poll.
62 */
63 if (!nev)
64 return;
38 65
39 struct epoll_event ev; 66 ev.data.u64 = fd; /* use u64 to fully initialise the struct, for nicer strace etc. */
40 ev.data.fd = fd;
41 ev.events =
42 (nev & EV_READ ? EPOLLIN : 0) 67 ev.events = (nev & EV_READ ? EPOLLIN : 0)
43 | (nev & EV_WRITE ? EPOLLOUT : 0); 68 | (nev & EV_WRITE ? EPOLLOUT : 0);
44 69
45 epoll_ctl (epoll_fd, mode, fd, &ev); 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);
46} 90}
47 91
48static void 92static void
49epoll_postfork_child (void) 93epoll_poll (EV_P_ ev_tstamp timeout)
50{ 94{
51 int fd; 95 int i;
96 int eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.));
52 97
53 epoll_fd = epoll_create (256); 98 if (expect_false (eventcnt < 0))
54 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); 99 {
100 if (errno != EINTR)
101 syserr ("(libev) epoll_wait");
55 102
56 /* re-register interest in fds */
57 for (fd = 0; fd < anfdmax; ++fd)
58 if (anfds [fd].events && !(anfds [fd].events & EV_REIFY))//D
59 epoll_modify (fd, EV_NONE, anfds [fd].events);
60}
61
62static struct epoll_event *events;
63static int eventmax;
64
65static void
66epoll_poll (ev_tstamp timeout)
67{
68 int eventcnt = epoll_wait (epoll_fd, events, eventmax, ceil (timeout * 1000.));
69 int i;
70
71 if (eventcnt < 0)
72 return; 103 return;
104 }
73 105
74 for (i = 0; i < eventcnt; ++i) 106 for (i = 0; i < eventcnt; ++i)
75 fd_event ( 107 {
76 events [i].data.fd, 108 struct epoll_event *ev = epoll_events + i;
109
110 int fd = ev->data.u64;
77 (events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) 111 int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
78 | (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0) 112 | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0);
79 ); 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 }
80 126
81 /* if the receive array was full, increase its size */ 127 /* if the receive array was full, increase its size */
82 if (eventcnt == eventmax) 128 if (expect_false (eventcnt == epoll_eventmax))
83 { 129 {
84 free (events); 130 ev_free (epoll_events);
85 eventmax += eventmax >> 1; 131 epoll_eventmax = array_nextsize (sizeof (struct epoll_event), epoll_eventmax, epoll_eventmax + 1);
86 events = malloc (sizeof (struct epoll_event) * eventmax); 132 epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
87 } 133 }
88} 134}
89 135
90static void 136int inline_size
91epoll_init (int flags) 137epoll_init (EV_P_ int flags)
92{ 138{
93 epoll_fd = epoll_create (256); 139 backend_fd = epoll_create (256);
94 140
95 if (epoll_fd < 0) 141 if (backend_fd < 0)
96 return; 142 return 0;
97 143
98 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); 144 fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
99 145
100 ev_method = EVMETHOD_EPOLL; 146 backend_fudge = 0.; /* kernel sources seem to indicate this to be zero */
101 method_fudge = 1e-3; /* needed to compensate for epoll returning early */
102 method_modify = epoll_modify; 147 backend_modify = epoll_modify;
103 method_poll = epoll_poll; 148 backend_poll = epoll_poll;
104 149
105 eventmax = 64; /* intiial number of events receivable per poll */ 150 epoll_eventmax = 64; /* intiial number of events receivable per poll */
106 events = malloc (sizeof (struct epoll_event) * eventmax); 151 epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
152
153 return EVBACKEND_EPOLL;
107} 154}
108 155
156void inline_size
157epoll_destroy (EV_P)
158{
159 ev_free (epoll_events);
160}
161
162void inline_size
163epoll_fork (EV_P)
164{
165 close (backend_fd);
166
167 while ((backend_fd = epoll_create (256)) < 0)
168 syserr ("(libev) epoll_create");
169
170 fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
171
172 fd_rearm_all (EV_A);
173}
174

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines