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

Comparing libev/ev_poll.c (file contents):
Revision 1.1 by root, Fri Nov 2 15:56:19 2007 UTC vs.
Revision 1.11 by root, Sat Nov 10 17:47:05 2007 UTC

27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (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
29 * 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.
30 */ 30 */
31 31
32#include <sys/epoll.h> 32#include <poll.h>
33
34static int epoll_fd = -1;
35 33
36static void 34static void
37epoll_modify (int fd, int oev, int nev) 35pollidx_init (int *base, int count)
38{ 36{
39 int mode = nev ? oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD : EPOLL_CTL_DEL; 37 while (count--)
40 38 *base++ = -1;
41 struct epoll_event ev;
42 ev.data.fd = fd;
43 ev.events =
44 (nev & EV_READ ? EPOLLIN : 0)
45 | (nev & EV_WRITE ? EPOLLOUT : 0);
46
47 epoll_ctl (epoll_fd, mode, fd, &ev);
48} 39}
49 40
50static void 41static void
51epoll_postfork_child (void) 42poll_modify (EV_P_ int fd, int oev, int nev)
52{ 43{
53 int fd; 44 int idx;
54 45
55 epoll_fd = epoll_create (256); 46 if (oev == nev)
56 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC);
57
58 /* re-register interest in fds */
59 for (fd = 0; fd < anfdmax; ++fd)
60 if (anfds [fd].events)//D
61 epoll_modify (fd, EV_NONE, anfds [fd].events);
62}
63
64static struct epoll_event *events;
65static int eventmax;
66
67static void
68epoll_poll (ev_tstamp timeout)
69{
70 int eventcnt = epoll_wait (epoll_fd, events, eventmax, ceil (timeout * 1000.));
71 int i;
72
73 if (eventcnt < 0)
74 return; 47 return;
75 48
76 for (i = 0; i < eventcnt; ++i) 49 array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init);
77 fd_event (
78 events [i].data.fd,
79 (events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
80 | (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0)
81 );
82 50
83 /* if the receive array was full, increase its size */ 51 idx = pollidxs [fd];
84 if (expect_false (eventcnt == eventmax)) 52
53 if (idx < 0) /* need to allocate a new pollfd */
85 { 54 {
86 free (events); 55 pollidxs [fd] = idx = pollcnt++;
87 eventmax = array_roundsize (events, eventmax << 1); 56 array_needsize (struct pollfd, polls, pollmax, pollcnt, );
88 events = malloc (sizeof (struct epoll_event) * eventmax); 57 polls [idx].fd = fd;
58 }
59
60 assert (polls [idx].fd == fd);
61
62 if (nev)
63 polls [idx].events =
64 (nev & EV_READ ? POLLIN : 0)
65 | (nev & EV_WRITE ? POLLOUT : 0);
66 else /* remove pollfd */
67 {
68 pollidxs [fd] = -1;
69
70 if (idx < --pollcnt)
71 {
72 polls [idx] = polls [pollcnt];
73 pollidxs [polls [idx].fd] = idx;
74 }
89 } 75 }
90} 76}
91 77
92static void 78static void
93epoll_init (int flags) 79poll_poll (EV_P_ ev_tstamp timeout)
94{ 80{
95 epoll_fd = epoll_create (256); 81 int i;
82 int res = poll (polls, pollcnt, (int)ceil (timeout * 1000.));
96 83
97 if (epoll_fd < 0) 84 if (res < 0)
98 return; 85 {
86 if (errno == EBADF)
87 fd_ebadf (EV_A);
88 else if (errno == ENOMEM && !syserr_cb)
89 fd_enomem (EV_A);
90 else if (errno != EINTR)
91 syserr ("(libev) poll");
99 92
100 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); 93 return;
94 }
101 95
102 ev_method = EVMETHOD_EPOLL; 96 for (i = 0; i < pollcnt; ++i)
103 method_fudge = 1e-3; /* needed to compensate for epoll returning early */ 97 fd_event (
104 method_modify = epoll_modify; 98 EV_A_
105 method_poll = epoll_poll; 99 polls [i].fd,
106 100 (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
107 eventmax = 64; /* intiial number of events receivable per poll */ 101 | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
108 events = malloc (sizeof (struct epoll_event) * eventmax); 102 );
109} 103}
110 104
105static int
106poll_init (EV_P_ int flags)
107{
108 method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */
109 method_modify = poll_modify;
110 method_poll = poll_poll;
111
112 pollidxs = 0; pollidxmax = 0;
113 polls = 0; pollmax = 0; pollcnt = 0;
114
115 return EVMETHOD_POLL;
116}
117
118static void
119poll_destroy (EV_P)
120{
121 ev_free (pollidxs);
122 ev_free (polls);
123}
124

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines