Commit 6109a0a6 authored by Rusty Russell's avatar Rusty Russell

ccan/io: eliminate dir argument from io_wait and io_always.

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: default avatarRusty Russell <rusty@rustcorp.com.au>
parent cdffdf5d
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
* *
* // No room? Wait for writer * // No room? Wait for writer
* if (b->end == sizeof(b->buf)) * 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, * return io_read_partial(c, b->buf + b->end, sizeof(b->buf) - b->end,
* &b->rlen, read_in, b); * &b->rlen, read_in, b);
...@@ -71,7 +71,7 @@ ...@@ -71,7 +71,7 @@
* if (b->end == b->start) { * if (b->end == b->start) {
* if (b->finished) * if (b->finished)
* return io_close(c); * 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, * return io_write_partial(c, b->buf + b->start, b->end - b->start,
......
...@@ -124,11 +124,17 @@ static struct io_plan *set_always(struct io_conn *conn, ...@@ -124,11 +124,17 @@ static struct io_plan *set_always(struct io_conn *conn,
} }
struct io_plan *io_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 *), struct io_plan *(*next)(struct io_conn *, void *),
void *arg) 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); assert(next);
set_always(conn, plan, next, arg); set_always(conn, plan, next, arg);
...@@ -320,11 +326,18 @@ struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr, ...@@ -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, 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 *), struct io_plan *(*next)(struct io_conn *, void *),
void *arg) 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); assert(next);
...@@ -417,7 +430,7 @@ void io_break(const void *ret) ...@@ -417,7 +430,7 @@ void io_break(const void *ret)
struct io_plan *io_never(struct io_conn *conn) 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) int io_conn_fd(const struct io_conn *conn)
......
...@@ -6,11 +6,6 @@ ...@@ -6,11 +6,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
enum io_direction {
IO_IN,
IO_OUT
};
/** /**
* struct io_plan - a plan for input or output. * struct io_plan - a plan for input or output.
* *
...@@ -340,7 +335,6 @@ struct io_plan *io_write_partial_(struct io_conn *conn, ...@@ -340,7 +335,6 @@ struct io_plan *io_write_partial_(struct io_conn *conn,
/** /**
* io_always - plan to immediately call next callback * io_always - plan to immediately call next callback
* @conn: the connection that plan is for. * @conn: the connection that plan is for.
* @dir: IO_IN or IO_OUT
* @next: function to call. * @next: function to call.
* @arg: @next argument * @arg: @next argument
* *
...@@ -352,16 +346,16 @@ struct io_plan *io_write_partial_(struct io_conn *conn, ...@@ -352,16 +346,16 @@ struct io_plan *io_write_partial_(struct io_conn *conn,
* void *unused) * void *unused)
* { * {
* // Silly example: close on next time around loop. * // 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) \ #define io_always(conn, next, arg) \
io_always_((conn), dir, typesafe_cb_preargs(struct io_plan *, void *, \ io_always_((conn), typesafe_cb_preargs(struct io_plan *, void *, \
(next), (arg), \ (next), (arg), \
struct io_conn *), \ struct io_conn *), \
(arg)) (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 *), struct io_plan *(*next)(struct io_conn *, void *),
void *arg); void *arg);
...@@ -413,7 +407,6 @@ struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr, ...@@ -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. * io_wait - leave a plan idle until something wakes us.
* @conn: the connection that plan is for. * @conn: the connection that plan is for.
* @waitaddr: the address to wait on. * @waitaddr: the address to wait on.
* @dir: IO_IN or IO_OUT
* @next: function to call after waiting. * @next: function to call after waiting.
* @arg: @next argument * @arg: @next argument
* *
...@@ -424,18 +417,18 @@ struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr, ...@@ -424,18 +417,18 @@ struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
* // Silly example to wait then close. * // Silly example to wait then close.
* static struct io_plan *wait(struct io_conn *conn, void *b) * 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) \ #define io_wait(conn, waitaddr, next, arg) \
io_wait_((conn), (waitaddr), (dir), \ io_wait_((conn), (waitaddr), \
typesafe_cb_preargs(struct io_plan *, void *, \ typesafe_cb_preargs(struct io_plan *, void *, \
(next), (arg), \ (next), (arg), \
struct io_conn *), \ struct io_conn *), \
(arg)) (arg))
struct io_plan *io_wait_(struct io_conn *conn, 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 *), struct io_plan *(*next)(struct io_conn *, void *),
void *arg); void *arg);
......
...@@ -27,6 +27,11 @@ enum io_plan_status { ...@@ -27,6 +27,11 @@ enum io_plan_status {
IO_CLOSING IO_CLOSING
}; };
enum io_direction {
IO_IN,
IO_OUT
};
/** /**
* struct io_plan - one half of I/O to do * struct io_plan - one half of I/O to do
* @status: the status of this plan. * @status: the status of this plan.
......
...@@ -71,7 +71,7 @@ static struct io_plan *init_idle(struct io_conn *conn, struct data *d) ...@@ -71,7 +71,7 @@ static struct io_plan *init_idle(struct io_conn *conn, struct data *d)
ok1(fd2 >= 0); ok1(fd2 >= 0);
io_set_finish(io_new_conn(NULL, fd2, init_waker, d), finish_waker, d); 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) static int make_listen_fd(const char *port, struct addrinfo **info)
......
...@@ -15,7 +15,7 @@ static struct io_plan *read_in(struct io_conn *conn, char *buf) ...@@ -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) 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) static struct io_plan *wake_and_close(struct io_conn *conn, char *buf)
......
...@@ -27,7 +27,7 @@ static struct io_plan *init_writer(struct io_conn *conn, struct io_conn *wakeme) ...@@ -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) 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) int main(void)
......
...@@ -34,7 +34,7 @@ static struct io_plan *poke_writer(struct io_conn *conn, struct buffer *buf) ...@@ -34,7 +34,7 @@ static struct io_plan *poke_writer(struct io_conn *conn, struct buffer *buf)
io_wake(&buf->writer); io_wake(&buf->writer);
/* I'll wait until you wake me. */ /* 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) 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) ...@@ -52,12 +52,12 @@ static struct io_plan *poke_reader(struct io_conn *conn, struct buffer *buf)
return io_close(conn); return io_close(conn);
/* I'll wait until you tell me to write. */ /* 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) 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]; static struct buffer buf[NUM];
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
static struct io_plan *setup_waiter(struct io_conn *conn, int *status) 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) int main(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment