1 #include <ccan/io/io.h>
2 #include <ccan/time/time.h>
11 #define NUM_ITERS 10000
15 struct io_conn *reader, *writer;
19 static struct io_plan *poke_writer(struct io_conn *conn, struct buffer *buf);
20 static struct io_plan *poke_reader(struct io_conn *conn, struct buffer *buf);
22 static struct io_plan *do_read(struct io_conn *conn, struct buffer *buf)
24 assert(conn == buf->reader);
26 return io_read(conn, &buf->buf, sizeof(buf->buf), poke_writer, buf);
29 static struct io_plan *do_write(struct io_conn *conn, struct buffer *buf)
31 assert(conn == buf->writer);
33 return io_write(conn, &buf->buf, sizeof(buf->buf), poke_reader, buf);
36 static struct io_plan *poke_writer(struct io_conn *conn, struct buffer *buf)
38 assert(conn == buf->reader);
40 if (buf->iters == NUM_ITERS)
41 return io_close(conn, NULL);
44 io_wake(buf->writer, do_write, buf);
46 /* I'll wait until you wake me. */
50 static struct io_plan *poke_reader(struct io_conn *conn, struct buffer *buf)
52 assert(conn == buf->writer);
54 io_wake(buf->reader, do_read, buf);
56 if (++buf->iters == NUM_ITERS)
57 return io_close(conn, NULL);
59 /* I'll wait until you tell me to write. */
63 static struct io_plan *reader(struct io_conn *conn, struct buffer *buf)
65 assert(conn == buf->reader);
67 /* Wait for writer to tell us to read. */
74 int fds[2], last_read, last_write;
75 struct timespec start, end;
76 struct buffer buf[NUM];
83 for (i = 1; i < NUM; i++) {
87 memset(buf[i].buf, i, sizeof(buf[i].buf));
88 sprintf(buf[i].buf, "%i-%i", i, i);
90 buf[i].reader = io_new_conn(last_read, reader, NULL, &buf[i]);
92 err(1, "Creating reader %i", i);
93 buf[i].writer = io_new_conn(fds[1], do_write, NULL, &buf[i]);
95 err(1, "Creating writer %i", i);
99 /* 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 err(1, "Creating reader %i", i);
106 buf[i].writer = io_new_conn(last_write, do_write, NULL, &buf[i]);
108 err(1, "Creating writer %i", i);
110 /* They should eventually exit */
112 if (io_loop() != NULL)
116 for (i = 0; i < NUM; i++) {
117 char b[sizeof(buf[0].buf)];
118 memset(b, i, sizeof(b));
119 sprintf(b, "%i-%i", i, i);
120 if (memcmp(b, buf[(i + NUM_ITERS) % NUM].buf, sizeof(b)) != 0)
121 errx(1, "Buffer for %i was '%s' not '%s'",
122 i, buf[(i + NUM_ITERS) % NUM].buf, b);
125 printf("run-many: %u %u iterations: %llu usec\n",
126 NUM, NUM_ITERS, (long long)time_to_usec(time_sub(end, start)));