]> git.ozlabs.org Git - ccan/commitdiff
ccan/io: eliminate dir argument from io_wait and io_always.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 4 Aug 2014 08:11:21 +0000 (17:41 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 4 Aug 2014 08:11:21 +0000 (17:41 +0930)
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 <rusty@rustcorp.com.au>
ccan/io/_info
ccan/io/io.c
ccan/io/io.h
ccan/io/io_plan.h
ccan/io/test/run-06-idle.c
ccan/io/test/run-08-hangup-on-idle.c
ccan/io/test/run-08-read-after-hangup.c
ccan/io/test/run-10-many.c
ccan/io/test/run-13-all-idle.c

index 6dd633abdfc4db651112c13c443f71262d44c91e..7fc6ac61c1e29a3800798315a73bce4b8d856d85 100644 (file)
@@ -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,
index 52f4368ca11533f739fea3bf480571a80d09af90..2832c513466f646cdca3c8d8ad7857f5175bb791 100644 (file)
@@ -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)
index e2792844d885da16d5049514bc665ea0911519c1..00b9b33db5e7884bfc969acee67247818d0cfa75 100644 (file)
@@ -6,11 +6,6 @@
 #include <stdbool.h>
 #include <unistd.h>
 
-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);
 
index 0b3c27b1a5a137f223f953bf163c3034945761d8..21a1921734f41e83d2ed836e157e4849496fb8ad 100644 (file)
@@ -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.
index e71f4d8b509a49c3c92b2b2f154a98c229546aa4..d01cb31fff4da1393e6d4071bc5b059ad7ff0326 100644 (file)
@@ -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)
index c1ff1e6eec9745a730a2b5677adfa91824c5d306..52309b4bf018045830f6a5a7f02ef6abf0921151 100644 (file)
@@ -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)
index f9ed267d80ce1effc7c555073371ef49cfff6966..7b6731ec9da2fa58fcffc242ca1214abe8d36e37 100644 (file)
@@ -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)
index b70310dd865ed3eaa0047216dbad941e6169a19a..fc48ecb56cf7d75ba3f65750ed4c08849db53b6e 100644 (file)
@@ -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];
index f8ee97f805ca791ffdc5c9417176e8d8de2d5656..3701472b18258b74a0a0d02626c61efdd395e374 100644 (file)
@@ -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)