2 * This is free and unencumbered software released into the public domain.
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
40 void (*cb)(int, int, void *);
60 static struct evbase *base;
69 t = recallocarray(base->pfds, base->pfdlen, len, sizeof(*base->pfds));
75 for (i = base->len; i < len; ++i)
76 base->pfds[i].fd = -1;
78 t = recallocarray(base->cbs, base->cblen, len, sizeof(*base->cbs));
96 if ((base = calloc(1, sizeof(*base))) == NULL)
99 base->sigpipe[0] = -1;
100 base->sigpipe[1] = -1;
101 base->timeout = INFTIM;
103 if (ev_resize(16) == -1) {
115 ev_add(int fd, int ev, void (*cb)(int, int, void *), void *udata)
117 if (fd >= base->len) {
118 if (ev_resize(fd + 1) == -1)
122 base->pfds[fd].fd = fd;
123 base->pfds[fd].events = ev;
125 base->cbs[fd].cb = cb;
126 base->cbs[fd].udata = udata;
132 ev_sigcatch(int signo)
140 * We should be able to write up to PIPE_BUF bytes without
144 (void) write(base->sigpipe[1], &s, sizeof(s));
150 ev_sigdispatch(int fd, int ev, void *data)
154 if (read(fd, &signo, sizeof(signo)) != sizeof(signo))
157 base->sigcb.cb(signo, 0, base->sigcb.udata);
161 ev_signal(int sig, void (*cb)(int, int, void *), void *udata)
163 if (base->sigpipe[0] == -1) {
164 if (pipe2(base->sigpipe, O_NONBLOCK) == -1)
166 if (ev_add(base->sigpipe[0], POLLIN, ev_sigdispatch, NULL)
172 base->sigcb.udata = udata;
174 signal(sig, ev_sigcatch);
179 ev_timer(const struct timeval *tv, void (*cb)(int, int, void*), void *udata)
181 base->timeout = INFTIM;
183 base->timeout = tv->tv_sec * 1000;
184 base->timeout += tv->tv_usec / 1000;
187 base->toutcb.cb = cb;
188 base->toutcb.udata = udata;
194 ev_timer_pending(void)
196 return base->timeout != INFTIM;
202 if (fd >= base->len) {
207 base->pfds[fd].fd = -1;
208 base->pfds[fd].events = 0;
210 base->cbs[fd].cb = NULL;
211 base->cbs[fd].udata = NULL;
219 struct timespec elapsed, beg, end;
224 clock_gettime(CLOCK_MONOTONIC, &beg);
225 if ((n = poll(base->pfds, base->len, base->timeout)) == -1) {
229 clock_gettime(CLOCK_MONOTONIC, &end);
231 timespecsub(&end, &beg, &elapsed);
232 em = elapsed.tv_sec * 1000 + elapsed.tv_nsec / 1000000;
233 if (base->timeout != INFTIM) {
234 if (base->timeout - em < 0 || n == 0) {
235 base->timeout = INFTIM;
236 base->toutcb.cb(-1, 0, base->toutcb.udata);
241 for (i = 0; i < base->len && n > 0 && !ev_stop; ++i) {
242 if (base->pfds[i].fd == -1)
244 if (base->pfds[i].revents & (POLLIN|POLLOUT|POLLHUP)) {
246 base->cbs[i].cb(base->pfds[i].fd,
247 base->pfds[i].revents,