]> git.ozlabs.org Git - ccan/commitdiff
ccan/io: io_close_cb()
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 14 Oct 2013 11:02:57 +0000 (21:32 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 14 Oct 2013 11:02:57 +0000 (21:32 +1030)
Overloading io_close() as a callback is ugly: create an explicit
io_close_cb().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
18 files changed:
ccan/io/_info
ccan/io/benchmarks/run-loop.c
ccan/io/io.c
ccan/io/io.h
ccan/io/poll.c
ccan/io/test/run-01-start-finish.c
ccan/io/test/run-02-read.c
ccan/io/test/run-03-readpartial.c
ccan/io/test/run-04-writepartial.c
ccan/io/test/run-05-write.c
ccan/io/test/run-06-idle.c
ccan/io/test/run-07-break.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-12-bidir.c
ccan/io/test/run-15-timeout.c
ccan/io/test/run-17-homemade-io.c

index a96ee669eb4ae16fbb229f0c247ebd9beb67bb2f..8786272cb97723fba8dbf43310acb86f9b5a6302 100644 (file)
@@ -46,7 +46,7 @@
  * static void reader_exit(struct io_conn *c, struct stdin_buffer *b)
  * {
  *     assert(c == b->reader);
- *     io_wake(b->writer, io_close(b->writer, NULL));
+ *     io_wake(b->writer, io_close());
  *     b->reader = NULL;
  * }
  *
@@ -54,7 +54,7 @@
  * {
  *     assert(c == b->writer);
  *     if (!b->reader)
- *             return io_close(c, NULL);
+ *             return io_close();
  *     b->len = sizeof(b->inbuf);
  *     io_wake(b->reader, io_read_partial(b->inbuf, &b->len, wake_writer, b));
  *     return io_idle();
index b0d383e89b7c2310f0c5a6acf42babfc808d9f3e..ef01cf6a94663597dec0b4f7d0fe63bbe9c02f7a 100644 (file)
@@ -23,7 +23,7 @@ static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf)
        assert(conn == buf->reader);
 
        if (buf->iters == NUM_ITERS)
-               return io_close(conn, NULL);
+               return io_close();
 
        /* You write. */
        io_wake(buf->writer,
@@ -41,7 +41,7 @@ static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf)
                io_read(&buf->buf, sizeof(buf->buf), poke_writer, buf));
 
        if (++buf->iters == NUM_ITERS)
-               return io_close(conn, NULL);
+               return io_close();
 
        /* I'll wait until you tell me to write. */
        return io_idle();
index 1c0af6b97e05f0ac6018c0cc7cba7fa1e0ef08a9..352afe33e87638ed638729f7e56f91f37ea1db71 100644 (file)
@@ -275,8 +275,8 @@ struct io_plan io_idle(void)
 
        plan.pollflag = 0;
        plan.io = NULL;
-       /* Never called (overridded by io_wake), but NULL means closing */
-       plan.next = io_close;
+       /* Never called (overridden by io_wake), but NULL means closing */
+       plan.next = (void *)io_idle;
 
        io_plan_debug(&plan);
        return plan;
@@ -301,7 +301,7 @@ void io_ready(struct io_conn *conn)
        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(NULL, NULL);
+               conn->plan = io_close();
                backend_plan_changed(conn);
                set_current(NULL);
                break;
@@ -317,9 +317,8 @@ void io_ready(struct io_conn *conn)
        }
 }
 
-/* Useful next functions. */
 /* Close the connection, we're done. */
-struct io_plan io_close(struct io_conn *conn, void *arg)
+struct io_plan io_close(void)
 {
        struct io_plan plan;
 
@@ -331,6 +330,11 @@ struct io_plan io_close(struct io_conn *conn, void *arg)
        return plan;
 }
 
