Browse Source

ccan/io: flatten debug callchain further.

Don't call through io_loop, but have it pass the connection back
and call manually.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell 12 years ago
parent
commit
c321a1d027
5 changed files with 130 additions and 32 deletions
  1. 11 1
      ccan/io/backend.h
  2. 71 19
      ccan/io/io.c
  3. 13 0
      ccan/io/io_plan.h
  4. 31 8
      ccan/io/poll.c
  5. 4 4
      ccan/io/test/run-17-homemade-io.c

+ 11 - 1
ccan/io/backend.h

@@ -53,14 +53,22 @@ static inline void set_current(struct io_conn *conn)
 {
 {
 	current = conn;
 	current = conn;
 }
 }
+static inline bool doing_debug_on(struct io_conn *conn)
+{
+	return io_debug_conn && io_debug_conn(conn);
+}
 static inline bool doing_debug(void)
 static inline bool doing_debug(void)
 {
 {
-	return io_debug_conn != NULL;
+	return io_debug_conn;
 }
 }
 #else
 #else
 static inline void set_current(struct io_conn *conn)
 static inline void set_current(struct io_conn *conn)
 {
 {
 }
 }
+static inline bool doing_debug_on(struct io_conn *conn)
+{
+	return false;
+}
 static inline bool doing_debug(void)
 static inline bool doing_debug(void)
 {
 {
 	return false;
 	return false;
@@ -74,6 +82,8 @@ void del_listener(struct io_listener *l);
 void backend_plan_changed(struct io_conn *conn);
 void backend_plan_changed(struct io_conn *conn);
 void backend_add_timeout(struct io_conn *conn, struct timespec ts);
 void backend_add_timeout(struct io_conn *conn, struct timespec ts);
 void backend_del_timeout(struct io_conn *conn);
 void backend_del_timeout(struct io_conn *conn);
+void backend_del_conn(struct io_conn *conn);
 
 
 void io_ready(struct io_conn *conn);
 void io_ready(struct io_conn *conn);
+void *do_io_loop(struct io_conn **ready);
 #endif /* CCAN_IO_BACKEND_H */
 #endif /* CCAN_IO_BACKEND_H */

+ 71 - 19
ccan/io/io.c

@@ -24,24 +24,78 @@ bool io_debug_wakeup;
 
 
 struct io_plan io_debug(struct io_plan plan)
 struct io_plan io_debug(struct io_plan plan)
 {
 {
+	struct io_conn *ready = NULL;
+
 	if (io_plan_nodebug) {
 	if (io_plan_nodebug) {
 		io_plan_nodebug = false;
 		io_plan_nodebug = false;
 		return plan;
 		return plan;
 	}
 	}
 
 
-	if (!io_debug_conn || !current)
-		return plan;
-
-	if (!io_debug_conn(current) && !io_debug_wakeup)
-		return plan;
+	if (!current || !doing_debug_on(current)) {
+		if (!io_debug_wakeup)
+			return plan;
+	}
 
 
 	io_debug_wakeup = false;
 	io_debug_wakeup = false;
 	current->plan = plan;
 	current->plan = plan;
 	backend_plan_changed(current);
 	backend_plan_changed(current);
 
 
 	/* Call back into the loop immediately. */
 	/* Call back into the loop immediately. */
-	io_loop_return = io_loop();
-	return plan;
+	io_loop_return = do_io_loop(&ready);
+
+	if (ready) {
+		set_current(ready);
+		if (!ready->plan.next) {
+			/* Call finish function immediately. */
+			if (ready->finish) {
+				errno = ready->plan.u.close.saved_errno;
+				ready->finish(ready, ready->finish_arg);
+				ready->finish = NULL;
+			}
+			backend_del_conn(ready);
+		} else {
+			/* Calls back in itself, via io_debug_io(). */
+			if (ready->plan.io(ready->fd.fd, &ready->plan) != 2)
+				abort();
+		}
+		set_current(NULL);
+	}
+
+	/* Return a do-nothing plan, so backend_plan_changed in
+	 * io_ready doesn't do anything (it's already been called). */
+	return io_idle_();
+}
+
+int io_debug_io(int ret)
+{
+	/* Cache it for debugging; current changes. */
+	struct io_conn *conn = current;
+	int saved_errno = errno;
+
+	if (!doing_debug_on(conn))
+		return ret;
+
+	/* These will all go linearly through the io_debug() path above. */
+	switch (ret) {
+	case -1:
+		/* This will call io_debug above. */
+		errno = saved_errno;
+		io_close();
+		break;
+	case 0: /* Keep going with plan. */
+		io_debug(conn->plan);
+		break;
+	case 1: /* Done: get next plan. */
+		if (timeout_active(conn))
+			backend_del_timeout(conn);
+		conn->plan.next(conn, conn->plan.next_arg);
+		break;
+	default:
+		abort();
+	}
+
+	/* Normally-invalid value, used for sanity check. */
+	return 2;
 }
 }
 
 
 static void debug_io_wake(struct io_conn *conn)
 static void debug_io_wake(struct io_conn *conn)
@@ -173,11 +227,11 @@ static int do_write(int fd, struct io_plan *plan)
 {
 {
 	ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
 	ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
 	if (ret < 0)
 	if (ret < 0)
-		return -1;
+		return io_debug_io(-1);
 
 
 	plan->u.write.buf += ret;
 	plan->u.write.buf += ret;
 	plan->u.write.len -= ret;
 	plan->u.write.len -= ret;
-	return (plan->u.write.len == 0);
+	return io_debug_io(plan->u.write.len == 0);
 }
 }
 
 
 /* Queue some data to be written. */
 /* Queue some data to be written. */
@@ -202,11 +256,11 @@ static int do_read(int fd, struct io_plan *plan)
 {
 {
 	ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
 	ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
 	if (ret <= 0)
 	if (ret <= 0)
-		return -1;
+		return io_debug_io(-1);
 
 
 	plan->u.read.buf += ret;
 	plan->u.read.buf += ret;
 	plan->u.read.len -= ret;
 	plan->u.read.len -= ret;
-	return (plan->u.read.len == 0);
+	return io_debug_io(plan->u.read.len == 0);
 }
 }
 
 
 /* Queue a request to read into a buffer. */
 /* Queue a request to read into a buffer. */
@@ -231,10 +285,10 @@ static int do_read_partial(int fd, struct io_plan *plan)
 {
 {
 	ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
 	ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
 	if (ret <= 0)
 	if (ret <= 0)
-		return -1;
+		return io_debug_io(-1);
 
 
 	*plan->u.readpart.lenp = ret;
 	*plan->u.readpart.lenp = ret;
-	return 1;
+	return io_debug_io(1);
 }
 }
 
 
 /* Queue a partial request to read into a buffer. */
 /* Queue a partial request to read into a buffer. */
@@ -259,10 +313,10 @@ static int do_write_partial(int fd, struct io_plan *plan)
 {
 {
 	ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
 	ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
 	if (ret < 0)
 	if (ret < 0)
-		return -1;
+		return io_debug_io(-1);
 
 
 	*plan->u.writepart.lenp = ret;
 	*plan->u.writepart.lenp = ret;
-	return 1;
+	return io_debug_io(1);
 }
 }
 
 
 /* Queue a partial write request. */
 /* Queue a partial write request. */
@@ -313,23 +367,21 @@ void io_wake_(struct io_conn *conn, struct io_plan plan)
 
 
 void io_ready(struct io_conn *conn)
 void io_ready(struct io_conn *conn)
 {
 {
+	set_current(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);
 		conn->plan = io_close();
 		conn->plan = io_close();
 		backend_plan_changed(conn);
 		backend_plan_changed(conn);
-		set_current(NULL);
 		break;
 		break;
 	case 0: /* Keep going with plan. */
 	case 0: /* Keep going with plan. */
 		break;
 		break;
 	case 1: /* Done: get next plan. */
 	case 1: /* Done: get next plan. */
-		set_current(conn);
 		if (timeout_active(conn))
 		if (timeout_active(conn))
 			backend_del_timeout(conn);
 			backend_del_timeout(conn);
 		conn->plan = conn->plan.next(conn, conn->plan.next_arg);
 		conn->plan = conn->plan.next(conn, conn->plan.next_arg);
 		backend_plan_changed(conn);
 		backend_plan_changed(conn);
-		set_current(NULL);
 	}
 	}
+	set_current(NULL);
 }
 }
 
 
 /* Close the connection, we're done. */
 /* Close the connection, we're done. */

