… | |
… | |
40 | |
40 | |
41 | static void |
41 | static void |
42 | poll_modify (EV_P_ int fd, int oev, int nev) |
42 | poll_modify (EV_P_ int fd, int oev, int nev) |
43 | { |
43 | { |
44 | int idx; |
44 | int idx; |
|
|
45 | |
|
|
46 | if (oev == nev) |
|
|
47 | return; |
|
|
48 | |
45 | array_needsize (pollidxs, pollidxmax, fd + 1, pollidx_init); |
49 | array_needsize (int, pollidxs, pollidxmax, fd + 1, pollidx_init); |
46 | |
50 | |
47 | idx = pollidxs [fd]; |
51 | idx = pollidxs [fd]; |
48 | |
52 | |
49 | if (idx < 0) /* need to allocate a new pollfd */ |
53 | if (idx < 0) /* need to allocate a new pollfd */ |
50 | { |
54 | { |
51 | idx = pollcnt++; |
55 | pollidxs [fd] = idx = pollcnt++; |
52 | array_needsize (polls, pollmax, pollcnt, ); |
56 | array_needsize (struct pollfd, polls, pollmax, pollcnt, ); |
53 | polls [idx].fd = fd; |
57 | polls [idx].fd = fd; |
54 | } |
58 | } |
|
|
59 | |
|
|
60 | assert (polls [idx].fd == fd); |
55 | |
61 | |
56 | if (nev) |
62 | if (nev) |
57 | polls [idx].events = |
63 | polls [idx].events = |
58 | (nev & EV_READ ? POLLIN : 0) |
64 | (nev & EV_READ ? POLLIN : 0) |
59 | | (nev & EV_WRITE ? POLLOUT : 0); |
65 | | (nev & EV_WRITE ? POLLOUT : 0); |
60 | else /* remove pollfd */ |
66 | else /* remove pollfd */ |
61 | { |
67 | { |
|
|
68 | pollidxs [fd] = -1; |
|
|
69 | |
62 | if (idx < pollcnt--) |
70 | if (idx < --pollcnt) |
63 | { |
71 | { |
64 | pollidxs [fd] = -1; |
|
|
65 | polls [idx] = polls [pollcnt]; |
72 | polls [idx] = polls [pollcnt]; |
66 | pollidxs [polls [idx].fd] = idx; |
73 | pollidxs [polls [idx].fd] = idx; |
67 | } |
74 | } |
68 | } |
75 | } |
69 | } |
76 | } |
70 | |
77 | |
71 | static void |
78 | static void |
72 | poll_poll (EV_P_ ev_tstamp timeout) |
79 | poll_poll (EV_P_ ev_tstamp timeout) |
73 | { |
80 | { |
|
|
81 | int i; |
74 | int res = poll (polls, pollcnt, ceil (timeout * 1000.)); |
82 | int res = poll (polls, pollcnt, ceil (timeout * 1000.)); |
75 | |
83 | |
76 | if (res > 0) |
84 | if (res < 0) |
77 | { |
|
|
78 | int i; |
|
|
79 | |
|
|
80 | for (i = 0; i < pollcnt; ++i) |
|
|
81 | fd_event ( |
|
|
82 | EV_A_ |
|
|
83 | polls [i].fd, |
|
|
84 | (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
|
|
85 | | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
|
|
86 | ); |
|
|
87 | } |
|
|
88 | else if (res < 0) |
|
|
89 | { |
85 | { |
90 | if (errno == EBADF) |
86 | if (errno == EBADF) |
91 | fd_ebadf (EV_A); |
87 | fd_ebadf (EV_A); |
92 | else if (errno == ENOMEM) |
88 | else if (errno == ENOMEM && !syserr_cb) |
93 | fd_enomem (EV_A); |
89 | fd_enomem (EV_A); |
|
|
90 | else if (errno != EINTR) |
|
|
91 | syserr ("(libev) poll"); |
|
|
92 | |
|
|
93 | return; |
94 | } |
94 | } |
|
|
95 | |
|
|
96 | for (i = 0; i < pollcnt; ++i) |
|
|
97 | fd_event ( |
|
|
98 | EV_A_ |
|
|
99 | polls [i].fd, |
|
|
100 | (polls [i].revents & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
|
|
101 | | (polls [i].revents & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
|
|
102 | ); |
95 | } |
103 | } |
96 | |
104 | |
97 | static int |
105 | static int |
98 | poll_init (EV_P_ int flags) |
106 | poll_init (EV_P_ int flags) |
99 | { |
107 | { |
100 | method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */ |
108 | method_fudge = 1e-3; /* needed to compensate for select returning early, very conservative */ |
101 | method_modify = poll_modify; |
109 | method_modify = poll_modify; |
102 | method_poll = poll_poll; |
110 | method_poll = poll_poll; |
103 | |
111 | |
104 | pollidxs = 0; pollidxmax = 0; |
112 | pollidxs = 0; pollidxmax = 0; |
105 | polls = 0; pollsmax = 0; pollscnt = 0; |
113 | polls = 0; pollmax = 0; pollcnt = 0; |
106 | |
114 | |
107 | return EVMETHOD_POLL; |
115 | return EVMETHOD_POLL; |
108 | } |
116 | } |
109 | |
117 | |
110 | static void |
118 | static void |
111 | poll_destroy (EV_P) |
119 | poll_destroy (EV_P) |
112 | { |
120 | { |
113 | free (pollidxs); |
121 | ev_free (pollidxs); |
114 | free (polls); |
122 | ev_free (polls); |
115 | } |
123 | } |
|
|
124 | |