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 // XXX we should hide the poll::error and return 'just' an error.
52 // maybe.
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,
58 };
60 type base = struct {
61 evloop,
62 working: queue,
63 wip: queue,
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
68 // TODO: timeout
69 };
71 export fn new() *evloop = alloc(base {
72 add = &evadd,
73 del = &evdel,
74 loop = &loop,
75 loopbreak = &evloopbreak,
76 sigpipe = void,
77 sigcb = void,
78 ...
79 });
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;
84 };
86 fn poll2ev(ev: i16) event = {
87 if ((ev & poll::event::POLLOUT) != 0) {
88 return event::WRITE;
89 };
90 return event::READ;
91 };
93 export fn evadd(
94 evloop: *evloop,
95 fd: io::file,
96 ev: event,
97 f: *fn(io::file, event, nullable *opaque) void,
98 data: nullable *opaque
99 ) void = {
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)
104 continue;
105 b.wip.pfds[i].events = ev2poll(ev);
106 b.wip.cbs[i] = cb { cb = f, data = data };
107 return;
108 };
110 append(b.wip.pfds, poll::pollfd {
111 fd = fd,
112 events = ev2poll(ev),
113 ...
114 });
115 append(b.wip.cbs, cb {
116 cb = f,
117 data = data,
118 });
119 };
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) {
125 continue;
126 };
127 delete(b.wip.pfds[i]);
128 delete(b.wip.cbs[i]);
129 return;
130 };
131 };
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]);
140 };
141 };
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;
149 for (!b.stop) {
150 prepare_queue(b);
152 let q = b.working;
154 let n = match (poll::poll(q.pfds, poll::INDEF)) {
155 case let err: errors::interrupted =>
156 continue;
157 case let err: errors::error =>
158 return err;
159 case let n: uint => yield n;
160 };
162 for (let i = 0z; i < len(q.pfds); i += 1) {
163 // XXX check for POLLNVAL ?
165 if ((q.pfds[i].revents & mask) == 0) {
166 continue;
167 };
168 q.cbs[i].cb(q.pfds[i].fd, poll2ev(q.pfds[i].revents),
169 q.cbs[i].data);
170 };
171 };
172 };
174 export fn evloopbreak(evloop: *evloop) void = {
175 const b = evloop: *base;
176 b.stop = true;
177 };