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_reader(struct io_conn *conn, struct buffer *buf);
21 static struct io_plan poke_writer(struct io_conn *conn, struct buffer *buf)
23 assert(conn == buf->reader);
25 if (buf->iters == NUM_ITERS)
26 return io_close(conn, NULL);
30 io_write(&buf->buf, sizeof(buf->buf), poke_reader, buf));
32 /* I'll wait until you wake me. */
36 static struct io_plan poke_reader(struct io_conn *conn, struct buffer *buf)
38 assert(conn == buf->writer);
41 io_read(&buf->buf, sizeof(buf->buf), poke_writer, buf));
43 if (++buf->iters == NUM_ITERS)
44 return io_close(conn, NULL);
46 /* I'll wait until you tell me to write. */
53 int fds[2], last_read, last_write;
54 struct timespec start, end;
55 struct buffer buf[NUM];
62 for (i = 1; i < NUM; i++) {
66 memset(buf[i].buf, i, sizeof(buf[i].buf));
67 sprintf(buf[i].buf, "%i-%i", i, i);
69 buf[i].reader = io_new_conn(last_read, io_idle(), NULL, NULL);
71 err(1, "Creating reader %i", i);
72 buf[i].writer = io_new_conn(fds[1],
75 poke_reader, &buf[i]),
78 err(1, "Creating writer %i", i);
82 /* Last one completes the cirle. */
85 sprintf(buf[i].buf, "%i-%i", i, i);
86 buf[i].reader = io_new_conn(last_read, io_idle(), NULL, NULL);
88 err(1, "Creating reader %i", i);
89 buf[i].writer = io_new_conn(last_write, io_write(&buf[i].buf,
91 poke_reader, &buf[i]),
94 err(1, "Creating writer %i", i);
96 /* They should eventually exit */
98 if (io_loop() != NULL)
102 for (i = 0; i < NUM; i++) {
103 char b[sizeof(buf[0].buf)];
104 memset(b, i, sizeof(b));
105 sprintf(b, "%i-%i", i, i);
106 if (memcmp(b, buf[(i + NUM_ITERS) % NUM].buf, sizeof(b)) != 0)
107 errx(1, "Buffer for %i was '%s' not '%s'",
108 i, buf[(i + NUM_ITERS) % NUM].buf, b);
111 printf("run-many: %u %u iterations: %llu usec\n",
112 NUM, NUM_ITERS, (long long)time_to_usec(time_sub(end, start)));