]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/io.h
io: add io_is_idle().
[ccan] / ccan / io / io.h
index 067a69c1dec441a752508dfe50ee8f83b8421466..df0764bd17e1ff7f50633ffe91a45de4c02c6a22 100644 (file)
@@ -394,7 +394,7 @@ bool io_timeout_(struct io_conn *conn, struct timespec ts,
  * to have two connections for the same fd, and use one for read
  * operations and one for write.
  *
- * You must io_close() both of them to close the fd.
+ * Returning io_close() on one will close both fds!
  *
  * Example:
  *     static void setup_read_write(int fd,
@@ -431,6 +431,21 @@ struct io_conn *io_duplex_(struct io_conn *conn, struct io_plan plan);
 #define io_wake(conn, plan) (io_plan_no_debug(), io_wake_((conn), (plan)))
 void io_wake_(struct io_conn *conn, struct io_plan plan);
 
+/**
+ * io_is_idle - is a connection idle?
+ *
+ * This can be useful for complex protocols, eg. where you want a connection
+ * to send something, so you queue it and wake it if it's idle.
+ *
+ * Example:
+ *     struct io_conn *sleeper;
+ *     sleeper = io_new_conn(open("/dev/null", O_RDONLY), io_idle());
+ *
+ *     assert(io_is_idle(sleeper));
+ *     io_wake(sleeper, io_write("junk", 4, io_close_cb, NULL));
+ */
+bool io_is_idle(const struct io_conn *conn);
+
 /**
  * io_break - return from io_loop()
  * @ret: non-NULL value to return from io_loop().
@@ -491,6 +506,14 @@ struct io_plan io_close_cb(struct io_conn *, void *unused);
  */
 void *io_loop(void);
 
+/**
+ * io_conn_fd - get the fd from a connection.
+ * @conn: the connection.
+ *
+ * Sometimes useful, eg for getsockname().
+ */
+int io_conn_fd(const struct io_conn *conn);
+
 /**
  * io_set_alloc - set alloc/realloc/free function for io to use.
  * @allocfn: allocator function