1 |
root |
1.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 |
root |
1.2 |
#include <poll.h> |
33 |
root |
1.1 |
|
34 |
root |
1.2 |
static struct pollfd *polls; |
35 |
|
|
static int pollmax, pollcnt; |
36 |
|
|
static int *pollidxs; /* maps fds into structure indices */ |
37 |
|
|
static int pollidxmax; |
38 |
root |
1.1 |
|
39 |
|
|
static void |
40 |
root |
1.2 |
pollidx_init (int *base, int count) |
41 |
root |
1.1 |
{ |
42 |
root |
1.2 |
while (count--) |
43 |
|
|
*base++ = -1; |
44 |
root |
1.1 |
} |
45 |
|
|
|
46 |
|
|
static void |
47 |
root |
1.2 |
poll_modify (int fd, int oev, int nev) |
48 |
root |
1.1 |
{ |
49 |
root |
1.2 |
int idx; |
50 |
|
|
array_needsize (pollidxs, pollidxmax, fd + 1, pollidx_init); |
51 |
root |
1.1 |
|
52 |
root |
1.2 |
idx = pollidxs [fd]; |
53 |
|
|
|
54 |
|
|
if (idx < 0) /* need to allocate a new pollfd */ |
55 |
|
|
{ |
56 |
|
|
idx = pollcnt++; |
57 |
|
|
array_needsize (polls, pollmax, pollcnt, ); |
58 |
|
|
polls [idx].fd = fd; |
59 |
|
|
} |
60 |
root |
1.1 |
|
61 |
root |
1.2 |
if (nev) |
62 |
|
|
polls [idx].events = |
63 |
|
|
(nev & EV_READ ? POLLIN : 0) |
64 |
|
|
| (nev & EV_WRITE ? POLLOUT : 0); |
65 |
|
|
else /* remove pollfd */ |
66 |
|
|
{ |
67 |
|
|
if (idx < pollcnt--) |
68 |
|
|
{ |
69 |
|
|
pollidxs [fd] = -1; |
70 |
|
|
polls [idx] = polls [pollcnt]; |
71 |
|
|
pollidxs [polls [idx].fd] = idx; |
72 |
|
|
} |
73 |
|
|
} |
74 |
root |
1.1 |
} |
75 |
|
|
|
76 |
|
|
static void |
77 |
root |
1.2 |
poll_poll (ev_tstamp timeout) |
78 |
root |
1.1 |
{ |
79 |
root |
1.2 |
int res = poll (polls, pollcnt, ceil (timeout * 1000.)); |
80 |
root |
1.1 |
|
81 |
root |
1.2 |
if (res > 0) |
82 |
|
|
{ |
83 |
|
|
int i; |
84 |
root |
1.1 |
|
85 |
root |
1.2 |
for (i = 0; i < pollcnt; ++i) |
86 |
|
|
fd_event ( |
87 |
|
|
polls [i].fd, |
88 |
|
|
(polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
89 |
|
|
| (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
90 |
|
|
); |
91 |
|
|
} |
92 |
|
|
else if (res < 0) |
93 |
root |
1.1 |
{ |
94 |
root |
1.2 |
if (errno == EBADF) |
95 |
|
|
fd_ebadf (); |
96 |
|
|
else if (errno == ENOMEM) |
97 |
|
|
fd_enomem (); |
98 |
root |
1.1 |
} |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
static void |
102 |
root |
1.2 |
poll_init (int flags) |
103 |
root |
1.1 |
{ |
104 |
root |
1.2 |
ev_method = EVMETHOD_POLL; |
105 |
|
|
method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */ |
106 |
|
|
method_modify = poll_modify; |
107 |
|
|
method_poll = poll_poll; |
108 |
root |
1.1 |
} |