+ 13 - 0
ccan/io/io_plan.h

@@ -80,6 +80,15 @@ extern bool (*io_debug_conn)(struct io_conn *conn);
  */
  */
 struct io_plan io_debug(struct io_plan plan);
 struct io_plan io_debug(struct io_plan plan);
 
 
+/**
+ * io_debug_io - return from function which actually does I/O.
+ *
+ * This determines if we are debugging the current connection: if so,
+ * it immediately sets the next function and calls into io_loop() to
+ * create a linear call chain.
+ */
+int io_debug_io(int ret);
+
 /**
 /**
  * io_plan_no_debug - mark the next plan not to be called immediately.
  * io_plan_no_debug - mark the next plan not to be called immediately.
  *
  *
@@ -99,6 +108,10 @@ static inline struct io_plan io_debug(struct io_plan plan)
 {
 {
 	return plan;
 	return plan;
 }
 }
+static inline int io_debug_io(int ret)
+{
+	return ret;
+}
 #define io_plan_no_debug() (void)0
 #define io_plan_no_debug() (void)0
 #endif
 #endif
 
 

+ 31 - 8
ccan/io/poll.c

@@ -172,7 +172,7 @@ bool add_duplex(struct io_conn *c)
 	return true;
 	return true;
 }
 }
 
 
-static void del_conn(struct io_conn *conn)
+void backend_del_conn(struct io_conn *conn)
 {
 {
 	if (conn->finish) {
 	if (conn->finish) {
 		errno = conn->plan.u.close.saved_errno;
 		errno = conn->plan.u.close.saved_errno;
@@ -189,6 +189,7 @@ static void del_conn(struct io_conn *conn)
 	} else
 	} else
 		del_fd(&conn->fd);
 		del_fd(&conn->fd);
 	num_closing--;
 	num_closing--;
+	free_conn(conn);
 }
 }
 
 
 void del_listener(struct io_listener *l)
 void del_listener(struct io_listener *l)
@@ -213,7 +214,7 @@ static void accept_conn(struct io_listener *l)
 }
 }
 
 
 /* It's OK to miss some, as long as we make progress. */
 /* It's OK to miss some, as long as we make progress. */
