Browse Source

io: io_never for events that should never happen.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell 12 years ago
parent
commit
cdf62dce70

+ 10 - 0
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;

+ 15 - 0
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 */
 
 /**

+ 1 - 1
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)

+ 1 - 1
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)

+ 1 - 1
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)

+ 1 - 1
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)

+ 1 - 1
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)

+ 1 - 1
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)

+ 1 - 1
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)

+ 1 - 1
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)

+ 1 - 1
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)

+ 1 - 1
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)