| 1 |
/* |
| 2 |
* libev epoll fd activity backend |
| 3 |
* |
| 4 |
* 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 |
#include <poll.h> |
| 33 |
|
| 34 |
static void |
| 35 |
pollidx_init (int *base, int count) |
| 36 |
{ |
| 37 |
while (count--) |
| 38 |
*base++ = -1; |
| 39 |
} |
| 40 |
|
| 41 |
static void |
| 42 |
poll_modify (EV_P_ int fd, int oev, int nev) |
| 43 |
{ |
| 44 |
int idx; |
| 45 |
|
| 46 |
if (oev == nev) |
| 47 |
return; |
| 48 |
|
| 49 |
array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init); |
| 50 |
|
| 51 |
idx = pollidxs [fd]; |
| 52 |
|
| 53 |
if (idx < 0) /* need to allocate a new pollfd */ |
| 54 |
{ |
| 55 |
pollidxs [fd] = idx = pollcnt++; |
| 56 |
array_needsize (struct pollfd, polls, pollmax, pollcnt, ); |
| 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 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
poll_poll (EV_P_ ev_tstamp timeout) |
| 80 |
{ |
| 81 |
int i; |
| 82 |
int res = poll (polls, pollcnt, ceil (timeout * 1000.)); |
| 83 |
|
| 84 |
if (res < 0) |
| 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"); |
| 92 |
|
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
for (i = 0; i < pollcnt; ++i) |
| 97 |
fd_event ( |
| 98 |
EV_A_ |
| 99 |
polls [i].fd, |
| 100 |
(polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
| 101 |
| (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
static int |
| 106 |
poll_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 |
|
| 118 |
static void |
| 119 |
poll_destroy (EV_P) |
| 120 |
{ |
| 121 |
ev_free (pollidxs); |
| 122 |
ev_free (polls); |
| 123 |
} |
| 124 |
|