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 |
|
|
static void |
35 |
root |
1.2 |
pollidx_init (int *base, int count) |
36 |
root |
1.1 |
{ |
37 |
root |
1.2 |
while (count--) |
38 |
|
|
*base++ = -1; |
39 |
root |
1.1 |
} |
40 |
|
|
|
41 |
|
|
static void |
42 |
root |
1.3 |
poll_modify (EV_P_ int fd, int oev, int nev) |
43 |
root |
1.1 |
{ |
44 |
root |
1.2 |
int idx; |
45 |
root |
1.6 |
|
46 |
|
|
if (oev == nev) |
47 |
|
|
return; |
48 |
|
|
|
49 |
root |
1.9 |
array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init); |
50 |
root |
1.1 |
|
51 |
root |
1.2 |
idx = pollidxs [fd]; |
52 |
|
|
|
53 |
|
|
if (idx < 0) /* need to allocate a new pollfd */ |
54 |
|
|
{ |
55 |
|
|
idx = pollcnt++; |
56 |
root |
1.9 |
array_needsize (struct pollfd, polls, pollmax, pollcnt, ); |
57 |
root |
1.2 |
polls [idx].fd = fd; |
58 |
|
|
} |
59 |
root |
1.1 |
|
60 |
root |
1.2 |
if (nev) |
61 |
|
|
polls [idx].events = |
62 |
|
|
(nev & EV_READ ? POLLIN : 0) |
63 |
|
|
| (nev & EV_WRITE ? POLLOUT : 0); |
64 |
|
|
else /* remove pollfd */ |
65 |
|
|
{ |
66 |
|
|
if (idx < pollcnt--) |
67 |
|
|
{ |
68 |
|
|
pollidxs [fd] = -1; |
69 |
|
|
polls [idx] = polls [pollcnt]; |
70 |
|
|
pollidxs [polls [idx].fd] = idx; |
71 |
|
|
} |
72 |
|
|
} |
73 |
root |
1.1 |
} |
74 |
|
|
|
75 |
|
|
static void |
76 |
root |
1.3 |
poll_poll (EV_P_ ev_tstamp timeout) |
77 |
root |
1.1 |
{ |
78 |
root |
1.7 |
int i; |
79 |
root |
1.2 |
int res = poll (polls, pollcnt, ceil (timeout * 1000.)); |
80 |
root |
1.1 |
|
81 |
root |
1.7 |
if (res < 0) |
82 |
root |
1.1 |
{ |
83 |
root |
1.2 |
if (errno == EBADF) |
84 |
root |
1.3 |
fd_ebadf (EV_A); |
85 |
root |
1.7 |
else if (errno == ENOMEM && !syserr_cb) |
86 |
root |
1.3 |
fd_enomem (EV_A); |
87 |
root |
1.7 |
else if (errno != EINTR) |
88 |
root |
1.8 |
syserr ("(libev) poll"); |
89 |
root |
1.7 |
|
90 |
|
|
return; |
91 |
root |
1.1 |
} |
92 |
root |
1.7 |
|
93 |
|
|
for (i = 0; i < pollcnt; ++i) |
94 |
|
|
fd_event ( |
95 |
|
|
EV_A_ |
96 |
|
|
polls [i].fd, |
97 |
|
|
(polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
98 |
|
|
| (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
99 |
|
|
); |
100 |
root |
1.1 |
} |
101 |
|
|
|
102 |
root |
1.3 |
static int |
103 |
|
|
poll_init (EV_P_ int flags) |
104 |
root |
1.1 |
{ |
105 |
root |
1.2 |
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.3 |
|
109 |
root |
1.4 |
pollidxs = 0; pollidxmax = 0; |
110 |
root |
1.5 |
polls = 0; pollmax = 0; pollcnt = 0; |
111 |
root |
1.4 |
|
112 |
root |
1.3 |
return EVMETHOD_POLL; |
113 |
root |
1.1 |
} |
114 |
root |
1.4 |
|
115 |
|
|
static void |
116 |
|
|
poll_destroy (EV_P) |
117 |
|
|
{ |
118 |
root |
1.7 |
ev_free (pollidxs); |
119 |
|
|
ev_free (polls); |
120 |
root |
1.4 |
} |
121 |
root |
1.9 |
|