// This is free and unencumbered software released into the public domain. // // Anyone is free to copy, modify, publish, use, compile, sell, or // distribute this software, either in source code form or as a compiled // binary, for any purpose, commercial or non-commercial, and by any // means. // // In jurisdictions that recognize copyright laws, the author or authors // of this software dedicate any and all copyright interest in the // software to the public domain. We make this dedication for the benefit // of the public at large and to the detriment of our heirs and // successors. We intend this dedication to be an overt act of // relinquishment in perpetuity of all present and future rights to this // software under copyright law. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // XXX would be nice to eventually have multiple backends for poll, // kqueue, epoll. use errors; use io; use unix; use unix::poll; use unix::signal; export type event = enum i16 { READ, WRITE, }; export def READ = event::READ; export def WRITE = event::WRITE; type cb = struct { cb: *fn(io::file, event, nullable *opaque) void, data: nullable *opaque, }; type queue = struct { pfds: []poll::pollfd, cbs: []cb, }; // XXX we should hide the poll::error and return 'just' an error. // maybe. export type evloop = struct { add: *fn(*evloop, io::file, event, *fn(io::file, event, nullable *opaque) void, nullable *opaque) void, del: *fn(*evloop, io::file) void, loop: *fn(*evloop) (void | poll::error), loopbreak: *fn(*evloop) void, }; type base = struct { evloop, working: queue, wip: queue, sigpipe: (void | (io::file, io::file)), sigcb: (void | *fn(signal::sig, nullable *opaque) void), sigdata: nullable *opaque, stop: bool, // signal to stop // TODO: timeout }; export fn new() *evloop = alloc(base { add = &evadd, del = &evdel, loop = &loop, loopbreak = &evloopbreak, sigpipe = void, sigcb = void, ... }); fn ev2poll(ev: event) poll::event = switch (ev) { case event::READ => yield poll::event::POLLIN; case event::WRITE => yield poll::event::POLLOUT; }; fn poll2ev(ev: i16) event = { if ((ev & poll::event::POLLOUT) != 0) { return event::WRITE; }; return event::READ; }; export fn evadd( evloop: *evloop, fd: io::file, ev: event, f: *fn(io::file, event, nullable *opaque) void, data: nullable *opaque ) void = { const b = evloop: *base; for (let i = 0z; i < len(b.wip.pfds); i += 1) { if (b.wip.pfds[i].fd != fd) continue; b.wip.pfds[i].events = ev2poll(ev); b.wip.cbs[i] = cb { cb = f, data = data }; return; }; append(b.wip.pfds, poll::pollfd { fd = fd, events = ev2poll(ev), ... }); append(b.wip.cbs, cb { cb = f, data = data, }); }; export fn evdel(evloop: *evloop, fd: io::file) void = { const b = evloop: *base; for (let i = 0z; i < len(b.wip.pfds); i += 1) { if (b.wip.pfds[i].fd != fd) { continue; }; delete(b.wip.pfds[i]); delete(b.wip.cbs[i]); return; }; }; fn prepare_queue(b: *base) void = { delete(b.working.pfds[..]); delete(b.working.cbs[..]); for (let i = 0z; i < len(b.wip.pfds); i += 1) { append(b.working.pfds, b.wip.pfds[i]); append(b.working.cbs, b.wip.cbs[i]); }; }; export fn loop(evloop: *evloop) (void | poll::error) = { const b = evloop: *base; const mask = poll::event::POLLIN | poll::event::POLLOUT | poll::event::POLLHUP; for (!b.stop) { prepare_queue(b); let q = b.working; let n = match (poll::poll(q.pfds, poll::INDEF)) { case let err: errors::interrupted => continue; case let err: errors::error => return err; case let n: uint => yield n; }; for (let i = 0z; i < len(q.pfds); i += 1) { // XXX check for POLLNVAL ? if ((q.pfds[i].revents & mask) == 0) { continue; }; q.cbs[i].cb(q.pfds[i].fd, poll2ev(q.pfds[i].revents), q.cbs[i].data); }; }; }; export fn evloopbreak(evloop: *evloop) void = { const b = evloop: *base; b.stop = true; };