1 #include <ccan/io/io.h>
2 /* Include the C files directly. */
3 #include <ccan/io/poll.c>
4 #include <ccan/io/io.c>
5 #include <ccan/tap/tap.h>
10 #define NUM_ITERS 1000
14 struct io_conn *reader, *writer;
18 static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf);
19 static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf);
21 static struct io_plan plan_read(struct io_conn *conn, struct buffer *buf)
23 assert(conn == buf->reader);
25 return io_read(&buf->buf, sizeof(buf->buf), poke_writer, buf);
28 static struct io_plan plan_write(struct io_conn *conn, struct buffer *buf)
30 assert(conn == buf->writer);
32 return io_write(&buf->buf, sizeof(buf->buf), poke_reader, buf);
35 static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf)
37 assert(conn == buf->reader);
39 if (buf->iters == NUM_ITERS)
40 return io_close(conn, NULL);
43 io_wake(buf->writer, plan_write, buf);
45 /* I'll wait until you wake me. */
49 static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf)
51 assert(conn == buf->writer);
53 io_wake(buf->reader, plan_read, buf);
55 if (++buf->iters == NUM_ITERS)
56 return io_close(conn, NULL);
58 /* I'll wait until you tell me to write. */
62 static struct io_plan reader(struct io_conn *conn, struct buffer *buf)
64 assert(conn == buf->reader);
66 /* Wait for writer to tell us to read. */
70 static struct buffer buf[NUM];
75 int fds[2], last_read, last_write;
83 for (i = 1; i < NUM; i++) {
86 memset(buf[i].buf, i, sizeof(buf[i].buf));
87 sprintf(buf[i].buf, "%i-%i", i, i);
89 buf[i].reader = io_new_conn(last_read, reader, NULL, &buf[i]);
92 buf[i].writer = io_new_conn(fds[1], plan_write, NULL, &buf[i]);
100 /* Last one completes the cirle. */
102 sprintf(buf[i].buf, "%i-%i", i, i);
103 buf[i].reader = io_new_conn(last_read, reader, NULL, &buf[i]);
105 buf[i].writer = io_new_conn(last_write, plan_write, NULL, &buf[i]);
108 /* They should eventually exit */
109 ok1(io_loop() == NULL);
111 for (i = 0; i < NUM; i++) {
112 char b[sizeof(buf[0].buf)];
113 memset(b, i, sizeof(b));
114 sprintf(b, "%i-%i", i, i);
115 ok1(memcmp(b, buf[(i + NUM_ITERS) % NUM].buf, sizeof(b)) == 0);
118 /* This exits depending on whether all tests passed */
119 return exit_status();