]> git.ozlabs.org Git - ccan/commitdiff
ccan/io: flatten debug callchain further.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 14 Oct 2013 11:03:07 +0000 (21:33 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 14 Oct 2013 11:03:07 +0000 (21:33 +1030)
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>
ccan/io/backend.h
ccan/io/io.c
ccan/io/io_plan.h
ccan/io/poll.c
ccan/io/test/run-17-homemade-io.c

index df168d9f2b696202265de7bec1c96f35e1b65bca..30a338f77a73958eaf83acad62eb87b027d27025 100644 (file)
@@ -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
 static inline void set_current(struct io_conn *conn)
 {
 }
 }
 #else
 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)
 {
        return false;
 static inline bool doing_debug(void)
 {
        return false;
@@ -74,6 +82,8 @@ void del_listener(struct io_listener *l);
 void backend_plan_changed(struct io_conn *conn);
 void backend_add_timeout(struct io_conn *conn, struct timespec ts);
 void backend_del_timeout(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_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 */
index a4489ee8855c5a67fce5d746672342b345c72dc5..ddcd502f367d3afdbbb3562b91f883be13072ece 100644 (file)
@@ -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) {
                io_plan_nodebug = false;
                return plan;
        }
 
        if (io_plan_nodebug) {
                io_plan_nodebug = false;
                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;
        current->plan = plan;
        backend_plan_changed(current);
 
        /* Call back into the loop immediately. */
 
        io_debug_wakeup = false;
        current->plan = plan;
        backend_plan_changed(current);
 
        /* 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);
        if (ret < 0)
 {
        ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
        if (ret < 0)
-               return -1;
+               return io_debug_io(-1);
 
        plan->u.write.buf += ret;
        plan->u.write.len -= ret;
 
        plan->u.write.buf += 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);
        if (ret <= 0)
 {
        ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
        if (ret <= 0)
-               return -1;
+               return io_debug_io(-1);
 
        plan->u.read.buf += ret;
        plan->u.read.len -= ret;
 
        plan->u.read.buf += 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);
        if (ret <= 0)
 {
        ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
        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);
        if (ret < 0)
 {
        ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
        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)) {
        case -1: /* Failure means a new plan: close up. */
        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();
                backend_plan_changed(conn);
                conn->plan = io_close();
                backend_plan_changed(conn);
-               set_current(NULL);
                break;
        case 0: /* Keep going with plan. */
                break;
        case 1: /* Done: get next plan. */
                break;
        case 0: /* Keep going with plan. */
                break;
        case 1: /* Done: get next plan. */
-               set_current(conn);
                if (timeout_active(conn))
                        backend_del_timeout(conn);
                conn->plan = conn->plan.next(conn, conn->plan.next_arg);
                backend_plan_changed(conn);
                if (timeout_active(conn))
                        backend_del_timeout(conn);
                conn->plan = conn->plan.next(conn, conn->plan.next_arg);
                backend_plan_changed(conn);
-               set_current(NULL);
        }
        }
+       set_current(NULL);
 }
 
 /* Close the connection, we're done. */
 }
 
 /* Close the connection, we're done. */
index 86be1d70d080721c1d490ae9c0e680e242470918..b855e717921df3df528eca37b4c4947e0206bd70 100644 (file)
@@ -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
 #endif
 
 #define io_plan_no_debug() (void)0
 #endif
 
index 3ed6321bc7f4a0309ca2c2a55f41ab17ae328511..31a56600fe487b836e8bf1b8bdb88ecfa75953e0 100644 (file)
@@ -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) {
                errno = conn->plan.u.close.saved_errno;
 {
        if (conn->finish) {
                errno = conn->plan.u.close.saved_errno;
@@ -189,6 +189,7 @@ static void del_conn(struct io_conn *conn)
        } else
                del_fd(&conn->fd);
        num_closing--;
        } else
                del_fd(&conn->fd);
        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];
                for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
                        if (!c->plan.next) {
                c = (void *)fds[i];
                for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
                        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. */
                        continue;
                }
                        /* Could have started/finished more. */
                        continue;
                }
@@ -327,6 +334,11 @@ void *io_loop(void)
                                if (c->duplex) {
                                        int mask = c->duplex->plan.pollflag;
                                        if (events & mask) {
                                if (c->duplex) {
                                        int mask = c->duplex->plan.pollflag;
                                        if (events & mask) {
+                                               if (doing_debug_on(c->duplex)
+                                                       && ready) {
+                                                       *ready = c->duplex;
+                                                       return NULL;
+                                               }
                                                io_ready(c->duplex);
                                                events &= ~mask;
                                                /* debug can recurse;
                                                io_ready(c->duplex);
                                                events &= ~mask;
                                                /* 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);
                                /* debug can recurse; anything can change. */
                                if (doing_debug())
                                io_ready(c);
                                /* debug can recurse; anything can change. */
                                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;
        io_loop_return = NULL;
 
        ret = io_loop_return;
        io_loop_return = NULL;
@@ -363,3 +381,8 @@ void *io_loop(void)
        io_loop_exit();
        return ret;
 }
        io_loop_exit();
        return ret;
 }
+
+void *io_loop(void)
+{
+       return do_io_loop(NULL);
+}
index 00fbf2233c59c9132c3f1c74a40e630429a64bf5..b827713a789209f27bbac586ebfbe80aedfeed0c 100644 (file)
@@ -41,7 +41,7 @@ static int do_read_packet(int fd, struct io_plan *plan)
                ok1(pkt->state == 2);
                pkt->state++;
                if (pkt->len == 0)
                ok1(pkt->state == 2);
                pkt->state++;
                if (pkt->len == 0)
-                       return 1;
+                       return io_debug_io(1);
                if (!pkt->contents && !(pkt->contents = malloc(pkt->len)))
                        goto fail;
                else {
                if (!pkt->contents && !(pkt->contents = malloc(pkt->len)))
                        goto fail;
                else {
@@ -58,12 +58,12 @@ static int do_read_packet(int fd, struct io_plan *plan)
        plan->u.ptr_len.len += ret;
 
        /* Finished? */
        plan->u.ptr_len.len += ret;
 
        /* 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:
        free(pkt->contents);
 
 fail:
        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,