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