diff options
Diffstat (limited to 'netio_posix.c')
-rw-r--r-- | netio_posix.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/netio_posix.c b/netio_posix.c new file mode 100644 index 0000000..0aa8277 --- /dev/null +++ b/netio_posix.c @@ -0,0 +1,49 @@ +#include <aio.h> +#include <sys/socket.h> + +#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(); + + + +} |