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,
51 // XXX we should hide the poll::error and return 'just' an error.
53 export type evloop = struct {
54 add: *fn(*evloop, io::file, event, *fn(io::file, event, nullable *opaque) void, nullable *opaque) void,
55 del: *fn(*evloop, io::file) void,
56 loop: *fn(*evloop) (void | poll::error),
57 loopbreak: *fn(*evloop) void,
64 sigpipe: (void | (io::file, io::file)),
65 sigcb: (void | *fn(signal::sig, nullable *opaque) void),
66 sigdata: nullable *opaque,
67 stop: bool, // signal to stop
71 export fn new() *evloop = alloc(base {
75 loopbreak = &evloopbreak,
81 fn ev2poll(ev: event) poll::event = switch (ev) {
82 case event::READ => yield poll::event::POLLIN;
83 case event::WRITE => yield poll::event::POLLOUT;
86 fn poll2ev(ev: i16) event = {
87 if ((ev & poll::event::POLLOUT) != 0) {
97 f: *fn(io::file, event, nullable *opaque) void,
98 data: nullable *opaque
100 const b = evloop: *base;
102 for (let i = 0z; i < len(b.wip.pfds); i += 1) {
103 if (b.wip.pfds[i].fd != fd)
105 b.wip.pfds[i].events = ev2poll(ev);
106 b.wip.cbs[i] = cb { cb = f, data = data };
110 append(b.wip.pfds, poll::pollfd {
112 events = ev2poll(ev),
115 append(b.wip.cbs, cb {
121 export fn evdel(evloop: *evloop, fd: io::file) void = {
122 const b = evloop: *base;
123 for (let i = 0z; i < len(b.wip.pfds); i += 1) {
124 if (b.wip.pfds[i].fd != fd) {
127 delete(b.wip.pfds[i]);
128 delete(b.wip.cbs[i]);
133 fn prepare_queue(b: *base) void = {
134 delete(b.working.pfds[..]);
135 delete(b.working.cbs[..]);
137 for (let i = 0z; i < len(b.wip.pfds); i += 1) {
138 append(b.working.pfds, b.wip.pfds[i]);
139 append(b.working.cbs, b.wip.cbs[i]);
143 export fn loop(evloop: *evloop) (void | poll::error) = {
144 const b = evloop: *base;
146 const mask = poll::event::POLLIN | poll::event::POLLOUT |
147 poll::event::POLLHUP;
154 let n = match (poll::poll(q.pfds, poll::INDEF)) {
155 case let err: errors::interrupted =>
157 case let err: errors::error =>
159 case let n: uint => yield n;
162 for (let i = 0z; i < len(q.pfds); i += 1) {
163 // XXX check for POLLNVAL ?
165 if ((q.pfds[i].revents & mask) == 0) {
168 q.cbs[i].cb(q.pfds[i].fd, poll2ev(q.pfds[i].revents),
174 export fn evloopbreak(evloop: *evloop) void = {
175 const b = evloop: *base;