#include <sys/types.h>
#include <sys/socket.h>
#include <limits.h>
+#include <errno.h>
static size_t num_fds = 0, max_fds = 0, num_closing = 0, num_waiting = 0;
static struct pollfd *pollfds = NULL;
return true;
}
-static void del_conn(struct io_conn *conn)
+void backend_del_conn(struct io_conn *conn)
{
- if (conn->finish)
+ if (conn->finish) {
+ errno = conn->plan.u.close.saved_errno;
conn->finish(conn, conn->finish_arg);
+ }
if (timeout_active(conn))
backend_del_timeout(conn);
free(conn->timeout);
} else
del_fd(&conn->fd);
num_closing--;
+ free_conn(conn);
}
void del_listener(struct io_listener *l)
}
/* It's OK to miss some, as long as we make progress. */
-static void finish_conns(void)
+static bool finish_conns(struct io_conn **ready)
{
unsigned int i;
c = (void *)fds[i];
for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
if (!c->plan.next) {
- del_conn(c);
- free_conn(c);
+ if (doing_debug_on(c) && ready) {
+ *ready = c;
+ return true;
+ }
+ backend_del_conn(c);
i--;
}
}
}
+ return false;
}
void backend_add_timeout(struct io_conn *conn, struct timespec duration)
}
/* This is the main loop. */
-void *io_loop(void)
+void *do_io_loop(struct io_conn **ready)
{
void *ret;
}
if (num_closing) {
- finish_conns();
+ /* If this finishes a debugging con, return now. */
+ if (finish_conns(ready))
+ return NULL;
/* Could have started/finished more. */
continue;
}
if (c->duplex) {
int mask = c->duplex->plan.pollflag;
if (events & mask) {
+ if (doing_debug_on(c->duplex)
+ && ready) {
+ *ready = c->duplex;
+ return NULL;
+ }
io_ready(c->duplex);
events &= ~mask;
/* debug can recurse;
continue;
}
}
+ if (doing_debug_on(c) && ready) {
+ *ready = c;
+ return NULL;
+ }
io_ready(c);
/* debug can recurse; anything can change. */
if (doing_debug())
} else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
r--;
set_current(c);
+ errno = EBADF;
set_plan(c, io_close());
if (c->duplex) {
set_current(c->duplex);
}
}
- while (num_closing)
- finish_conns();
+ while (num_closing && !io_loop_return) {
+ if (finish_conns(ready))
+ return NULL;
+ }
ret = io_loop_return;
io_loop_return = NULL;
io_loop_exit();
return ret;
}
+
+void *io_loop(void)
+{
+ return do_io_loop(NULL);
+}