Lindenii Project Forge
ev::read, ev::write: initial support
use errors; use io; use rt;
export type op = enum { NONE, READV, WRITEV, };
export type file = struct { fd: io::file,
ev: *loop, // Pending operation on this file object op: op, callback: 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 => 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(loop: *loop, file: *file) void = { // 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); };
// 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, ... }; 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); file.op = op::READV; file.callback = cb; file.vec = vec; filemod(file, rt::EPOLLOUT); return req { ... }; }; // 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); file.op = op::WRITEV; file.callback = cb; file.vec = vec; filemod(file, rt::EPOLLOUT); return req { ... }; };
// Defines events which are available for monitoring. export type event = enum u32 { // Read operations are available IN = 0x00000001, // Write operations are available OUT = 0x00000004, // An error condition has been reported ERR = 0x00000008, // There is exceptional data available in the file descriptor; // equivalent to [[unix::poll::event::POLLPRI]] PRI = 0x00000002, // A hang-up event has occured HUP = 0x00000010,
export type req = struct { placeholder: uint,
};
// TODO: Add less portable flags like EPOLLWAKEUP