Commit 3a7b8a8a authored by Rusty Russell's avatar Rusty Russell

ccan/io: io_close_cb()

Overloading io_close() as a callback is ugly: create an explicit
io_close_cb().
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 869dc152
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
* static void reader_exit(struct io_conn *c, struct stdin_buffer *b) * static void reader_exit(struct io_conn *c, struct stdin_buffer *b)
* { * {
* assert(c == b->reader); * assert(c == b->reader);
* io_wake(b->writer, io_close(b->writer, NULL)); * io_wake(b->writer, io_close());
* b->reader = NULL; * b->reader = NULL;
* } * }
* *
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
* { * {
* assert(c == b->writer); * assert(c == b->writer);
* if (!b->reader) * if (!b->reader)
* return io_close(c, NULL); * return io_close();
* b->len = sizeof(b->inbuf); * b->len = sizeof(b->inbuf);
* io_wake(b->reader, io_read_partial(b->inbuf, &b->len, wake_writer, b)); * io_wake(b->reader, io_read_partial(b->inbuf, &b->len, wake_writer, b));
* return io_idle(); * return io_idle();
......
...@@ -23,7 +23,7 @@ static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf) ...@@ -23,7 +23,7 @@ static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf)
assert(conn == buf->reader); assert(conn == buf->reader);
if (buf->iters == NUM_ITERS) if (buf->iters == NUM_ITERS)
return io_close(conn, NULL); return io_close();
/* You write. */ /* You write. */
io_wake(buf->writer, io_wake(buf->writer,
...@@ -41,7 +41,7 @@ static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf) ...@@ -41,7 +41,7 @@ static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf)
io_read(&buf->buf, sizeof(buf->buf), poke_writer, buf)); io_read(&buf->buf, sizeof(buf->buf), poke_writer, buf));
if (++buf->iters == NUM_ITERS) if (++buf->iters == NUM_ITERS)
return io_close(conn, NULL); return io_close();
/* I'll wait until you tell me to write. */ /* I'll wait until you tell me to write. */
return io_idle(); return io_idle();
......
...@@ -275,8 +275,8 @@ struct io_plan io_idle(void) ...@@ -275,8 +275,8 @@ struct io_plan io_idle(void)
plan.pollflag = 0; plan.pollflag = 0;
plan.io = NULL; plan.io = NULL;
/* Never called (overridded by io_wake), but NULL means closing */ /* Never called (overridden by io_wake), but NULL means closing */
plan.next = io_close; plan.next = (void *)io_idle;
io_plan_debug(&plan); io_plan_debug(&plan);
return plan; return plan;
...@@ -301,7 +301,7 @@ void io_ready(struct io_conn *conn) ...@@ -301,7 +301,7 @@ void io_ready(struct io_conn *conn)
switch (conn->plan.io(conn->fd.fd, &conn->plan)) { switch (conn->plan.io(conn->fd.fd, &conn->plan)) {
case -1: /* Failure means a new plan: close up. */ case -1: /* Failure means a new plan: close up. */
set_current(conn); set_current(conn);
conn->plan = io_close(NULL, NULL); conn->plan = io_close();
backend_plan_changed(conn); backend_plan_changed(conn);
set_current(NULL); set_current(NULL);
break; break;
...@@ -317,9 +317,8 @@ void io_ready(struct io_conn *conn) ...@@ -317,9 +317,8 @@ void io_ready(struct io_conn *conn)
} }
} }
/* Useful next functions. */
/* Close the connection, we're done. */ /* Close the connection, we're done. */
struct io_plan io_close(struct io_conn *conn, void *arg) struct io_plan io_close(void)
{ {
struct io_plan plan; struct io_plan plan;
...@@ -331,6 +330,11 @@ struct io_plan io_close(struct io_conn *conn, void *arg) ...@@ -331,6 +330,11 @@ struct io_plan io_close(struct io_conn *conn, void *arg)
return plan; return plan;
} }
struct io_plan io_close_cb(struct io_conn *conn, void *arg)
{
return io_close();
}
/* Exit the loop, returning this (non-NULL) arg. */ /* Exit the loop, returning this (non-NULL) arg. */
struct io_plan io_break_(void *ret, struct io_plan plan) struct io_plan io_break_(void *ret, struct io_plan plan)
{ {
......
...@@ -290,17 +290,20 @@ struct io_plan io_break_(void *ret, struct io_plan plan); ...@@ -290,17 +290,20 @@ struct io_plan io_break_(void *ret, struct io_plan plan);
/* FIXME: io_recvfrom/io_sendto */ /* FIXME: io_recvfrom/io_sendto */
/** /**
* io_close - terminate a connection. * io_close - plan to close a connection.
* @conn: any connection.
* *
* The schedules a connection to be closed. It can be done on any * On return to io_loop, the connection will be closed.
* connection, whether it has I/O queued or not (though that I/O may */
* be performed first). struct io_plan io_close(void);
/**
* io_close_cb - helper callback to close a connection.
* @conn: the connection.
* *
* It's common to 'return io_close(...)' from a @next function, but * This schedules a connection to be closed; designed to be used as
* io_close can also be used as an argument to io_next(). * a callback function.
*/ */
struct io_plan io_close(struct io_conn *, void *unused); struct io_plan io_close_cb(struct io_conn *, void *unused);
/** /**
* io_loop - process fds until all closed on io_break. * io_loop - process fds until all closed on io_break.
......
...@@ -341,11 +341,10 @@ void *io_loop(void) ...@@ -341,11 +341,10 @@ void *io_loop(void)
} else if (events & POLLHUP) { } else if (events & POLLHUP) {
r--; r--;
set_current(c); set_current(c);
set_plan(c, io_close(c, NULL)); set_plan(c, io_close());
if (c->duplex) { if (c->duplex) {
set_current(c->duplex); set_current(c->duplex);
set_plan(c->duplex, set_plan(c->duplex, io_close());
io_close(c->duplex, NULL));
} }
} }
} }
......
...@@ -21,8 +21,7 @@ static void init_conn(int fd, int *state) ...@@ -21,8 +21,7 @@ static void init_conn(int fd, int *state)
{ {
ok1(*state == 0); ok1(*state == 0);
(*state)++; (*state)++;
io_set_finish(io_new_conn(fd, io_close(NULL, NULL)), finish_ok, state); io_set_finish(io_new_conn(fd, io_close()), finish_ok, state);
} }
static int make_listen_fd(const char *port, struct addrinfo **info) static int make_listen_fd(const char *port, struct addrinfo **info)
......
...@@ -28,7 +28,7 @@ static void init_conn(int fd, struct data *d) ...@@ -28,7 +28,7 @@ static void init_conn(int fd, struct data *d)
d->state++; d->state++;
io_set_finish(io_new_conn(fd, io_set_finish(io_new_conn(fd,
io_read(d->buf, sizeof(d->buf), io_close, d)), io_read(d->buf, sizeof(d->buf), io_close_cb, d)),
finish_ok, d); finish_ok, d);
} }
......
...@@ -30,7 +30,7 @@ static void init_conn(int fd, struct data *d) ...@@ -30,7 +30,7 @@ static void init_conn(int fd, struct data *d)
d->bytes = sizeof(d->buf); d->bytes = sizeof(d->buf);
io_set_finish(io_new_conn(fd, io_set_finish(io_new_conn(fd,
io_read_partial(d->buf, &d->bytes, io_close, d)), io_read_partial(d->buf, &d->bytes, io_close_cb, d)),
finish_ok, d); finish_ok, d);
} }
......
...@@ -28,7 +28,7 @@ static void init_conn(int fd, struct data *d) ...@@ -28,7 +28,7 @@ static void init_conn(int fd, struct data *d)
ok1(d->state == 0); ok1(d->state == 0);
d->state++; d->state++;
io_set_finish(io_new_conn(fd, io_set_finish(io_new_conn(fd,
io_write_partial(d->buf, &d->bytes, io_close, d)), io_write_partial(d->buf, &d->bytes, io_close_cb, d)),
finish_ok, d); finish_ok, d);
} }
......
...@@ -27,7 +27,8 @@ static void init_conn(int fd, struct data *d) ...@@ -27,7 +27,8 @@ static void init_conn(int fd, struct data *d)
{ {
ok1(d->state == 0); ok1(d->state == 0);
d->state++; d->state++;
io_set_finish(io_new_conn(fd, io_write(d->buf, d->bytes, io_close, d)), io_set_finish(io_new_conn(fd, io_write(d->buf, d->bytes,
io_close_cb, d)),
finish_ok, d); finish_ok, d);
} }
......
...@@ -24,7 +24,7 @@ static struct io_plan read_done(struct io_conn *conn, struct data *d) ...@@ -24,7 +24,7 @@ static struct io_plan read_done(struct io_conn *conn, struct data *d)
{ {
ok1(d->state == 2 || d->state == 3); ok1(d->state == 2 || d->state == 3);
d->state++; d->state++;
return io_close(conn, NULL); return io_close();
} }
static void finish_waker(struct io_conn *conn, struct data *d) static void finish_waker(struct io_conn *conn, struct data *d)
......
...@@ -19,7 +19,7 @@ static struct io_plan read_done(struct io_conn *conn, struct data *d) ...@@ -19,7 +19,7 @@ static struct io_plan read_done(struct io_conn *conn, struct data *d)
{ {
ok1(d->state == 1); ok1(d->state == 1);
d->state++; d->state++;
return io_close(conn, NULL); return io_close();
} }
static void finish_ok(struct io_conn *conn, struct data *d) static void finish_ok(struct io_conn *conn, struct data *d)
......
...@@ -12,7 +12,7 @@ static struct io_plan timeout_wakeup(struct io_conn *conn, char *buf) ...@@ -12,7 +12,7 @@ static struct io_plan timeout_wakeup(struct io_conn *conn, char *buf)
{ {
/* This kills the dummy connection. */ /* This kills the dummy connection. */
close(fds2[1]); close(fds2[1]);
return io_read(buf, 16, io_close, NULL); return io_read(buf, 16, io_close_cb, NULL);
} }
int main(void) int main(void)
...@@ -26,12 +26,13 @@ int main(void) ...@@ -26,12 +26,13 @@ int main(void)
ok1(pipe(fds) == 0); ok1(pipe(fds) == 0);
/* Write then close. */ /* Write then close. */
io_new_conn(fds[1], io_write("hello there world", 16, io_close, NULL)); io_new_conn(fds[1], io_write("hello there world", 16,
io_close_cb, NULL));
conn = io_new_conn(fds[0], io_idle()); conn = io_new_conn(fds[0], io_idle());
/* To avoid assert(num_waiting) */ /* To avoid assert(num_waiting) */
ok1(pipe(fds2) == 0); ok1(pipe(fds2) == 0);
io_new_conn(fds2[0], io_read(buf, 16, io_close, NULL)); io_new_conn(fds2[0], io_read(buf, 16, io_close_cb, NULL));
/* After half a second, it will read. */ /* After half a second, it will read. */
io_timeout(conn, time_from_msec(500), timeout_wakeup, buf); io_timeout(conn, time_from_msec(500), timeout_wakeup, buf);
......
...@@ -11,8 +11,8 @@ static char inbuf[8]; ...@@ -11,8 +11,8 @@ static char inbuf[8];
static struct io_plan wake_it(struct io_conn *conn, struct io_conn *reader) static struct io_plan wake_it(struct io_conn *conn, struct io_conn *reader)
{ {
io_wake(reader, io_read(inbuf, 8, io_close, NULL)); io_wake(reader, io_read(inbuf, 8, io_close_cb, NULL));
return io_close(conn, NULL); return io_close();
} }
int main(void) int main(void)
......
...@@ -22,7 +22,7 @@ static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf) ...@@ -22,7 +22,7 @@ static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf)
assert(conn == buf->reader); assert(conn == buf->reader);
if (buf->iters == NUM_ITERS) if (buf->iters == NUM_ITERS)
return io_close(conn, NULL); return io_close();
/* You write. */ /* You write. */
io_wake(buf->writer, io_wake(buf->writer,
...@@ -40,7 +40,7 @@ static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf) ...@@ -40,7 +40,7 @@ static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf)
io_read(&buf->buf, sizeof(buf->buf), poke_writer, buf)); io_read(&buf->buf, sizeof(buf->buf), poke_writer, buf));
if (++buf->iters == NUM_ITERS) if (++buf->iters == NUM_ITERS)
return io_close(conn, NULL); return io_close();
/* I'll wait until you tell me to write. */ /* I'll wait until you tell me to write. */
return io_idle(); return io_idle();
......
...@@ -25,7 +25,7 @@ static void finish_ok(struct io_conn *conn, struct data *d) ...@@ -25,7 +25,7 @@ static void finish_ok(struct io_conn *conn, struct data *d)
static struct io_plan write_done(struct io_conn *conn, struct data *d) static struct io_plan write_done(struct io_conn *conn, struct data *d)
{ {
d->state++; d->state++;
return io_close(conn, NULL); return io_close();
} }
static void init_conn(int fd, struct data *d) static void init_conn(int fd, struct data *d)
...@@ -39,7 +39,7 @@ static void init_conn(int fd, struct data *d) ...@@ -39,7 +39,7 @@ static void init_conn(int fd, struct data *d)
memset(d->wbuf, 7, sizeof(d->wbuf)); memset(d->wbuf, 7, sizeof(d->wbuf));
conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close, d)); conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close_cb, d));
io_set_finish(conn, finish_ok, d); io_set_finish(conn, finish_ok, d);
conn = io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d)); conn = io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d));
ok1(conn); ok1(conn);
......
...@@ -23,7 +23,7 @@ static struct io_plan no_timeout(struct io_conn *conn, struct data *d) ...@@ -23,7 +23,7 @@ static struct io_plan no_timeout(struct io_conn *conn, struct data *d)
{ {
ok1(d->state == 1); ok1(d->state == 1);
d->state++; d->state++;
return io_close(conn, d); return io_close();
} }
static struct io_plan timeout(struct io_conn *conn, struct data *d) static struct io_plan timeout(struct io_conn *conn, struct data *d)
...@@ -31,7 +31,7 @@ static struct io_plan timeout(struct io_conn *conn, struct data *d) ...@@ -31,7 +31,7 @@ static struct io_plan timeout(struct io_conn *conn, struct data *d)
ok1(d->state == 1); ok1(d->state == 1);
d->state++; d->state++;
d->timed_out = true; d->timed_out = true;
return io_close(conn, d); return io_close();
} }
static void finish_ok(struct io_conn *conn, struct data *d) static void finish_ok(struct io_conn *conn, struct data *d)
......
...@@ -90,7 +90,7 @@ static void init_conn(int fd, struct packet *pkt) ...@@ -90,7 +90,7 @@ static void init_conn(int fd, struct packet *pkt)
ok1(pkt->state == 0); ok1(pkt->state == 0);
pkt->state++; pkt->state++;
io_set_finish(io_new_conn(fd, io_read_packet(pkt, io_close, pkt)), io_set_finish(io_new_conn(fd, io_read_packet(pkt, io_close_cb, pkt)),
finish_ok, pkt); finish_ok, pkt);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment