summaryrefslogtreecommitdiff
path: root/netio_posix.c
blob: 0aa82773795001fa276c1cde3107b8d5d83acd1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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();
	

	
}