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_op *poke_writer(struct io_conn *conn, struct buffer *buf);
19 static struct io_op *poke_reader(struct io_conn *conn, struct buffer *buf);
21 static struct io_op *do_read(struct io_conn *conn, struct buffer *buf)
23 assert(conn == buf->reader);
25 return io_read(&buf->buf, sizeof(buf->buf),
26 io_next(conn, poke_writer, buf));
29 static struct io_op *do_write(struct io_conn *conn, struct buffer *buf)
31 assert(conn == buf->writer);
33 return io_write(&buf->buf, sizeof(buf->buf),
34 io_next(conn, poke_reader, buf));
37 static struct io_op *poke_writer(struct io_conn *conn, struct buffer *buf)
39 assert(conn == buf->reader);
41 if (buf->iters == NUM_ITERS)
42 return io_close(conn, NULL);
45 io_wake(buf->writer, do_write, buf);
47 /* I'll wait until you wake me. */
51 static struct io_op *poke_reader(struct io_conn *conn, struct buffer *buf)
53 assert(conn == buf->writer);
55 io_wake(buf->reader, do_read, buf);
57 if (++buf->iters == NUM_ITERS)
58 return io_close(conn, NULL);
60 /* I'll wait until you tell me to write. */
64 static struct io_op *reader(struct io_conn *conn, struct buffer *buf)
66 assert(conn == buf->reader);
68 /* Wait for writer to tell us to read. */
72 static struct buffer buf[NUM];
77 int fds[2], last_read, last_write;
85 for (i = 1; i < NUM; i++) {
88 memset(buf[i].buf, i, sizeof(buf[i].buf));
89 sprintf(buf[i].buf, "%i-%i", i, i);
91 buf[i].reader = io_new_conn(last_read, reader, NULL, &buf[i]);
94 buf[i].writer = io_new_conn(fds[1], do_write, NULL, &buf[i]);
102 /* Last one completes the cirle. */
104 sprintf(buf[i].buf, "%i-%i", i, i);
105 buf[i].reader = io_new_conn(last_read, reader, NULL, &buf[i]);
107 buf[i].writer = io_new_conn(last_write, do_write, NULL, &buf[i]);
110 /* They should eventually exit */
111 ok1(io_loop() == NULL);
113 for (i = 0; i < NUM; i++) {
114 char b[sizeof(buf[0].buf)];
115 memset(b, i, sizeof(b));
116 sprintf(b, "%i-%i", i, i);
117 ok1(memcmp(b, buf[(i + NUM_ITERS) % NUM].buf, sizeof(b)) == 0);
120 /* This exits depending on whether all tests passed */
121 return exit_status();