X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fio%2Fio.c;h=f298af70781d3fe90e606934614f5d06874e770f;hp=68b95e82467194f13e87302e907cda5020fe37db;hb=4f6d604ce616e70659b8494fd41ecd41e8fca30a;hpb=3f642347378afc9e1db1768d88c9f5b2baffe9ba diff --git a/ccan/io/io.c b/ccan/io/io.c index 68b95e82..f298af70 100644 --- a/ccan/io/io.c +++ b/ccan/io/io.c @@ -483,3 +483,37 @@ struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir, return plan; } + +bool io_flush_sync(struct io_conn *conn) +{ + struct io_plan *plan = &conn->plan[IO_OUT]; + bool ok; + + /* Not writing? Nothing to do. */ + if (plan->status != IO_POLLING) + return true; + + /* Synchronous please. */ + set_blocking(io_conn_fd(conn), true); + +again: + switch (plan->io(conn->fd.fd, &plan->arg)) { + case -1: + ok = false; + break; + /* Incomplete, try again. */ + case 0: + goto again; + case 1: + ok = true; + /* In case they come back. */ + set_always(conn, IO_OUT, plan->next, plan->next_arg); + break; + default: + /* IO should only return -1, 0 or 1 */ + abort(); + } + + set_blocking(io_conn_fd(conn), false); + return ok; +}