2 * libev epoll fd activity backend
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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.
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.
35 pollidx_init (int *base, int count)
42 poll_modify (EV_P_ int fd, int oev, int nev)
49 array_needsize (pollidxs, pollidxmax, fd + 1, pollidx_init);
53 if (idx < 0) /* need to allocate a new pollfd */
56 array_needsize (polls, pollmax, pollcnt, );
62 (nev & EV_READ ? POLLIN : 0)
63 | (nev & EV_WRITE ? POLLOUT : 0);
64 else /* remove pollfd */
69 polls [idx] = polls [pollcnt];
70 pollidxs [polls [idx].fd] = idx;
76 poll_poll (EV_P_ ev_tstamp timeout)
79 int res = poll (polls, pollcnt, ceil (timeout * 1000.));
85 else if (errno == ENOMEM && !syserr_cb)
87 else if (errno != EINTR)
88 syserr ("(libev) poll");
93 for (i = 0; i < pollcnt; ++i)
97 (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
98 | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
103 poll_init (EV_P_ int flags)
105 method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */
106 method_modify = poll_modify;
107 method_poll = poll_poll;
109 pollidxs = 0; pollidxmax = 0;
110 polls = 0; pollmax = 0; pollcnt = 0;
112 return EVMETHOD_POLL;