From 6109a0a6140acbbfe5e998f7d7ea1215f035cb90 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 4 Aug 2014 17:41:21 +0930 Subject: [PATCH] ccan/io: eliminate dir argument from io_wait and io_always. We can use either empty slot for this, so figure it out internally. This could cause problems if you want to use it with io_duplex, so document that. Signed-off-by: Rusty Russell --- ccan/io/_info | 4 ++-- ccan/io/io.c | 23 ++++++++++++++++----- ccan/io/io.h | 27 +++++++++---------------- ccan/io/io_plan.h | 5 +++++ ccan/io/test/run-06-idle.c | 2 +- ccan/io/test/run-08-hangup-on-idle.c | 2 +- ccan/io/test/run-08-read-after-hangup.c | 2 +- ccan/io/test/run-10-many.c | 6 +++--- ccan/io/test/run-13-all-idle.c | 2 +- 9 files changed, 42 insertions(+), 31 deletions(-) diff --git a/ccan/io/_info b/ccan/io/_info index 6dd633ab..7fc6ac61 100644 --- a/ccan/io/_info +++ b/ccan/io/_info @@ -51,7 +51,7 @@ * * // No room? Wait for writer * if (b->end == sizeof(b->buf)) - * return io_wait(c, b, IO_IN, read_in, b); + * return io_wait(c, b, read_in, b); * * return io_read_partial(c, b->buf + b->end, sizeof(b->buf) - b->end, * &b->rlen, read_in, b); @@ -71,7 +71,7 @@ * if (b->end == b->start) { * if (b->finished) * return io_close(c); - * return io_wait(c, b, IO_OUT, write_out, b); + * return io_wait(c, b, write_out, b); * } * * return io_write_partial(c, b->buf + b->start, b->end - b->start, diff --git a/ccan/io/io.c b/ccan/io/io.c index 52f4368c..2832c513 100644 --- a/ccan/io/io.c +++ b/ccan/io/io.c @@ -124,11 +124,17 @@ static struct io_plan *set_always(struct io_conn *conn, } struct io_plan *io_always_(struct io_conn *conn, - enum io_direction dir, struct io_plan *(*next)(struct io_conn *, void *), void *arg) { - struct io_plan *plan = io_get_plan(conn, dir); + struct io_plan *plan; + + /* If we're duplex, we want this on the current plan. Otherwise, + * doesn't matter. */ + if (conn->plan[IO_IN].status == IO_UNSET) + plan = io_get_plan(conn, IO_IN); + else + plan = io_get_plan(conn, IO_OUT); assert(next); set_always(conn, plan, next, arg); @@ -320,11 +326,18 @@ struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr, } struct io_plan *io_wait_(struct io_conn *conn, - const void *wait, enum io_direction dir, + const void *wait, struct io_plan *(*next)(struct io_conn *, void *), void *arg) { - struct io_plan *plan = io_get_plan(conn, dir); + struct io_plan *plan; + + /* If we're duplex, we want this on the current plan. Otherwise, + * doesn't matter. */ + if (conn->plan[IO_IN].status == IO_UNSET) + plan = io_get_plan(conn, IO_IN); + else + plan = io_get_plan(conn, IO_OUT); assert(next); @@ -417,7 +430,7 @@ void io_break(const void *ret) struct io_plan *io_never(struct io_conn *conn) { - return io_always(conn, IO_IN, io_never_called, NULL); + return io_always(conn, io_never_called, NULL); } int io_conn_fd(const struct io_conn *conn) diff --git a/ccan/io/io.h b/ccan/io/io.h index e2792844..00b9b33d 100644 --- a/ccan/io/io.h +++ b/ccan/io/io.h @@ -6,11 +6,6 @@ #include #include -enum io_direction { - IO_IN, - IO_OUT -}; - /** * struct io_plan - a plan for input or output. * @@ -340,7 +335,6 @@ struct io_plan *io_write_partial_(struct io_conn *conn, /** * io_always - plan to immediately call next callback * @conn: the connection that plan is for. - * @dir: IO_IN or IO_OUT * @next: function to call. * @arg: @next argument * @@ -352,16 +346,16 @@ struct io_plan *io_write_partial_(struct io_conn *conn, * void *unused) * { * // Silly example: close on next time around loop. - * return io_always(conn, IO_IN, io_close_cb, NULL); + * return io_always(conn, io_close_cb, NULL); * } */ -#define io_always(conn, dir, next, arg) \ - io_always_((conn), dir, typesafe_cb_preargs(struct io_plan *, void *, \ - (next), (arg), \ - struct io_conn *), \ +#define io_always(conn, next, arg) \ + io_always_((conn), typesafe_cb_preargs(struct io_plan *, void *, \ + (next), (arg), \ + struct io_conn *), \ (arg)) -struct io_plan *io_always_(struct io_conn *conn, enum io_direction dir, +struct io_plan *io_always_(struct io_conn *conn, struct io_plan *(*next)(struct io_conn *, void *), void *arg); @@ -413,7 +407,6 @@ struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr, * io_wait - leave a plan idle until something wakes us. * @conn: the connection that plan is for. * @waitaddr: the address to wait on. - * @dir: IO_IN or IO_OUT * @next: function to call after waiting. * @arg: @next argument * @@ -424,18 +417,18 @@ struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr, * // Silly example to wait then close. * static struct io_plan *wait(struct io_conn *conn, void *b) * { - * return io_wait(conn, b, IO_IN, io_close_cb, NULL); + * return io_wait(conn, b, io_close_cb, NULL); * } */ -#define io_wait(conn, waitaddr, dir, next, arg) \ - io_wait_((conn), (waitaddr), (dir), \ +#define io_wait(conn, waitaddr, next, arg) \ + io_wait_((conn), (waitaddr), \ typesafe_cb_preargs(struct io_plan *, void *, \ (next), (arg), \ struct io_conn *), \ (arg)) struct io_plan *io_wait_(struct io_conn *conn, - const void *wait, enum io_direction dir, + const void *wait, struct io_plan *(*next)(struct io_conn *, void *), void *arg); diff --git a/ccan/io/io_plan.h b/ccan/io/io_plan.h index 0b3c27b1..21a19217 100644 --- a/ccan/io/io_plan.h +++ b/ccan/io/io_plan.h @@ -27,6 +27,11 @@ enum io_plan_status { IO_CLOSING }; +enum io_direction { + IO_IN, + IO_OUT +}; + /** * struct io_plan - one half of I/O to do * @status: the status of this plan. diff --git a/ccan/io/test/run-06-idle.c b/ccan/io/test/run-06-idle.c index e71f4d8b..d01cb31f 100644 --- a/ccan/io/test/run-06-idle.c +++ b/ccan/io/test/run-06-idle.c @@ -71,7 +71,7 @@ static struct io_plan *init_idle(struct io_conn *conn, struct data *d) ok1(fd2 >= 0); io_set_finish(io_new_conn(NULL, fd2, init_waker, d), finish_waker, d); - return io_wait(conn, d, IO_IN, read_buf, d); + return io_wait(conn, d, read_buf, d); } static int make_listen_fd(const char *port, struct addrinfo **info) diff --git a/ccan/io/test/run-08-hangup-on-idle.c b/ccan/io/test/run-08-hangup-on-idle.c index c1ff1e6e..52309b4b 100644 --- a/ccan/io/test/run-08-hangup-on-idle.c +++ b/ccan/io/test/run-08-hangup-on-idle.c @@ -15,7 +15,7 @@ static struct io_plan *read_in(struct io_conn *conn, char *buf) static struct io_plan *setup_waiter(struct io_conn *conn, char *buf) { - return io_wait(conn, buf, IO_IN, read_in, buf); + return io_wait(conn, buf, read_in, buf); } static struct io_plan *wake_and_close(struct io_conn *conn, char *buf) diff --git a/ccan/io/test/run-08-read-after-hangup.c b/ccan/io/test/run-08-read-after-hangup.c index f9ed267d..7b6731ec 100644 --- a/ccan/io/test/run-08-read-after-hangup.c +++ b/ccan/io/test/run-08-read-after-hangup.c @@ -27,7 +27,7 @@ static struct io_plan *init_writer(struct io_conn *conn, struct io_conn *wakeme) static struct io_plan *init_waiter(struct io_conn *conn, void *unused) { - return io_wait(conn, inbuf, IO_IN, read_buf, NULL); + return io_wait(conn, inbuf, read_buf, NULL); } int main(void) diff --git a/ccan/io/test/run-10-many.c b/ccan/io/test/run-10-many.c index b70310dd..fc48ecb5 100644 --- a/ccan/io/test/run-10-many.c +++ b/ccan/io/test/run-10-many.c @@ -34,7 +34,7 @@ static struct io_plan *poke_writer(struct io_conn *conn, struct buffer *buf) io_wake(&buf->writer); /* I'll wait until you wake me. */ - return io_wait(conn, &buf->reader, IO_IN, read_buf, buf); + return io_wait(conn, &buf->reader, read_buf, buf); } static struct io_plan *write_buf(struct io_conn *conn, struct buffer *buf) @@ -52,12 +52,12 @@ static struct io_plan *poke_reader(struct io_conn *conn, struct buffer *buf) return io_close(conn); /* I'll wait until you tell me to write. */ - return io_wait(conn, &buf->writer, IO_OUT, write_buf, buf); + return io_wait(conn, &buf->writer, write_buf, buf); } static struct io_plan *setup_reader(struct io_conn *conn, struct buffer *buf) { - return io_wait(conn, &buf->reader, IO_IN, read_buf, buf); + return io_wait(conn, &buf->reader, read_buf, buf); } static struct buffer buf[NUM]; diff --git a/ccan/io/test/run-13-all-idle.c b/ccan/io/test/run-13-all-idle.c index f8ee97f8..3701472b 100644 --- a/ccan/io/test/run-13-all-idle.c +++ b/ccan/io/test/run-13-all-idle.c @@ -9,7 +9,7 @@ static struct io_plan *setup_waiter(struct io_conn *conn, int *status) { - return io_wait(conn, status, IO_IN, io_close_cb, NULL); + return io_wait(conn, status, io_close_cb, NULL); } int main(void) -- 2.39.2