-static void finish_conns(void)
+static bool finish_conns(struct io_conn **ready)
 {
 {
 	unsigned int i;
 	unsigned int i;
 
 
@@ -228,12 +229,16 @@ static void finish_conns(void)
 		c = (void *)fds[i];
 		c = (void *)fds[i];
 		for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
 		for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
 			if (!c->plan.next) {
 			if (!c->plan.next) {
-				del_conn(c);
-				free_conn(c);
+				if (doing_debug_on(c) && ready) {
+					*ready = c;
+					return true;
+				}
+				backend_del_conn(c);
 				i--;
 				i--;
 			}
 			}
 		}
 		}
 	}
 	}
+	return false;
 }
 }
 
 
 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
@@ -253,7 +258,7 @@ void backend_del_timeout(struct io_conn *conn)
 }
 }
 
 
 /* This is the main loop. */
 /* This is the main loop. */
-void *io_loop(void)
+void *do_io_loop(struct io_conn **ready)
 {
 {
 	void *ret;
 	void *ret;
 
 
@@ -291,7 +296,9 @@ void *io_loop(void)
 		}
 		}
 
 
 		if (num_closing) {
 		if (num_closing) {
-			finish_conns();
+			/* If this finishes a debugging con, return now. */
+			if (finish_conns(ready))
+				return NULL;
 			/* Could have started/finished more. */
 			/* Could have started/finished more. */
 			continue;
 			continue;
 		}
 		}
