#include #include #include "coroutine.h" /* http://davmac.org/davpage/linux/async-io.html */ void netio_init(void) { sigaction(TODO) } int netio_accept(int socket) { /* AFAICT there is no good POSIX way to do this without busy-polling. */ for (;;) { int conn = accept(socket, NULL, NULL); if (conn < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { cr_yield(); continue; } return -errno; } return conn; } } int netio_read(int conn, void *buf, size_t count) { int r; struct aiocb ctl_block = { .aio_filedes = conn, .aio_buf = buff, .aio_nbytes = count, .aio_sigevent = { .sigev_notify = SIGEV_SIGNAL, .sigev_signo = SIGIO, .sigev_value = { .sigval_int = (int)cr_getcid(), }, }, }; if (aio_read(&ctl_block) < 0) return -errno; while ((r = aio_error(&ctl_block)) == EINPROGRESS) cr_pause_and_yield(); }