+struct io_plan io_close_cb(struct io_conn *conn, void *arg)
+{
+       return io_close();
+}
+
 /* Exit the loop, returning this (non-NULL) arg. */
 struct io_plan io_break_(void *ret, struct io_plan plan)
 {
index 105a90825dcea8caf53cdb1ec1ce25890acdf53e..8199bda9802050f747440f66ec1da97f987fcb47 100644 (file)
@@ -290,17 +290,20 @@ struct io_plan io_break_(void *ret, struct io_plan plan);
 /* FIXME: io_recvfrom/io_sendto */
 
 /**
- * io_close - terminate a connection.
- * @conn: any connection.
+ * io_close - plan to close a connection.
  *
- * The schedules a connection to be closed.  It can be done on any
- * connection, whether it has I/O queued or not (though that I/O may
- * be performed first).
+ * On return to io_loop, the connection will be closed.
+ */
+struct io_plan io_close(void);
+
+/**
+ * io_close_cb - helper callback to close a connection.
+ * @conn: the connection.
  *
- * It's common to 'return io_close(...)' from a @next function, but
- * io_close can also be used as an argument to io_next().
+ * This schedules a connection to be closed; designed to be used as
+ * a callback function.
  */
-struct io_plan io_close(struct io_conn *, void *unused);
+struct io_plan io_close_cb(struct io_conn *, void *unused);
 
 /**
  * io_loop - process fds until all closed on io_break.
index 85407f62a8e32bafef27bb735b31992b3c1d2c66..06e3b59104d0f4e4d9d010e86109f31d59af7c04 100644 (file)
@@ -341,11 +341,10 @@ void *io_loop(void)
                        } else if (events & POLLHUP) {
                                r--;
                                set_current(c);
-                               set_plan(c, io_close(c, NULL));
+                               set_plan(c, io_close());
                                if (c->duplex) {
                                        set_current(c->duplex);
-                                       set_plan(c->duplex,
-                                                io_close(c->duplex, NULL));
+                                       set_plan(c->duplex, io_close());
                                }
                        }
                }
index 297dbc6f9bd9a6ccb15d7af96fb3c49369099e90..a63baf7706c12970ed25b27e84c0cec76328c65d 100644 (file)
@@ -21,8 +21,7 @@ static void init_conn(int fd, int *state)
 {
        ok1(*state == 0);
        (*state)++;
-       io_set_finish(io_new_conn(fd, io_close(NULL, NULL)), finish_ok, state);
-
+       io_set_finish(io_new_conn(fd, io_close()), finish_ok, state);
 }
 
 static int make_listen_fd(const char *port, struct addrinfo **info)
index 65dbe4dca220874a57a73e0ebd7e0901fd472918..c7a7eaf1c338704ad42cbcd3c6f7796377521134 100644 (file)
@@ -28,7 +28,7 @@ static void init_conn(int fd, struct data *d)
        d->state++;
 
        io_set_finish(io_new_conn(fd,
-                                 io_read(d->buf, sizeof(d->buf), io_close, d)),
+                                 io_read(d->buf, sizeof(d->buf), io_close_cb, d)),
                      finish_ok, d);
 }
 
index d5411848b85673ea0808a4b5aad7e5f8d8924c6e..7ecccc7412cd2aa3f2b5342614a695168e5e46d9 100644 (file)
@@ -30,7 +30,7 @@ static void init_conn(int fd, struct data *d)
        d->bytes = sizeof(d->buf);
 
        io_set_finish(io_new_conn(fd,
-                                 io_read_partial(d->buf, &d->bytes, io_close, d)),
+                                 io_read_partial(d->buf, &d->bytes, io_close_cb, d)),
                      finish_ok, d);
 }
 
index c80238fe5004145244d87c978cd079e39cf524cb..8cfb2abeb16bb126c8db502ede3358fcf46d8ba2 100644 (file)
@@ -28,7 +28,7 @@ static void init_conn(int fd, struct data *d)
        ok1(d->state == 0);
        d->state++;
        io_set_finish(io_new_conn(fd,
-                                 io_write_partial(d->buf, &d->bytes, io_close, d)),
+                                 io_write_partial(d->buf, &d->bytes, io_close_cb, d)),
                                  finish_ok, d);
 }
 
index bcbc9cad119e3b82ccc0eb3e12f1838fc734792a..5319def8f2cc28c6921b2b611a2c99b013b1402d 100644 (file)
@@ -27,7 +27,8 @@ static void init_conn(int fd, struct data *d)
 {
        ok1(d->state == 0);
        d->state++;
-       io_set_finish(io_new_conn(fd, io_write(d->buf, d->bytes, io_close, d)),
+       io_set_finish(io_new_conn(fd, io_write(d->buf, d->bytes,
+                                              io_close_cb, d)),
                      finish_ok, d);
 }
 
index b32cbfe225c1e2bd5949509cd9d33558f1ce1712..51cca961a077416c47287a5b78d697cd5513d554 100644 (file)
@@ -24,7 +24,7 @@ static struct io_plan read_done(struct io_conn *conn, struct data *d)
 {
        ok1(d->state == 2 || d->state == 3);
        d->state++;
-       return io_close(conn, NULL);
+       return io_close();
 }
 
 static void finish_waker(struct io_conn *conn, struct data *d)
index 6fcaea341b58f1123ab72a322d3260dd71f53ba2..19cc6a8e662b393f8f672e3a46d1340d37e2a634 100644 (file)
@@ -19,7 +19,7 @@ static struct io_plan read_done(struct io_conn *conn, struct data *d)
 {
        ok1(d->state == 1);
        d->state++;
-       return io_close(conn, NULL);
+       return io_close();
 }
 
 static void finish_ok(struct io_conn *conn, struct data *d)
index 7463dd98c83e592525fc09b0a30ea250681fef20..b3840433410b7bfda4068293728c8050b2483856 100644 (file)
@@ -12,7 +12,7 @@ static struct io_plan timeout_wakeup(struct io_conn *conn, char *buf)
 {
        /* This kills the dummy connection. */
        close(fds2[1]);
-       return io_read(buf, 16, io_close, NULL);
+       return io_read(buf, 16, io_close_cb, NULL);
 }
 
 int main(void)
