- for (i = 0; i < eventcnt; ++i)
- event (events [i], type);
+ for (i = 0; i < fdchangecnt; ++i)
+ {
+ int fd = fdchanges [i];
+ ANFD *anfd = anfds + fd;
+ struct ev_io *w;
+
+ int events = 0;
+
+ for (w = anfd->head; w; w = w->next)
+ events |= w->events;
+
+ anfd->reify = 0;
+
+ if (anfd->events != events)
+ {
+ method_modify (fd, anfd->events, events);
+ anfd->events = events;
+ }
+ }
+
+ fdchangecnt = 0;
+}
+
+static void
+fd_change (int fd)
+{
+ if (anfds [fd].reify || fdchangecnt < 0)
+ return;
+
+ anfds [fd].reify = 1;
+
+ ++fdchangecnt;
+ array_needsize (fdchanges, fdchangemax, fdchangecnt, );
+ fdchanges [fdchangecnt - 1] = fd;
+}
+
+static void
+fd_kill (int fd)
+{
+ struct ev_io *w;
+
+ printf ("killing fd %d\n", fd);//D
+ while ((w = anfds [fd].head))
+ {
+ ev_io_stop (w);
+ event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
+ }
+}
+
+/* called on EBADF to verify fds */
+static void
+fd_ebadf (void)
+{
+ int fd;
+
+ for (fd = 0; fd < anfdmax; ++fd)
+ if (anfds [fd].events)
+ if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
+ fd_kill (fd);
+}
+
+/* called on ENOMEM in select/poll to kill some fds and retry */
+static void
+fd_enomem (void)
+{
+ int fd = anfdmax;
+
+ while (fd--)
+ if (anfds [fd].events)
+ {
+ close (fd);
+ fd_kill (fd);
+ return;
+ }