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