@@ -26,12 +26,13 @@ int main(void)
        ok1(pipe(fds) == 0);
 
        /* Write then close. */
-       io_new_conn(fds[1], io_write("hello there world", 16, io_close, NULL));
+       io_new_conn(fds[1], io_write("hello there world", 16,
+                                    io_close_cb, NULL));
        conn = io_new_conn(fds[0], io_idle());
 
        /* To avoid assert(num_waiting) */
        ok1(pipe(fds2) == 0);
-       io_new_conn(fds2[0], io_read(buf, 16, io_close, NULL));
+       io_new_conn(fds2[0], io_read(buf, 16, io_close_cb, NULL));
 
        /* After half a second, it will read. */
        io_timeout(conn, time_from_msec(500), timeout_wakeup, buf);
index 56d9a164accddbbd40ba290016a2486a008fd45d..b73139e4f4c7a256c1e00301dce63c67622c1869 100644 (file)
@@ -11,8 +11,8 @@ static char inbuf[8];
 
 static struct io_plan wake_it(struct io_conn *conn, struct io_conn *reader)
 {
-       io_wake(reader, io_read(inbuf, 8, io_close, NULL));
-       return io_close(conn, NULL);
+       io_wake(reader, io_read(inbuf, 8, io_close_cb, NULL));
+       return io_close();
 }
 
 int main(void)
index b13e1b4345a34997450b5d3ec62b064286fa0240..da59a3cc30ef0f6ebb734a24d755515f636b4971 100644 (file)
@@ -22,7 +22,7 @@ static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf)
        assert(conn == buf->reader);
 
        if (buf->iters == NUM_ITERS)
-               return io_close(conn, NULL);
+               return io_close();
 
        /* You write. */
        io_wake(buf->writer,
@@ -40,7 +40,7 @@ static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf)
                io_read(&buf->buf, sizeof(buf->buf), poke_writer, buf));
 
        if (++buf->iters == NUM_ITERS)
-               return io_close(conn, NULL);
+               return io_close();
 
        /* I'll wait until you tell me to write. */
        return io_idle();
index 32319232d707fe5b873d994825ed1d8e19d8ce11..1ab0a218d02a200e7918a079966d7eda941cd4c6 100644 (file)
@@ -25,7 +25,7 @@ static void finish_ok(struct io_conn *conn, struct data *d)
 static struct io_plan write_done(struct io_conn *conn, struct data *d)
 {
        d->state++;
-       return io_close(conn, NULL);
+       return io_close();
 }
 
 static void init_conn(int fd, struct data *d)
@@ -39,7 +39,7 @@ static void init_conn(int fd, struct data *d)
 
        memset(d->wbuf, 7, sizeof(d->wbuf));
 
-       conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close, d));
+       conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close_cb, d));
        io_set_finish(conn, finish_ok, d);
        conn = io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d));
        ok1(conn);
index a3e9526b165692ab8ae60b4e92e4a9adfcb14de2..6f92ec3a634848c0770fcd21016e56acfb082274 100644 (file)
@@ -23,7 +23,7 @@ static struct io_plan no_timeout(struct io_conn *conn, struct data *d)
 {
        ok1(d->state == 1);
        d->state++;
-       return io_close(conn, d);
+       return io_close();
 }
 
 static struct io_plan timeout(struct io_conn *conn, struct data *d)
@@ -31,7 +31,7 @@ static struct io_plan timeout(struct io_conn *conn, struct data *d)
        ok1(d->state == 1);
        d->state++;
        d->timed_out = true;
-       return io_close(conn, d);
+       return io_close();
 }
 
 static void finish_ok(struct io_conn *conn, struct data *d)
index de7ce7510d9915e4c2637a5e1dfb06b62aa773cb..07794fa98e125d66480af8e8be62d3f9a7b0e16c 100644 (file)
@@ -90,7 +90,7 @@ static void init_conn(int fd, struct packet *pkt)
        ok1(pkt->state == 0);
        pkt->state++;
 
-       io_set_finish(io_new_conn(fd, io_read_packet(pkt, io_close, pkt)),
+       io_set_finish(io_new_conn(fd, io_read_packet(pkt, io_close_cb, pkt)),
                      finish_ok, pkt);
 }