… | |
… | |
130 | /*****************************************************************************/ |
130 | /*****************************************************************************/ |
131 | |
131 | |
132 | typedef struct |
132 | typedef struct |
133 | { |
133 | { |
134 | struct ev_io *head; |
134 | struct ev_io *head; |
135 | unsigned char wev, rev; /* want, received event set */ |
135 | int events; |
136 | } ANFD; |
136 | } ANFD; |
137 | |
137 | |
138 | static ANFD *anfds; |
138 | static ANFD *anfds; |
139 | static int anfdmax; |
139 | static int anfdmax; |
140 | |
140 | |
141 | static int *fdchanges; |
|
|
142 | static int fdchangemax, fdchangecnt; |
|
|
143 | |
|
|
144 | static void |
141 | static void |
145 | anfds_init (ANFD *base, int count) |
142 | anfds_init (ANFD *base, int count) |
146 | { |
143 | { |
147 | while (count--) |
144 | while (count--) |
148 | { |
145 | { |
149 | base->head = 0; |
146 | base->head = 0; |
150 | base->wev = base->rev = EV_NONE; |
147 | base->events = EV_NONE; |
151 | ++base; |
148 | ++base; |
152 | } |
149 | } |
153 | } |
150 | } |
154 | |
151 | |
155 | typedef struct |
152 | typedef struct |
… | |
… | |
172 | pendings [pendingcnt - 1].events = events; |
169 | pendings [pendingcnt - 1].events = events; |
173 | } |
170 | } |
174 | } |
171 | } |
175 | |
172 | |
176 | static void |
173 | static void |
|
|
174 | queue_events (W *events, int eventcnt, int type) |
|
|
175 | { |
|
|
176 | int i; |
|
|
177 | |
|
|
178 | for (i = 0; i < eventcnt; ++i) |
|
|
179 | event (events [i], type); |
|
|
180 | } |
|
|
181 | |
|
|
182 | static void |
177 | fd_event (int fd, int events) |
183 | fd_event (int fd, int events) |
178 | { |
184 | { |
179 | ANFD *anfd = anfds + fd; |
185 | ANFD *anfd = anfds + fd; |
180 | struct ev_io *w; |
186 | struct ev_io *w; |
181 | |
187 | |
… | |
… | |
186 | if (ev) |
192 | if (ev) |
187 | event ((W)w, ev); |
193 | event ((W)w, ev); |
188 | } |
194 | } |
189 | } |
195 | } |
190 | |
196 | |
|
|
197 | /*****************************************************************************/ |
|
|
198 | |
|
|
199 | static int *fdchanges; |
|
|
200 | static int fdchangemax, fdchangecnt; |
|
|
201 | |
191 | static void |
202 | static void |
192 | queue_events (W *events, int eventcnt, int type) |
203 | fd_reify (void) |
193 | { |
204 | { |
194 | int i; |
205 | int i; |
195 | |
206 | |
196 | for (i = 0; i < eventcnt; ++i) |
207 | for (i = 0; i < fdchangecnt; ++i) |
197 | event (events [i], type); |
208 | { |
|
|
209 | int fd = fdchanges [i]; |
|
|
210 | ANFD *anfd = anfds + fd; |
|
|
211 | struct ev_io *w; |
|
|
212 | |
|
|
213 | int events = 0; |
|
|
214 | |
|
|
215 | for (w = anfd->head; w; w = w->next) |
|
|
216 | events |= w->events; |
|
|
217 | |
|
|
218 | anfd->events &= ~EV_REIFY; |
|
|
219 | |
|
|
220 | if (anfd->events != events) |
|
|
221 | { |
|
|
222 | method_modify (fd, anfd->events, events); |
|
|
223 | anfd->events = events; |
|
|
224 | } |
|
|
225 | } |
|
|
226 | |
|
|
227 | fdchangecnt = 0; |
|
|
228 | } |
|
|
229 | |
|
|
230 | static void |
|
|
231 | fd_change (int fd) |
|
|
232 | { |
|
|
233 | if (anfds [fd].events & EV_REIFY) |
|
|
234 | return; |
|
|
235 | |
|
|
236 | anfds [fd].events |= EV_REIFY; |
|
|
237 | |
|
|
238 | ++fdchangecnt; |
|
|
239 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
240 | fdchanges [fdchangecnt - 1] = fd; |
198 | } |
241 | } |
199 | |
242 | |
200 | /* called on EBADF to verify fds */ |
243 | /* called on EBADF to verify fds */ |
201 | static void |
244 | static void |
202 | fd_recheck (void) |
245 | fd_recheck (void) |
203 | { |
246 | { |
204 | int fd; |
247 | int fd; |
205 | |
248 | |
206 | for (fd = 0; fd < anfdmax; ++fd) |
249 | for (fd = 0; fd < anfdmax; ++fd) |
207 | if (anfds [fd].wev) |
250 | if (anfds [fd].events) |
208 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
209 | while (anfds [fd].head) |
252 | while (anfds [fd].head) |
210 | { |
253 | { |
211 | event ((W)anfds [fd].head, EV_ERROR); |
254 | event ((W)anfds [fd].head, EV_ERROR); |
212 | evio_stop (anfds [fd].head); |
255 | evio_stop (anfds [fd].head); |
… | |
… | |
459 | } |
502 | } |
460 | |
503 | |
461 | /*****************************************************************************/ |
504 | /*****************************************************************************/ |
462 | |
505 | |
463 | static void |
506 | static void |
464 | fd_reify (void) |
|
|
465 | { |
|
|
466 | int i; |
|
|
467 | |
|
|
468 | for (i = 0; i < fdchangecnt; ++i) |
|
|
469 | { |
|
|
470 | int fd = fdchanges [i]; |
|
|
471 | ANFD *anfd = anfds + fd; |
|
|
472 | struct ev_io *w; |
|
|
473 | |
|
|
474 | int wev = 0; |
|
|
475 | |
|
|
476 | for (w = anfd->head; w; w = w->next) |
|
|
477 | wev |= w->events; |
|
|
478 | |
|
|
479 | if (anfd->wev != wev) |
|
|
480 | { |
|
|
481 | method_modify (fd, anfd->wev, wev); |
|
|
482 | anfd->wev = wev; |
|
|
483 | } |
|
|
484 | } |
|
|
485 | |
|
|
486 | fdchangecnt = 0; |
|
|
487 | } |
|
|
488 | |
|
|
489 | static void |
|
|
490 | call_pending (void) |
507 | call_pending (void) |
491 | { |
508 | { |
492 | while (pendingcnt) |
509 | while (pendingcnt) |
493 | { |
510 | { |
494 | ANPENDING *p = pendings + --pendingcnt; |
511 | ANPENDING *p = pendings + --pendingcnt; |
… | |
… | |
737 | |
754 | |
738 | ev_start ((W)w, 1); |
755 | ev_start ((W)w, 1); |
739 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
740 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
741 | |
758 | |
742 | ++fdchangecnt; |
759 | fd_change (fd); |
743 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
744 | fdchanges [fdchangecnt - 1] = fd; |
|
|
745 | |
|
|
746 | if (w->fd == 9) |
|
|
747 | printf ("start %p:%x\n", w, w->events);//D |
|
|
748 | } |
760 | } |
749 | |
761 | |
750 | void |
762 | void |
751 | evio_stop (struct ev_io *w) |
763 | evio_stop (struct ev_io *w) |
752 | { |
764 | { |
753 | if (w->fd == 9) |
|
|
754 | printf ("stop %p:%x\n", w, w->events);//D |
|
|
755 | ev_clear ((W)w); |
765 | ev_clear ((W)w); |
756 | if (!ev_is_active (w)) |
766 | if (!ev_is_active (w)) |
757 | return; |
767 | return; |
758 | |
768 | |
759 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
760 | ev_stop ((W)w); |
770 | ev_stop ((W)w); |
761 | |
771 | |
762 | ++fdchangecnt; |
772 | fd_change (w->fd); |
763 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
764 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
765 | } |
773 | } |
766 | |
774 | |
767 | void |
775 | void |
768 | evtimer_start (struct ev_timer *w) |
776 | evtimer_start (struct ev_timer *w) |
769 | { |
777 | { |