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: ((io::file, io::file) | void),
65 sigcb: (cb | void),
66 stop: bool, // signal to stop
67 // TODO: timeout
68 };
70 let _default = base {
71 add = &evadd,
72 del = &evdel,
73 loop = &loop,
74 loopbreak = &evloopbreak,
75 sigpipe = void,
76 sigcb = void,
77 ...
78 };
80 let global = &_default;
82 export fn new() *evloop = alloc(base {
83 add = &evadd,
84 del = &evdel,
85 loop = &loop,
86 loopbreak = &evloopbreak,
87 sigpipe = void,
88 sigcb = void,
89 ...
90 });
92 fn ev2poll(ev: event) poll::event = switch (ev) {
93 case event::READ => yield poll::event::POLLIN;
94 case event::WRITE => yield poll::event::POLLOUT;
95 };
97 fn poll2ev(ev: i16) event = {
98 if ((ev & poll::event::POLLOUT) != 0) {
99 return event::WRITE;
100 };
101 return event::READ;
102 };
104 export fn add(fd: io::file, ev: event, f: *fn(io::file, event, nullable *opaque) void, data: nullable *opaque) void = {
105 global.add(global, fd, ev, f, data);
106 };
108 export fn evadd(evloop: *evloop, fd: io::file, ev: event, f: *fn(io::file, event, nullable *opaque) void, data: nullable *opaque) void = {
109 const b = evloop: *base;
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 del(fd: io::file) void = {
122 global.del(global, fd);
123 };
125 export fn evdel(evloop: *evloop, fd: io::file) void = {
126 const b = evloop: *base;
127 for (let i = 0z; i < len(b.wip.pfds); i += 1) {
128 if (b.wip.pfds[i].fd != fd) {
129 continue;
130 };
131 delete(b.wip.pfds[i]);
132 delete(b.wip.cbs[i]);
133 return;
134 };
135 };
137 fn prepare_queue(b: *base) void = {
138 let wip = b.wip;
140 delete(b.working.pfds[..]);
141 delete(b.working.cbs[..]);
143 for (let i = 0z; i < len(wip.pfds); i += 1) {
144 append(b.working.pfds, wip.pfds[i]);
145 append(b.working.cbs, wip.cbs[i]);
146 };
147 };
149 export fn mainloop() (void | poll::error) = global.loop(global);
151 export fn loop(evloop: *evloop) (void | poll::error) = {
152 const b = evloop: *base;
154 const mask = poll::event::POLLIN | poll::event::POLLOUT |
155 poll::event::POLLHUP;
157 for (!b.stop) {
158 prepare_queue(b);
160 let q = b.working;
162 let n = poll::poll(q.pfds, poll::INDEF)?;
163 for (let i = 0z; i < len(q.pfds); i += 1) {
164 if ((q.pfds[i].revents & mask) == 0) {
165 continue;
166 };
167 q.cbs[i].cb(q.pfds[i].fd, poll2ev(q.pfds[i].revents),
168 q.cbs[i].data);
169 };
170 };
171 };
173 export fn loopbreak() void = {
174 global.loopbreak(global);
175 };
177 export fn evloopbreak(evloop: *evloop) void = {
178 const b = evloop: *base;
179 b.stop = true;
180 };