@@ -327,6 +334,11 @@ void *io_loop(void)
 				if (c->duplex) {
 				if (c->duplex) {
 					int mask = c->duplex->plan.pollflag;
 					int mask = c->duplex->plan.pollflag;
 					if (events & mask) {
 					if (events & mask) {
+						if (doing_debug_on(c->duplex)
+							&& ready) {
+							*ready = c->duplex;
+							return NULL;
+						}
 						io_ready(c->duplex);
 						io_ready(c->duplex);
 						events &= ~mask;
 						events &= ~mask;
 						/* debug can recurse;
 						/* debug can recurse;
@@ -337,6 +349,10 @@ void *io_loop(void)
 							continue;
 							continue;
 					}
 					}
 				}
 				}
+				if (doing_debug_on(c) && ready) {
+					*ready = c;
+					return NULL;
+				}
 				io_ready(c);
 				io_ready(c);
 				/* debug can recurse; anything can change. */
 				/* debug can recurse; anything can change. */
 				if (doing_debug())
 				if (doing_debug())
@@ -354,8 +370,10 @@ void *io_loop(void)
 		}
 		}
 	}
 	}
 
 
-	while (num_closing)
-		finish_conns();
+	while (num_closing && !io_loop_return) {
+		if (finish_conns(ready))
+			return NULL;
+	}
 
 
 	ret = io_loop_return;
 	ret = io_loop_return;
 	io_loop_return = NULL;
 	io_loop_return = NULL;
@@ -363,3 +381,8 @@ void *io_loop(void)
 	io_loop_exit();
 	io_loop_exit();
 	return ret;
 	return ret;
 }
 }
+
+void *io_loop(void)
+{
+	return do_io_loop(NULL);
+}

+ 4 - 4
ccan/io/test/run-17-homemade-io.c

@@ -41,7 +41,7 @@ static int do_read_packet(int fd, struct io_plan *plan)
 		ok1(pkt->state == 2);
 		ok1(pkt->state == 2);
 		pkt->state++;
 		pkt->state++;
 		if (pkt->len == 0)
 		if (pkt->len == 0)
-			return 1;
+			return io_debug_io(1);
 		if (!pkt->contents && !(pkt->contents = malloc(pkt->len)))
 		if (!pkt->contents && !(pkt->contents = malloc(pkt->len)))
 			goto fail;
 			goto fail;
 		else {
 		else {
@@ -58,12 +58,12 @@ static int do_read_packet(int fd, struct io_plan *plan)
 	plan->u.ptr_len.len += ret;
 	plan->u.ptr_len.len += ret;
 
 
 	/* Finished? */
 	/* Finished? */
-	return (plan->u.ptr_len.len >= sizeof(pkt->len)
-		&& plan->u.ptr_len.len == pkt->len + sizeof(pkt->len));
+	return io_debug_io(plan->u.ptr_len.len >= sizeof(pkt->len)
+			   && plan->u.ptr_len.len == pkt->len + sizeof(pkt->len));
 
 
 fail:
 fail:
 	free(pkt->contents);
 	free(pkt->contents);
-	return -1;
+	return io_debug_io(-1);
 }
 }
 
 
 static struct io_plan io_read_packet(struct packet *pkt,
 static struct io_plan io_read_packet(struct packet *pkt,