1 // This is free and unencumbered software released into the public domain.
3 // Anyone is free to copy, modify, publish, use, compile, sell, or
4 // distribute this software, either in source code form or as a compiled
5 // binary, for any purpose, commercial or non-commercial, and by any
8 // In jurisdictions that recognize copyright laws, the author or authors
9 // of this software dedicate any and all copyright interest in the
10 // software to the public domain. We make this dedication for the benefit
11 // of the public at large and to the detriment of our heirs and
12 // successors. We intend this dedication to be an overt act of
13 // relinquishment in perpetuity of all present and future rights to this
14 // software under copyright law.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 // OTHER DEALINGS IN THE SOFTWARE.
24 // XXX would be nice to eventually have multiple backends for poll,
33 export type event = enum i16 {
38 export def READ = event::READ;
39 export def WRITE = event::WRITE;
42 cb: *fn(io::file, event, nullable *opaque) void,
43 data: nullable *opaque,
54 sigpipe: ((io::file, io::file) | void),
56 stop: bool, // signal to stop
66 fn ev2poll(ev: event) poll::event = switch (ev) {
67 case event::READ => yield poll::event::POLLIN;
68 case event::WRITE => yield poll::event::POLLOUT;
71 fn poll2ev(ev: i16) event = {
72 if ((ev & poll::event::POLLOUT) != 0) {
78 export fn add(fd: io::file, ev: event, f: *fn(io::file, event, nullable *opaque) void, data: nullable *opaque) void = {
79 append(b.wip.pfds, poll::pollfd {
84 append(b.wip.cbs, cb {
90 export fn del(fd: io::file) void = {
91 for (let i = 0z; i < len(b.wip.pfds); i += 1) {
92 if (b.wip.pfds[i].fd != fd) {
95 delete(b.wip.pfds[i]);
101 fn prepare_queue() void = {
104 if (len(b.working.pfds) > 0) {
105 delete(b.working.pfds[..]);
106 delete(b.working.cbs[..]);
109 for (let i = 0z; i < len(wip.pfds); i += 1) {
110 append(b.working.pfds, wip.pfds[i]);
111 append(b.working.cbs, wip.cbs[i]);
115 export fn loop() (void | poll::error) = {
116 const mask = poll::event::POLLIN | poll::event::POLLOUT |
117 poll::event::POLLHUP;
124 let n = poll::poll(q.pfds, poll::INDEF)?;
125 for (let i = 0z; i < len(q.pfds); i += 1) {
126 if ((q.pfds[i].revents & mask) == 0) {
129 q.cbs[i].cb(q.pfds[i].fd, poll2ev(q.pfds[i].revents),
135 export fn loopexit() void = {