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)
165 if (base->sigpipe[0] == -1) {
166 /* pipe2(2) is not available everywhere... sigh */
167 if (pipe(base->sigpipe) == -1)
170 if ((flags = fcntl(base->sigpipe[1], F_GETFL)) == -1 ||
171 fcntl(base->sigpipe[1], F_SETFL, flags | O_NONBLOCK) == -1)
174 if (ev_add(base->sigpipe[0], POLLIN, ev_sigdispatch, NULL)
180 base->sigcb.udata = udata;
182 signal(sig, ev_sigcatch);
187 ev_timer(const struct timeval *tv, void (*cb)(int, int, void*), void *udata)
189 base->timeout = INFTIM;
191 base->timeout = tv->tv_sec * 1000;
192 base->timeout += tv->tv_usec / 1000;
195 base->toutcb.cb = cb;
196 base->toutcb.udata = udata;
202 ev_timer_pending(void)
204 return base->timeout != INFTIM;
210 if (fd >= base->len) {
215 base->pfds[fd].fd = -1;
216 base->pfds[fd].events = 0;
218 base->cbs[fd].cb = NULL;
219 base->cbs[fd].udata = NULL;
227 struct timespec elapsed, beg, end;
232 clock_gettime(CLOCK_MONOTONIC, &beg);
233 if ((n = poll(base->pfds, base->len, base->timeout)) == -1) {
237 clock_gettime(CLOCK_MONOTONIC, &end);
239 timespecsub(&end, &beg, &elapsed);
240 em = elapsed.tv_sec * 1000 + elapsed.tv_nsec / 1000000;
241 if (base->timeout != INFTIM) {
242 if (base->timeout - em < 0 || n == 0) {
243 base->timeout = INFTIM;
244 base->toutcb.cb(-1, 0, base->toutcb.udata);
249 for (i = 0; i < base->len && n > 0 && !ev_stop; ++i) {
250 if (base->pfds[i].fd == -1)
252 if (base->pfds[i].revents & (POLLIN|POLLOUT|POLLHUP)) {
254 base->cbs[i].cb(base->pfds[i].fd,
255 base->pfds[i].revents,