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 void |
34 | void inline_size |
35 | pollidx_init (int *base, int count) |
35 | pollidx_init (int *base, int count) |
36 | { |
36 | { |
37 | while (count--) |
37 | while (count--) |
38 | *base++ = -1; |
38 | *base++ = -1; |
39 | } |
39 | } |
… | |
… | |
51 | idx = pollidxs [fd]; |
51 | idx = pollidxs [fd]; |
52 | |
52 | |
53 | if (idx < 0) /* need to allocate a new pollfd */ |
53 | if (idx < 0) /* need to allocate a new pollfd */ |
54 | { |
54 | { |
55 | pollidxs [fd] = idx = pollcnt++; |
55 | pollidxs [fd] = idx = pollcnt++; |
56 | array_needsize (struct pollfd, polls, pollmax, pollcnt, ); |
56 | array_needsize (struct pollfd, polls, pollmax, pollcnt, EMPTY2); |
57 | polls [idx].fd = fd; |
57 | polls [idx].fd = fd; |
58 | } |
58 | } |
59 | |
59 | |
60 | assert (polls [idx].fd == fd); |
60 | assert (polls [idx].fd == fd); |
61 | |
61 | |
… | |
… | |
65 | | (nev & EV_WRITE ? POLLOUT : 0); |
65 | | (nev & EV_WRITE ? POLLOUT : 0); |
66 | else /* remove pollfd */ |
66 | else /* remove pollfd */ |
67 | { |
67 | { |
68 | pollidxs [fd] = -1; |
68 | pollidxs [fd] = -1; |
69 | |
69 | |
70 | if (idx < --pollcnt) |
70 | if (expect_true (idx < --pollcnt)) |
71 | { |
71 | { |
72 | polls [idx] = polls [pollcnt]; |
72 | polls [idx] = polls [pollcnt]; |
73 | pollidxs [polls [idx].fd] = idx; |
73 | pollidxs [polls [idx].fd] = idx; |
74 | } |
74 | } |
75 | } |
75 | } |
… | |
… | |
79 | poll_poll (EV_P_ ev_tstamp timeout) |
79 | poll_poll (EV_P_ ev_tstamp timeout) |
80 | { |
80 | { |
81 | int i; |
81 | int i; |
82 | int res = poll (polls, pollcnt, (int)ceil (timeout * 1000.)); |
82 | int res = poll (polls, pollcnt, (int)ceil (timeout * 1000.)); |
83 | |
83 | |
84 | if (res < 0) |
84 | if (expect_false (res < 0)) |
85 | { |
85 | { |
86 | if (errno == EBADF) |
86 | if (errno == EBADF) |
87 | fd_ebadf (EV_A); |
87 | fd_ebadf (EV_A); |
88 | else if (errno == ENOMEM && !syserr_cb) |
88 | else if (errno == ENOMEM && !syserr_cb) |
89 | fd_enomem (EV_A); |
89 | fd_enomem (EV_A); |
… | |
… | |
92 | |
92 | |
93 | return; |
93 | return; |
94 | } |
94 | } |
95 | |
95 | |
96 | for (i = 0; i < pollcnt; ++i) |
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 |
97 | fd_event ( |
100 | fd_event ( |
98 | EV_A_ |
101 | EV_A_ |
99 | polls [i].fd, |
102 | polls [i].fd, |
100 | (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
103 | (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
101 | | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
104 | | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
102 | ); |
105 | ); |
103 | } |
106 | } |
104 | |
107 | |
105 | static int |
108 | int inline_size |
106 | poll_init (EV_P_ int flags) |
109 | poll_init (EV_P_ int flags) |
107 | { |
110 | { |
108 | method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */ |
111 | backend_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */ |
109 | method_modify = poll_modify; |
112 | backend_modify = poll_modify; |
110 | method_poll = poll_poll; |
113 | backend_poll = poll_poll; |
111 | |
114 | |
112 | pollidxs = 0; pollidxmax = 0; |
115 | pollidxs = 0; pollidxmax = 0; |
113 | polls = 0; pollmax = 0; pollcnt = 0; |
116 | polls = 0; pollmax = 0; pollcnt = 0; |
114 | |
117 | |
115 | return EVMETHOD_POLL; |
118 | return EVBACKEND_POLL; |
116 | } |
119 | } |
117 | |
120 | |
118 | static void |
121 | void inline_size |
119 | poll_destroy (EV_P) |
122 | poll_destroy (EV_P) |
120 | { |
123 | { |
121 | ev_free (pollidxs); |
124 | ev_free (pollidxs); |
122 | ev_free (polls); |
125 | ev_free (polls); |
123 | } |
126 | } |