Blob


1 // This is free and unencumbered software released into the public domain.
2 //
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
6 // means.
7 //
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.
15 //
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,
25 // kqueue, epoll.
27 use errors;
28 use io;
29 use unix;
30 use unix::poll;
31 use unix::signal;
33 export type event = enum i16 {
34 READ,
35 WRITE,
36 };
38 export def READ = event::READ;
39 export def WRITE = event::WRITE;
41 type cb = struct {
42 cb: *fn(io::file, event, nullable *opaque) void,
43 data: nullable *opaque,
44 };
46 type queue = struct {
47 pfds: []poll::pollfd,
48 cbs: []cb,
49 };
51 type base = struct {
52 working: queue,
53 wip: queue,
54 sigpipe: ((io::file, io::file) | void),
55 sigcb: (cb | void),
56 stop: bool, // signal to stop
57 // TODO: timeout
58 };
60 let b = base {
61 sigpipe = void,
62 sigcb = void,
63 ...
64 };
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;
69 };
71 fn poll2ev(ev: i16) event = {
72 if ((ev & poll::event::POLLOUT) != 0) {
73 return event::WRITE;
74 };
75 return event::READ;
76 };
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 {
80 fd = fd,
81 events = ev2poll(ev),
82 ...
83 });
84 append(b.wip.cbs, cb {
85 cb = f,
86 data = data,
87 });
88 };
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) {
93 continue;
94 };
95 delete(b.wip.pfds[i]);
96 delete(b.wip.cbs[i]);
97 return;
98 };
99 };
101 fn prepare_queue() void = {
102 let wip = b.wip;
104 if (len(b.working.pfds) > 0) {
105 delete(b.working.pfds[..]);
106 delete(b.working.cbs[..]);
107 };
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]);
112 };
113 };
115 export fn loop() (void | poll::error) = {
116 const mask = poll::event::POLLIN | poll::event::POLLOUT |
117 poll::event::POLLHUP;
119 for (!b.stop) {
120 prepare_queue();
122 let q = b.working;
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) {
127 continue;
128 };
129 q.cbs[i].cb(q.pfds[i].fd, poll2ev(q.pfds[i].revents),
130 q.cbs[i].data);
131 };
132 };
133 };
135 export fn loopexit() void = {
136 b.stop = true;
137 };