From 3a7b8a8a8081ebbb6457527de376dec6264bc381 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 14 Oct 2013 21:32:57 +1030 Subject: [PATCH] ccan/io: io_close_cb() Overloading io_close() as a callback is ugly: create an explicit io_close_cb(). Signed-off-by: Rusty Russell --- ccan/io/_info | 4 ++-- ccan/io/benchmarks/run-loop.c | 4 ++-- ccan/io/io.c | 14 +++++++++----- ccan/io/io.h | 19 +++++++++++-------- ccan/io/poll.c | 5 ++--- ccan/io/test/run-01-start-finish.c | 3 +-- ccan/io/test/run-02-read.c | 2 +- ccan/io/test/run-03-readpartial.c | 2 +- ccan/io/test/run-04-writepartial.c | 2 +- ccan/io/test/run-05-write.c | 3 ++- ccan/io/test/run-06-idle.c | 2 +- ccan/io/test/run-07-break.c | 2 +- ccan/io/test/run-08-hangup-on-idle.c | 7 ++++--- ccan/io/test/run-08-read-after-hangup.c | 4 ++-- ccan/io/test/run-10-many.c | 4 ++-- ccan/io/test/run-12-bidir.c | 4 ++-- ccan/io/test/run-15-timeout.c | 4 ++-- ccan/io/test/run-17-homemade-io.c | 2 +- 18 files changed, 47 insertions(+), 40 deletions(-) diff --git a/ccan/io/_info b/ccan/io/_info index a96ee669..8786272c 100644 --- a/ccan/io/_info +++ b/ccan/io/_info @@ -46,7 +46,7 @@ * static void reader_exit(struct io_conn *c, struct stdin_buffer *b) * { * assert(c == b->reader); - * io_wake(b->writer, io_close(b->writer, NULL)); + * io_wake(b->writer, io_close()); * b->reader = NULL; * } * @@ -54,7 +54,7 @@ * { * assert(c == b->writer); * if (!b->reader) - * return io_close(c, NULL); + * return io_close(); * b->len = sizeof(b->inbuf); * io_wake(b->reader, io_read_partial(b->inbuf, &b->len, wake_writer, b)); * return io_idle(); diff --git a/ccan/io/benchmarks/run-loop.c b/ccan/io/benchmarks/run-loop.c index b0d383e8..ef01cf6a 100644 --- a/ccan/io/benchmarks/run-loop.c +++ b/ccan/io/benchmarks/run-loop.c @@ -23,7 +23,7 @@ static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf) assert(conn == buf->reader); if (buf->iters == NUM_ITERS) - return io_close(conn, NULL); + return io_close(); /* You write. */ io_wake(buf->writer, @@ -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)); if (++buf->iters == NUM_ITERS) - return io_close(conn, NULL); + return io_close(); /* I'll wait until you tell me to write. */ return io_idle(); diff --git a/ccan/io/io.c b/ccan/io/io.c index 1c0af6b9..352afe33 100644 --- a/ccan/io/io.c +++ b/ccan/io/io.c @@ -275,8 +275,8 @@ struct io_plan io_idle(void) plan.pollflag = 0; plan.io = NULL; - /* Never called (overridded by io_wake), but NULL means closing */ - plan.next = io_close; + /* Never called (overridden by io_wake), but NULL means closing */ + plan.next = (void *)io_idle; io_plan_debug(&plan); return plan; @@ -301,7 +301,7 @@ void io_ready(struct io_conn *conn) switch (conn->plan.io(conn->fd.fd, &conn->plan)) { case -1: /* Failure means a new plan: close up. */ set_current(conn); - conn->plan = io_close(NULL, NULL); + conn->plan = io_close(); backend_plan_changed(conn); set_current(NULL); break; @@ -317,9 +317,8 @@ void io_ready(struct io_conn *conn) } } -/* Useful next functions. */ /* 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; @@ -331,6 +330,11 @@ struct io_plan io_close(struct io_conn *conn, void *arg) 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. */ struct io_plan io_break_(void *ret, struct io_plan plan) { diff --git a/ccan/io/io.h b/ccan/io/io.h index 105a9082..8199bda9 100644 --- a/ccan/io/io.h +++ b/ccan/io/io.h @@ -290,17 +290,20 @@ struct io_plan io_break_(void *ret, struct io_plan plan); /* FIXME: io_recvfrom/io_sendto */ /** - * io_close - terminate a connection. - * @conn: any connection. + * io_close - plan to close a connection. * - * The schedules a connection to be closed. It can be done on any - * connection, whether it has I/O queued or not (though that I/O may - * be performed first). + * On return to io_loop, the connection will be closed. + */ +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 - * io_close can also be used as an argument to io_next(). + * This schedules a connection to be closed; designed to be used as + * 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. diff --git a/ccan/io/poll.c b/ccan/io/poll.c index 85407f62..06e3b591 100644 --- a/ccan/io/poll.c +++ b/ccan/io/poll.c @@ -341,11 +341,10 @@ void *io_loop(void) } else if (events & POLLHUP) { r--; set_current(c); - set_plan(c, io_close(c, NULL)); + set_plan(c, io_close()); if (c->duplex) { set_current(c->duplex); - set_plan(c->duplex, - io_close(c->duplex, NULL)); + set_plan(c->duplex, io_close()); } } } diff --git a/ccan/io/test/run-01-start-finish.c b/ccan/io/test/run-01-start-finish.c index 297dbc6f..a63baf77 100644 --- a/ccan/io/test/run-01-start-finish.c +++ b/ccan/io/test/run-01-start-finish.c @@ -21,8 +21,7 @@ static void init_conn(int fd, int *state) { ok1(*state == 0); (*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) diff --git a/ccan/io/test/run-02-read.c b/ccan/io/test/run-02-read.c index 65dbe4dc..c7a7eaf1 100644 --- a/ccan/io/test/run-02-read.c +++ b/ccan/io/test/run-02-read.c @@ -28,7 +28,7 @@ static void init_conn(int fd, struct data *d) d->state++; 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); } diff --git a/ccan/io/test/run-03-readpartial.c b/ccan/io/test/run-03-readpartial.c index d5411848..7ecccc74 100644 --- a/ccan/io/test/run-03-readpartial.c +++ b/ccan/io/test/run-03-readpartial.c @@ -30,7 +30,7 @@ static void init_conn(int fd, struct data *d) d->bytes = sizeof(d->buf); 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); } diff --git a/ccan/io/test/run-04-writepartial.c b/ccan/io/test/run-04-writepartial.c index c80238fe..8cfb2abe 100644 --- a/ccan/io/test/run-04-writepartial.c +++ b/ccan/io/test/run-04-writepartial.c @@ -28,7 +28,7 @@ static void init_conn(int fd, struct data *d) ok1(d->state == 0); d->state++; 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); } diff --git a/ccan/io/test/run-05-write.c b/ccan/io/test/run-05-write.c index bcbc9cad..5319def8 100644 --- a/ccan/io/test/run-05-write.c +++ b/ccan/io/test/run-05-write.c @@ -27,7 +27,8 @@ static void init_conn(int fd, struct data *d) { ok1(d->state == 0); 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); } diff --git a/ccan/io/test/run-06-idle.c b/ccan/io/test/run-06-idle.c index b32cbfe2..51cca961 100644 --- a/ccan/io/test/run-06-idle.c +++ b/ccan/io/test/run-06-idle.c @@ -24,7 +24,7 @@ static struct io_plan read_done(struct io_conn *conn, struct data *d) { ok1(d->state == 2 || d->state == 3); d->state++; - return io_close(conn, NULL); + return io_close(); } static void finish_waker(struct io_conn *conn, struct data *d) diff --git a/ccan/io/test/run-07-break.c b/ccan/io/test/run-07-break.c index 6fcaea34..19cc6a8e 100644 --- a/ccan/io/test/run-07-break.c +++ b/ccan/io/test/run-07-break.c @@ -19,7 +19,7 @@ static struct io_plan read_done(struct io_conn *conn, struct data *d) { ok1(d->state == 1); d->state++; - return io_close(conn, NULL); + return io_close(); } static void finish_ok(struct io_conn *conn, struct data *d) diff --git a/ccan/io/test/run-08-hangup-on-idle.c b/ccan/io/test/run-08-hangup-on-idle.c index 7463dd98..b3840433 100644 --- a/ccan/io/test/run-08-hangup-on-idle.c +++ b/ccan/io/test/run-08-hangup-on-idle.c @@ -12,7 +12,7 @@ static struct io_plan timeout_wakeup(struct io_conn *conn, char *buf) { /* This kills the dummy connection. */ close(fds2[1]); - return io_read(buf, 16, io_close, NULL); + return io_read(buf, 16, io_close_cb, NULL); } int main(void) @@ -26,12 +26,13 @@ int main(void) ok1(pipe(fds) == 0); /* 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()); /* To avoid assert(num_waiting) */ 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. */ io_timeout(conn, time_from_msec(500), timeout_wakeup, buf); diff --git a/ccan/io/test/run-08-read-after-hangup.c b/ccan/io/test/run-08-read-after-hangup.c index 56d9a164..b73139e4 100644 --- a/ccan/io/test/run-08-read-after-hangup.c +++ b/ccan/io/test/run-08-read-after-hangup.c @@ -11,8 +11,8 @@ static char inbuf[8]; static struct io_plan wake_it(struct io_conn *conn, struct io_conn *reader) { - io_wake(reader, io_read(inbuf, 8, io_close, NULL)); - return io_close(conn, NULL); + io_wake(reader, io_read(inbuf, 8, io_close_cb, NULL)); + return io_close(); } int main(void) diff --git a/ccan/io/test/run-10-many.c b/ccan/io/test/run-10-many.c index b13e1b43..da59a3cc 100644 --- a/ccan/io/test/run-10-many.c +++ b/ccan/io/test/run-10-many.c @@ -22,7 +22,7 @@ static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf) assert(conn == buf->reader); if (buf->iters == NUM_ITERS) - return io_close(conn, NULL); + return io_close(); /* You write. */ io_wake(buf->writer, @@ -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)); if (++buf->iters == NUM_ITERS) - return io_close(conn, NULL); + return io_close(); /* I'll wait until you tell me to write. */ return io_idle(); diff --git a/ccan/io/test/run-12-bidir.c b/ccan/io/test/run-12-bidir.c index 32319232..1ab0a218 100644 --- a/ccan/io/test/run-12-bidir.c +++ b/ccan/io/test/run-12-bidir.c @@ -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) { d->state++; - return io_close(conn, NULL); + return io_close(); } 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)); - 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); conn = io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d)); ok1(conn); diff --git a/ccan/io/test/run-15-timeout.c b/ccan/io/test/run-15-timeout.c index a3e9526b..6f92ec3a 100644 --- a/ccan/io/test/run-15-timeout.c +++ b/ccan/io/test/run-15-timeout.c @@ -23,7 +23,7 @@ static struct io_plan no_timeout(struct io_conn *conn, struct data *d) { ok1(d->state == 1); d->state++; - return io_close(conn, d); + return io_close(); } 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); d->state++; d->timed_out = true; - return io_close(conn, d); + return io_close(); } static void finish_ok(struct io_conn *conn, struct data *d) diff --git a/ccan/io/test/run-17-homemade-io.c b/ccan/io/test/run-17-homemade-io.c index de7ce751..07794fa9 100644 --- a/ccan/io/test/run-17-homemade-io.c +++ b/ccan/io/test/run-17-homemade-io.c @@ -90,7 +90,7 @@ static void init_conn(int fd, struct packet *pkt) ok1(pkt->state == 0); 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); } -- 2.39.2