Lindenii Project Forge
Login

hare-ev

Temporary fork of hare-ev for... reasons
Commit info
ID
e78fbb7a8b5bebf518faced27372aa27f692395f
Author
Drew DeVault <sir@cmpwn.com>
Author date
Wed, 30 Nov 2022 15:28:36 +0100
Committer
Drew DeVault <sir@cmpwn.com>
Committer date
Wed, 30 Nov 2022 15:28:36 +0100
Actions
ev: implement blocking I/O fallback
The ev module provides an event loop. Use [[newloop]] to create an event loop,
and [[finish]] to finalize it. You may add external I/O sources via functions
like [[register]] and [[unregister]].

On Linux, ev is implemented with epoll. Note that, on Linux, I/O on regular
files is always blocking.
use errors;
use io;
use rt;

export type op = enum {
	NONE,
	READV,
	WRITEV,
};

export type fflags = enum uint {
	BLOCKING = 1 << 31,
};

export type file = struct {
	fd: io::file,
	ev: *loop,
	// Pending operation on this file object

	flags: fflags,
	op: op,
	cb: nullable *void,
	user: nullable *void,

	// Operation-specific data
	vbuf: rt::iovec,
	union {
		vec: []rt::iovec,
	},
};

// Registers a file descriptor with an event loop.
export fn register(
	loop: *loop,
	fd: io::file,
) (*file | errors::error) = {
	const file = alloc(file {
		fd = fd,
		ev = loop,
		op = op::NONE,
		...
	});

	let ev = rt::epoll_event {
		events = 0,
		...
	};
	ev.data.ptr = file;
	match (rt::epoll_ctl(loop.fd, rt::EPOLL_CTL_ADD, fd, &ev)) {
	case void =>
		yield;
	case let err: rt::errno =>
		if (err: int == rt::EPERM) {
			// epoll(2) does not support regular files, use blocking
			// I/O instead
			file.flags = fflags::BLOCKING;
			return file;
		};
		return errors::errno(err);
	};

	return file;
};

// Unregisters a file object with an event loop and frees resources associated
// with it. Does not close the underlying file descriptor.
export fn unregister(file: *file) void = {
	const loop = file.ev;
	// The only way that this could fail is in the event of a use-after-free
	// or if the user fucks around and constructs a custom [[file]] which
	// was never registered, so assert on error.
	rt::epoll_ctl(loop.fd, rt::EPOLL_CTL_DEL, file.fd, null)!;
	if (file.flags & fflags::BLOCKING == 0) {
		// The only way that this could fail is in the event of a
		// use-after-free or if the user fucks around and constructs a
		// custom [[file]] which was never registered, so assert on
		// error.
		rt::epoll_ctl(loop.fd, rt::EPOLL_CTL_DEL, file.fd, null)!;
	};
	free(file);
};

// Sets the user data field on this file object to the provided object.
export fn setuser(file: *file, user: nullable *void) void = {
	file.user = user;
};

// Returns the user data field from this file object. If the field was null, an
// assertion is raised.
export fn getuser(file: *file) *void = {
	return file.user as *void;
};

// Returns the file descriptor for a given file. Note that ev assumes that it
// will be responsible for all I/O on the file and any user modifications may
// cause the event loop to enter an invalid state.
export fn getfd(file: *file) io::file = {
	return file.fd;
};

// Returns the event loop for a given file.
export fn getloop(file: *file) *loop = {
	return file.ev;
};

// Modifies the epoll events for a given file. For internal use.
fn filemod(file: *file, events: u32) void = {
	let ev = rt::epoll_event {
		events = events | rt::EPOLLONESHOT,
		...
	};
	ev.data.ptr = file;
	// This can only fail under conditions associated with EPOLLEXCLUSIVE,
	// which we do not support.
	rt::epoll_ctl(file.ev.fd, rt::EPOLL_CTL_MOD, file.fd, &ev)!;
};
use io;
use rt;

// A callback for a [[read]] or [[readv]] operation.
export type readcb = fn(file: *file, result: (size | io::EOF | io::error)) void;

// Schedules a read operation on a file object.
export fn read(
	file: *file,
	cb: *readcb,
	buf: []u8,
) req = {
	file.vbuf = io::mkvector(buf);
	// XXX: Bit of a hack to avoid allocating a slice
	const vec = (&file.vbuf: *[*]io::vector)[..1];
	return readv(file, cb, vec...);
};

// Schedules a vectored read operation on a file object.
export fn readv(
	file: *file,
	cb: *readcb,
	vec: io::vector...
) req = {
	assert(file.op == op::NONE);
	if (file.flags & fflags::BLOCKING != 0) {
		const r = io::readv(file.fd, vec...);
		cb(file, r);
		return req { ... };
	};

	file.op = op::READV;
	file.cb = cb;
	file.vec = vec;
	filemod(file, rt::EPOLLIN | rt::EPOLLHUP);
	return req { ... };
};

fn readv_finish(file: *file, ev: *rt::epoll_event) void = {
	assert(file.op == op::READV);
	assert(ev.events & (rt::EPOLLIN | rt::EPOLLHUP) != 0);
	assert(file.cb != null);
	const cb = file.cb: *readcb;
	file.op = op::NONE;

	if (ev.events & rt::EPOLLHUP != 0) {
		cb(file, io::EOF);
	} else {
		const r = io::readv(file.fd, file.vec...);
		cb(file, r);
	};
};

// A callback for a [[write]] or [[writev]] operation.
export type writecb = fn(file: *file, result: (size | io::error)) void;

// Schedules a write operation on a file object.
export fn write(
	file: *file,
	cb: *writecb,
	buf: []u8,
) req = {
	file.vbuf = io::mkvector(buf);
	// XXX: Bit of a hack to avoid allocating a slice
	const vec = (&file.vbuf: *[*]io::vector)[..1];
	return writev(file, cb, vec...);
};

// Schedules a vectored read operation on a file object.
export fn writev(
	file: *file,
	cb: *writecb,
	vec: io::vector...
) req = {
	// XXX: Should we support both pending reads and writes at the same
	// time? (yes)
	assert(file.op == op::NONE);
	if (file.flags & fflags::BLOCKING != 0) {
		const r = io::writev(file.fd, vec...);
		cb(file, r);
		return req { ... };
	};

	file.op = op::WRITEV;
	file.cb = cb;
	file.vec = vec;
	filemod(file, rt::EPOLLOUT | rt::EPOLLHUP);
	return req { ... };
};

fn writev_finish(file: *file, ev: *rt::epoll_event) void = {
	assert(file.op == op::WRITEV && ev.events & rt::EPOLLOUT != 0);
	assert(file.cb != null);
	const r = io::writev(file.fd, file.vec...);
	const cb = file.cb: *writecb;
	file.op = op::NONE;
	cb(file, r);
};