+ /*
+ * Add all currently open fds to the select sets, and store
+ * them in fdlist as well.
+ */
+ fdcount = 0;
+ for (fd = first_fd(&fdstate, &rwx); fd >= 0;
+ fd = next_fd(&fdstate, &rwx)) {
+ fdlist[fdcount++] = fd;
+ if (rwx & 1)
+ FD_SET_MAX(fd, maxfd, rset);
+ if (rwx & 2)
+ FD_SET_MAX(fd, maxfd, wset);
+ if (rwx & 4)
+ FD_SET_MAX(fd, maxfd, xset);
+ }
+
+ if (include_stdin)
+ FD_SET_MAX(0, maxfd, rset);
+
+ if (toplevel_callback_pending()) {
+ struct timeval tv;
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ ret = select(maxfd, &rset, &wset, &xset, &tv);
+ if (ret == 0)
+ run_toplevel_callbacks();
+ } else if (run_timers(now, &next)) {
+ do {
+ unsigned long then;
+ long ticks;
+ struct timeval tv;
+
+ then = now;
+ now = GETTICKCOUNT();
+ if (now - then > next - then)
+ ticks = 0;
+ else
+ ticks = next - now;
+ tv.tv_sec = ticks / 1000;
+ tv.tv_usec = ticks % 1000 * 1000;
+ ret = select(maxfd, &rset, &wset, &xset, &tv);
+ if (ret == 0)
+ now = next;
+ else
+ now = GETTICKCOUNT();
+ } while (ret < 0 && errno == EINTR);
+ } else {
+ do {
+ ret = select(maxfd, &rset, &wset, &xset, NULL);
+ } while (ret < 0 && errno == EINTR);
+ }
+ } while (ret == 0);