From: Rusty Russell Date: Tue, 8 Apr 2014 09:33:52 +0000 (+0930) Subject: io: io_never for events that should never happen. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=cdf62dce7077a9f9a818edbb67d31d033cbb73c6 io: io_never for events that should never happen. Signed-off-by: Rusty Russell --- diff --git a/ccan/io/io.c b/ccan/io/io.c index 734cb393..90039e1e 100644 --- a/ccan/io/io.c +++ b/ccan/io/io.c @@ -531,6 +531,16 @@ struct io_plan io_break_(void *ret, struct io_plan plan) return plan; } +static struct io_plan io_never_called(struct io_conn *conn, void *arg) +{ + abort(); +} + +struct io_plan io_never(void) +{ + return io_always_(io_never_called, NULL); +} + int io_conn_fd(const struct io_conn *conn) { return conn->fd.fd; diff --git a/ccan/io/io.h b/ccan/io/io.h index ecbeb0b0..478190f4 100644 --- a/ccan/io/io.h +++ b/ccan/io/io.h @@ -490,6 +490,21 @@ bool io_is_idle(const struct io_conn *conn); #define io_break(ret, plan) (io_plan_no_debug(), io_break_((ret), (plan))) struct io_plan io_break_(void *ret, struct io_plan plan); +/** + * io_never - assert if callback is called. + * + * Sometimes you want to make it clear that a callback should never happen + * (eg. for io_break). This will assert() if called. + * + * Example: + * static struct io_plan break_out(struct io_conn *conn, void *unused) + * { + * // We won't ever return from io_break + * return io_break(conn, io_never()); + * } + */ +struct io_plan io_never(void); + /* FIXME: io_recvfrom/io_sendto */ /** diff --git a/ccan/io/test/run-01-start-finish.c b/ccan/io/test/run-01-start-finish.c index d8a24a52..3ee65947 100644 --- a/ccan/io/test/run-01-start-finish.c +++ b/ccan/io/test/run-01-start-finish.c @@ -16,7 +16,7 @@ static void finish_ok(struct io_conn *conn, int *state) ok1(*state == 1); ok1(io_conn_fd(conn) == expected_fd); (*state)++; - io_break(state + 1, io_idle()); + io_break(state + 1, io_never()); } static void init_conn(int fd, int *state) diff --git a/ccan/io/test/run-02-read.c b/ccan/io/test/run-02-read.c index c7a7eaf1..439beb1f 100644 --- a/ccan/io/test/run-02-read.c +++ b/ccan/io/test/run-02-read.c @@ -19,7 +19,7 @@ static void finish_ok(struct io_conn *conn, struct data *d) { ok1(d->state == 1); d->state++; - io_break(d, io_idle()); + io_break(d, io_never()); } static void init_conn(int fd, struct data *d) diff --git a/ccan/io/test/run-03-readpartial.c b/ccan/io/test/run-03-readpartial.c index 7ecccc74..860792b7 100644 --- a/ccan/io/test/run-03-readpartial.c +++ b/ccan/io/test/run-03-readpartial.c @@ -20,7 +20,7 @@ static void finish_ok(struct io_conn *conn, struct data *d) { ok1(d->state == 1); d->state++; - io_break(d, io_idle()); + io_break(d, io_never()); } static void init_conn(int fd, struct data *d) diff --git a/ccan/io/test/run-04-writepartial.c b/ccan/io/test/run-04-writepartial.c index 8cfb2abe..5c1f5e01 100644 --- a/ccan/io/test/run-04-writepartial.c +++ b/ccan/io/test/run-04-writepartial.c @@ -20,7 +20,7 @@ static void finish_ok(struct io_conn *conn, struct data *d) { ok1(d->state == 1); d->state++; - io_break(d, io_idle()); + io_break(d, io_never()); } static void init_conn(int fd, struct data *d) diff --git a/ccan/io/test/run-05-write.c b/ccan/io/test/run-05-write.c index 5319def8..2c3e1839 100644 --- a/ccan/io/test/run-05-write.c +++ b/ccan/io/test/run-05-write.c @@ -20,7 +20,7 @@ static void finish_ok(struct io_conn *conn, struct data *d) { ok1(d->state == 1); d->state++; - io_break(d, io_idle()); + io_break(d, io_never()); } static void init_conn(int fd, struct data *d) diff --git a/ccan/io/test/run-06-idle.c b/ccan/io/test/run-06-idle.c index 455b8608..7ae16e9e 100644 --- a/ccan/io/test/run-06-idle.c +++ b/ccan/io/test/run-06-idle.c @@ -39,7 +39,7 @@ static void finish_idle(struct io_conn *conn, struct data *d) { ok1(d->state == 3); d->state++; - io_break(d, io_idle()); + io_break(d, io_never()); } static struct io_plan never(struct io_conn *conn, void *arg) diff --git a/ccan/io/test/run-15-timeout.c b/ccan/io/test/run-15-timeout.c index 6f92ec3a..2aceb2b5 100644 --- a/ccan/io/test/run-15-timeout.c +++ b/ccan/io/test/run-15-timeout.c @@ -38,7 +38,7 @@ static void finish_ok(struct io_conn *conn, struct data *d) { ok1(d->state == 2); d->state++; - io_break(d, io_idle()); + io_break(d, io_never()); } static void init_conn(int fd, struct data *d) diff --git a/ccan/io/test/run-17-homemade-io.c b/ccan/io/test/run-17-homemade-io.c index 65fe7415..c5d82889 100644 --- a/ccan/io/test/run-17-homemade-io.c +++ b/ccan/io/test/run-17-homemade-io.c @@ -20,7 +20,7 @@ static void finish_ok(struct io_conn *conn, struct packet *pkt) { ok1(pkt->state == 3); pkt->state++; - io_break(pkt, io_idle()); + io_break(pkt, io_never()); } static int do_read_packet(int fd, struct io_plan *plan) diff --git a/ccan/io/test/run-19-always.c b/ccan/io/test/run-19-always.c index e6413fc9..1805f79f 100644 --- a/ccan/io/test/run-19-always.c +++ b/ccan/io/test/run-19-always.c @@ -20,7 +20,7 @@ static void finish_ok(struct io_conn *conn, struct data *d) { ok1(d->state == 1); d->state++; - io_break(d, io_idle()); + io_break(d, io_never()); } static struct io_plan write_buf(struct io_conn *conn, struct data *d) diff --git a/ccan/io/test/run-set_alloc.c b/ccan/io/test/run-set_alloc.c index 61440056..8d201689 100644 --- a/ccan/io/test/run-set_alloc.c +++ b/ccan/io/test/run-set_alloc.c @@ -95,7 +95,7 @@ static void finish_ok(struct io_conn *conn, struct data *d) { ok1(d->state == 2); d->state++; - io_break(d, io_idle()); + io_break(d, io_never()); } static void init_conn(int fd, struct data *d)