Use a NULL next pointer instead to indicate a closing connection.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
bool io_timeout_(struct io_conn *conn, struct timespec ts,
struct io_plan (*cb)(struct io_conn *, void *), void *arg)
{
+ assert(cb);
+
if (!conn->timeout) {
conn->timeout = malloc(sizeof(*conn->timeout));
if (!conn->timeout)
{
struct io_plan plan;
+ assert(cb);
plan.u.write.buf = data;
plan.u.write.len = len;
plan.io = do_write;
plan.next = cb;
plan.next_arg = arg;
plan.pollflag = POLLOUT;
- plan.state = IO_IO;
return plan;
}
{
struct io_plan plan;
+ assert(cb);
plan.u.read.buf = data;
plan.u.read.len = len;
plan.io = do_read;
plan.next = cb;
plan.next_arg = arg;
plan.pollflag = POLLIN;
- plan.state = IO_IO;
return plan;
}
{
struct io_plan plan;
+ assert(cb);
plan.u.readpart.buf = data;
plan.u.readpart.lenp = len;
plan.io = do_read_partial;
plan.next = cb;
plan.next_arg = arg;
plan.pollflag = POLLIN;
- plan.state = IO_IO;
return plan;
}
{
struct io_plan plan;
+ assert(cb);
plan.u.writepart.buf = data;
plan.u.writepart.lenp = len;
plan.io = do_write_partial;
plan.next = cb;
plan.next_arg = arg;
plan.pollflag = POLLOUT;
- plan.state = IO_IO;
return plan;
}
struct io_plan plan;
plan.pollflag = 0;
- plan.state = IO_IO;
plan.io = NULL;
+ /* Never called (overridded by io_wake), but NULL means closing */
+ plan.next = io_close;
return plan;
}
void io_wake(struct io_conn *conn, struct io_plan plan)
{
- /* It might have finished, but we haven't called its finish() yet. */
- if (conn->plan.state == IO_FINISHED)
+ /* It might be closing, but we haven't called its finish() yet. */
+ if (!conn->plan.next)
return;
+ /* It was idle, right? */
assert(!conn->plan.io);
conn->plan = plan;
backend_wakeup(conn);
struct io_plan do_ready(struct io_conn *conn)
{
- assert(conn->plan.state == IO_IO);
switch (conn->plan.io(conn)) {
case RESULT_CLOSE:
return io_close(conn, NULL);
{
struct io_plan plan;
- plan.state = IO_FINISHED;
plan.pollflag = 0;
+ /* This means we're closing. */
+ plan.next = NULL;
return plan;
}
/* Exit the loop, returning this (non-NULL) arg. */
struct io_plan io_break(void *ret, struct io_plan plan)
{
+ assert(ret);
io_loop_return = ret;
return plan;
RESULT_CLOSE
};
-enum io_state {
- IO_IO,
- IO_FINISHED
-};
-
/**
* struct io_plan - returned from a setup function.
*
*/
struct io_plan {
int pollflag;
- enum io_state state;
+ /* Only NULL if idle. */
enum io_result (*io)(struct io_conn *conn);
+ /* Only NULL if closing. */
struct io_plan (*next)(struct io_conn *, void *arg);
void *next_arg;
#include <sys/socket.h>
#include <limits.h>
-static size_t num_fds = 0, max_fds = 0, num_finished = 0, num_waiting = 0;
+static size_t num_fds = 0, max_fds = 0, num_closing = 0, num_waiting = 0;
static struct pollfd *pollfds = NULL;
static struct fd **fds = NULL;
static struct timers timeouts;
return true;
}
-static void adjust_counts(enum io_state state)
-{
- if (state == IO_FINISHED)
- num_finished++;
-}
-
static void update_pollevents(struct io_conn *conn)
{
struct pollfd *pfd = &pollfds[conn->fd.backend_info];
if (pfd->events)
num_waiting++;
- adjust_counts(conn->plan.state);
+ if (!conn->plan.next)
+ num_closing++;
}
bool add_conn(struct io_conn *c)
{
if (!add_fd(&c->fd, c->plan.pollflag))
return false;
- adjust_counts(c->plan.state);
+ /* Immediate close is allowed. */
+ if (!c->plan.next)
+ num_closing++;
return true;
}
static void del_conn(struct io_conn *conn)
{
- assert(conn->plan.state == IO_FINISHED);
if (conn->finish)
conn->finish(conn, conn->finish_arg);
if (timeout_active(conn))
conn->duplex->duplex = NULL;
} else
del_fd(&conn->fd);
- num_finished--;
+ num_closing--;
}
void del_listener(struct io_listener *l)
for (i = 0; !io_loop_return && i < num_fds; i++) {
struct io_conn *c, *duplex;
- if (!num_finished)
+ if (!num_closing)
break;
if (fds[i]->listener)
continue;
c = (void *)fds[i];
for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
- if (c->plan.state == IO_FINISHED) {
+ if (!c->plan.next) {
del_conn(c);
free(c);
i--;
}
}
- if (num_finished) {
+ if (num_closing) {
finish_conns();
/* Could have started/finished more. */
continue;
}
}
- while (num_finished)
+ while (num_closing)
finish_conns();
ret = io_loop_return;
io_break(d, io_idle());
}
+static struct io_plan never(struct io_conn *conn, void *arg)
+{
+ abort();
+}
+
static void init_conn(int fd, struct data *d)
{
int fd2;
/* This will wake us up, as read will fail. */
fd2 = open("/dev/null", O_RDONLY);
ok1(fd2 >= 0);
- ok1(io_new_conn(fd2, io_read(idler, 1, NULL, NULL), finish_waker, d));
+ ok1(io_new_conn(fd2, io_read(idler, 1, never, NULL), finish_waker, d));
}
static int make_listen_fd(const char *port, struct addrinfo **info)