1 | /* |
1 | /* |
2 | * libev epoll fd activity backend |
2 | * libev poll fd activity backend |
3 | * |
3 | * |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
… | |
… | |
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; |
34 | void inline_size |
35 | static int pollmax, pollcnt; |
|
|
36 | static int *pollidxs; /* maps fds into structure indices */ |
|
|
37 | static int pollidxmax; |
|
|
38 | |
|
|
39 | 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; |
|
|
45 | |
|
|
46 | if (oev == nev) |
|
|
47 | return; |
|
|
48 | |
50 | array_needsize (pollidxs, pollidxmax, fd + 1, pollidx_init); |
49 | array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init); |
51 | |
50 | |
52 | idx = pollidxs [fd]; |
51 | idx = pollidxs [fd]; |
53 | |
52 | |
54 | if (idx < 0) /* need to allocate a new pollfd */ |
53 | if (idx < 0) /* need to allocate a new pollfd */ |
55 | { |
54 | { |
56 | idx = pollcnt++; |
55 | pollidxs [fd] = idx = pollcnt++; |
57 | array_needsize (polls, pollmax, pollcnt, ); |
56 | array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2); |
58 | polls [idx].fd = fd; |
57 | polls [idx].fd = fd; |
59 | } |
58 | } |
|
|
59 | |
|
|
60 | assert (polls [idx].fd == fd); |
60 | |
61 | |
61 | if (nev) |
62 | if (nev) |
62 | polls [idx].events = |
63 | polls [idx].events = |
63 | (nev & EV_READ ? POLLIN : 0) |
64 | (nev & EV_READ ? POLLIN : 0) |
64 | | (nev & EV_WRITE ? POLLOUT : 0); |
65 | | (nev & EV_WRITE ? POLLOUT : 0); |
65 | else /* remove pollfd */ |
66 | else /* remove pollfd */ |
66 | { |
67 | { |
67 | if (idx < pollcnt--) |
68 | pollidxs [fd] = -1; |
|
|
69 | |
|
|
70 | if (expect_true (idx < --pollcnt)) |
68 | { |
71 | { |
69 | pollidxs [fd] = -1; |
|
|
70 | polls [idx] = polls [pollcnt]; |
72 | polls [idx] = polls [pollcnt]; |
71 | pollidxs [polls [idx].fd] = idx; |
73 | pollidxs [polls [idx].fd] = idx; |
72 | } |
74 | } |
73 | } |
75 | } |
74 | } |
76 | } |
75 | |
77 | |
76 | static void |
78 | static void |
77 | poll_poll (ev_tstamp timeout) |
79 | poll_poll (EV_P_ ev_tstamp timeout) |
78 | { |
80 | { |
|
|
81 | int i; |
79 | int res = poll (polls, pollcnt, ceil (timeout * 1000.)); |
82 | int res = poll (polls, pollcnt, (int)ceil (timeout * 1000.)); |
80 | |
83 | |
81 | if (res > 0) |
84 | if (expect_false (res < 0)) |
82 | { |
|
|
83 | int i; |
|
|
84 | |
|
|
85 | 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 | { |
85 | { |
94 | if (errno == EBADF) |
86 | if (errno == EBADF) |
95 | fd_ebadf (); |
87 | fd_ebadf (EV_A); |
96 | else if (errno == ENOMEM) |
88 | else if (errno == ENOMEM && !syserr_cb) |
97 | fd_enomem (); |
89 | fd_enomem (EV_A); |
|
|
90 | else if (errno != EINTR) |
|
|
91 | syserr ("(libev) poll"); |
|
|
92 | |
|
|
93 | return; |
98 | } |
94 | } |
|
|
95 | |
|
|
96 | for (i = 0; i < pollcnt; ++i) |
|
|
97 | if (expect_false (polls [i].revents & POLLNVAL)) |
|
|
98 | fd_kill (EV_A_ polls [i].fd); |
|
|
99 | else |
|
|
100 | fd_event ( |
|
|
101 | EV_A_ |
|
|
102 | polls [i].fd, |
|
|
103 | (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
|
|
104 | | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
|
|
105 | ); |
99 | } |
106 | } |
100 | |
107 | |
101 | static void |
108 | int inline_size |
102 | poll_init (int flags) |
109 | poll_init (EV_P_ int flags) |
103 | { |
110 | { |
104 | ev_method = EVMETHOD_POLL; |
|
|
105 | method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */ |
111 | backend_fudge = 0; /* needed to compensate for select returning early, very conservative */ |
106 | method_modify = poll_modify; |
112 | backend_modify = poll_modify; |
107 | method_poll = poll_poll; |
113 | backend_poll = poll_poll; |
|
|
114 | |
|
|
115 | pollidxs = 0; pollidxmax = 0; |
|
|
116 | polls = 0; pollmax = 0; pollcnt = 0; |
|
|
117 | |
|
|
118 | return EVBACKEND_POLL; |
108 | } |
119 | } |
|
|
120 | |
|
|
121 | void inline_size |
|
|
122 | poll_destroy (EV_P) |
|
|
123 | { |
|
|
124 | ev_free (pollidxs); |
|
|
125 | ev_free (polls); |
|
|
126 | } |
|
|
127 | |