]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/io.c
io: io_always, and zero-length operations support.
[ccan] / ccan / io / io.c
index f45589c0fac71888b1be312debcb3ec04597b0bb..734cb3939636acbf0922362915c5c804bcc2cb97 100644 (file)
@@ -8,7 +8,6 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <assert.h>
-#include <poll.h>
 #include <unistd.h>
 #include <fcntl.h>
 
@@ -94,6 +93,10 @@ int io_debug_io(int ret)
        case 1: /* Done: get next plan. */
                if (timeout_active(conn))
                        backend_del_timeout(conn);
+               /* In case they call io_duplex, clear our poll flags so
+                * both sides don't seem to be both doing read or write
+                * (See assert(!mask || pfd->events != mask) in poll.c) */
+               conn->plan.pollflag = 0;
                conn->plan.next(conn, conn->plan.next_arg);
                break;
        default:
@@ -228,6 +231,26 @@ bool io_timeout_(struct io_conn *conn, struct timespec ts,
        return true;
 }
 
+/* Always done: call the next thing. */
+static int do_always(int fd, struct io_plan *plan)
+{
+       return 1;
+}
+
+struct io_plan io_always_(struct io_plan (*cb)(struct io_conn *, void *),
+                         void *arg)
+{
+       struct io_plan plan;
+
+       assert(cb);
+       plan.io = do_always;
+       plan.next = cb;
+       plan.next_arg = arg;
+       plan.pollflag = POLLALWAYS;
+
+       return plan;
+}
+
 /* Returns true if we're finished. */
 static int do_write(int fd, struct io_plan *plan)
 {
@@ -248,6 +271,10 @@ struct io_plan io_write_(const void *data, size_t len,
        struct io_plan plan;
 
        assert(cb);
+
+       if (len == 0)
+               return io_always_(cb, arg);
+
        plan.u1.const_vp = data;
        plan.u2.s = len;
        plan.io = do_write;
@@ -277,11 +304,16 @@ struct io_plan io_read_(void *data, size_t len,
        struct io_plan plan;
 
        assert(cb);
+
+       if (len == 0)
+               return io_always_(cb, arg);
+
        plan.u1.cp = data;
        plan.u2.s = len;
        plan.io = do_read;
        plan.next = cb;
        plan.next_arg = arg;
+
        plan.pollflag = POLLIN;
 
        return plan;
@@ -305,6 +337,10 @@ struct io_plan io_read_partial_(void *data, size_t *len,
        struct io_plan plan;
 
        assert(cb);
+
+       if (*len == 0)
+               return io_always_(cb, arg);
+
        plan.u1.cp = data;
        plan.u2.vp = len;
        plan.io = do_read_partial;
@@ -333,6 +369,10 @@ struct io_plan io_write_partial_(const void *data, size_t *len,
        struct io_plan plan;
 
        assert(cb);
+
+       if (*len == 0)
+               return io_always_(cb, arg);
+
        plan.u1.const_vp = data;
        plan.u2.vp = len;
        plan.io = do_write_partial;
@@ -408,6 +448,11 @@ struct io_plan io_idle_(void)
        return plan;
 }
 
+bool io_is_idle(const struct io_conn *conn)
+{
+       return conn->plan.io == NULL;
+}
+
 void io_wake_(struct io_conn *conn, struct io_plan plan)
 
 {
@@ -426,6 +471,10 @@ void io_wake_(struct io_conn *conn, struct io_plan plan)
 
 void io_ready(struct io_conn *conn)
 {
+       /* Beware io_close_other! */
+       if (!conn->plan.next)
+               return;
+
        set_current(conn);
        switch (conn->plan.io(conn->fd.fd, &conn->plan)) {
        case -1: /* Failure means a new plan: close up. */
@@ -437,6 +486,10 @@ void io_ready(struct io_conn *conn)
        case 1: /* Done: get next plan. */
                if (timeout_active(conn))
                        backend_del_timeout(conn);
+               /* In case they call io_duplex, clear our poll flags so
+                * both sides don't seem to be both doing read or write
+                * (See assert(!mask || pfd->events != mask) in poll.c) */
+               conn->plan.pollflag = 0;
                conn->plan = conn->plan.next(conn, conn->plan.next_arg);
                backend_plan_changed(conn);
        }
@@ -461,6 +514,12 @@ struct io_plan io_close_cb(struct io_conn *conn, void *arg)
        return io_close();
 }
 
+void io_close_other(struct io_conn *conn)
+{
+       conn->plan = io_close_();
+       backend_plan_changed(conn);
+}
+
 /* Exit the loop, returning this (non-NULL) arg. */
 struct io_plan io_break_(void *ret, struct io_plan plan)
 {