2 * uloop - event loop implementation
4 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
34 #include <sys/event.h>
37 #include <sys/epoll.h>
41 #define ULOOP_MAX_EVENTS 10
43 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
44 static struct list_head processes = LIST_HEAD_INIT(processes);
46 static int poll_fd = -1;
47 bool uloop_cancelled = false;
48 bool uloop_handle_sigchld = true;
49 static bool do_sigchld = false;
50 static int cur_fd, cur_nfds;
56 struct timespec timeout = { 0, 0 };
57 struct kevent ev = {};
66 EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
67 kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
73 static uint16_t get_flags(unsigned int flags, unsigned int mask)
81 if (flags & ULOOP_EDGE_TRIGGER)
87 static struct kevent events[ULOOP_MAX_EVENTS];
89 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
91 struct timespec timeout = { 0, 0 };
98 if (flags & ULOOP_EDGE_DEFER)
99 flags &= ~ULOOP_EDGE_TRIGGER;
101 changed = flags ^ fd->flags;
102 if (changed & ULOOP_EDGE_TRIGGER)
105 if (changed & ULOOP_READ) {
106 kflags = get_flags(flags, ULOOP_READ);
107 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
110 if (changed & ULOOP_WRITE) {
111 kflags = get_flags(flags, ULOOP_WRITE);
112 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
119 if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
125 static int register_poll(struct uloop_fd *fd, unsigned int flags)
127 if (flags & ULOOP_EDGE_TRIGGER)
128 flags |= ULOOP_EDGE_DEFER;
130 flags &= ~ULOOP_EDGE_DEFER;
132 return register_kevent(fd, flags);
135 int uloop_fd_delete(struct uloop_fd *sock)
139 for (i = cur_fd + 1; i < cur_nfds; i++) {
140 if (events[i].udata != sock)
143 events[i].udata = NULL;
146 sock->registered = false;
147 return register_poll(sock, 0);
150 static void uloop_run_events(int timeout)
156 ts.tv_sec = timeout / 1000;
157 ts.tv_nsec = (timeout % 1000) * 1000000;
160 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
161 for(n = 0; n < nfds; ++n)
163 struct uloop_fd *u = events[n].udata;
169 if (events[n].flags & EV_ERROR) {
174 if(events[n].filter == EVFILT_READ)
176 else if (events[n].filter == EVFILT_WRITE)
179 if (events[n].flags & EV_EOF)
188 if (u->flags & ULOOP_EDGE_DEFER) {
189 u->flags &= ~ULOOP_EDGE_DEFER;
190 register_kevent(u, u->flags);
202 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
205 #define EPOLLRDHUP 0x2000
213 poll_fd = epoll_create(32);
217 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
221 static int register_poll(struct uloop_fd *fd, unsigned int flags)
223 struct epoll_event ev;
224 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
226 memset(&ev, 0, sizeof(struct epoll_event));
228 if (flags & ULOOP_READ)
229 ev.events |= EPOLLIN | EPOLLRDHUP;
231 if (flags & ULOOP_WRITE)
232 ev.events |= EPOLLOUT;
234 if (flags & ULOOP_EDGE_TRIGGER)
235 ev.events |= EPOLLET;
240 return epoll_ctl(poll_fd, op, fd->fd, &ev);
243 static struct epoll_event events[ULOOP_MAX_EVENTS];
245 int uloop_fd_delete(struct uloop_fd *sock)
249 if (!sock->registered)
252 for (i = cur_fd + 1; i < cur_nfds; i++) {
253 if (events[i].data.ptr != sock)
256 events[i].data.ptr = NULL;
258 sock->registered = false;
259 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
262 static void uloop_run_events(int timeout)
266 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
267 for(n = 0; n < nfds; ++n)
269 struct uloop_fd *u = events[n].data.ptr;
275 if(events[n].events & (EPOLLERR|EPOLLHUP)) {
280 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)))
283 if(events[n].events & EPOLLRDHUP)
286 if(events[n].events & EPOLLIN)
289 if(events[n].events & EPOLLOUT)
303 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
308 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
309 return uloop_fd_delete(sock);
311 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
312 fl = fcntl(sock->fd, F_GETFL, 0);
314 fcntl(sock->fd, F_SETFL, fl);
317 ret = register_poll(sock, flags);
321 sock->registered = true;
328 static int tv_diff(struct timeval *t1, struct timeval *t2)
331 (t1->tv_sec - t2->tv_sec) * 1000 +
332 (t1->tv_usec - t2->tv_usec) / 1000;
335 int uloop_timeout_add(struct uloop_timeout *timeout)
337 struct uloop_timeout *tmp;
338 struct list_head *h = &timeouts;
340 if (timeout->pending)
343 list_for_each_entry(tmp, &timeouts, list) {
344 if (tv_diff(&tmp->time, &timeout->time) > 0) {
350 list_add_tail(&timeout->list, h);
351 timeout->pending = true;
356 static void uloop_gettime(struct timeval *tv)
360 clock_gettime(CLOCK_MONOTONIC, &ts);
361 tv->tv_sec = ts.tv_sec;
362 tv->tv_usec = ts.tv_nsec / 1000;
365 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
367 struct timeval *time = &timeout->time;
369 if (timeout->pending)
370 uloop_timeout_cancel(timeout);
372 uloop_gettime(&timeout->time);
374 time->tv_sec += msecs / 1000;
375 time->tv_usec += (msecs % 1000) * 1000;
377 if (time->tv_usec > 1000000) {
379 time->tv_usec %= 1000000;
382 return uloop_timeout_add(timeout);
385 int uloop_timeout_cancel(struct uloop_timeout *timeout)
387 if (!timeout->pending)
390 list_del(&timeout->list);
391 timeout->pending = false;
396 int uloop_timeout_remaining(struct uloop_timeout *timeout)
400 if (!timeout->pending)
405 return tv_diff(&timeout->time, &now);
408 int uloop_process_add(struct uloop_process *p)
410 struct uloop_process *tmp;
411 struct list_head *h = &processes;
416 list_for_each_entry(tmp, &processes, list) {
417 if (tmp->pid > p->pid) {
423 list_add_tail(&p->list, h);
429 int uloop_process_delete(struct uloop_process *p)
440 static void uloop_handle_processes(void)
442 struct uloop_process *p, *tmp;
449 pid = waitpid(-1, &ret, WNOHANG);
453 list_for_each_entry_safe(p, tmp, &processes, list) {
460 uloop_process_delete(p);
467 static void uloop_handle_sigint(int signo)
469 uloop_cancelled = true;
472 static void uloop_sigchld(int signo)
477 static void uloop_setup_signals(void)
481 memset(&s, 0, sizeof(struct sigaction));
482 s.sa_handler = uloop_handle_sigint;
484 sigaction(SIGINT, &s, NULL);
486 if (uloop_handle_sigchld) {
487 s.sa_handler = uloop_sigchld;
488 sigaction(SIGCHLD, &s, NULL);
492 static int uloop_get_next_timeout(struct timeval *tv)
494 struct uloop_timeout *timeout;
497 if (list_empty(&timeouts))
500 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
501 diff = tv_diff(&timeout->time, tv);
508 static void uloop_process_timeouts(struct timeval *tv)
510 struct uloop_timeout *t;
512 while (!list_empty(&timeouts)) {
513 t = list_first_entry(&timeouts, struct uloop_timeout, list);
515 if (tv_diff(&t->time, tv) > 0)
518 uloop_timeout_cancel(t);
524 static void uloop_clear_timeouts(void)
526 struct uloop_timeout *t, *tmp;
528 list_for_each_entry_safe(t, tmp, &timeouts, list)
529 uloop_timeout_cancel(t);
532 static void uloop_clear_processes(void)
534 struct uloop_process *p, *tmp;
536 list_for_each_entry_safe(p, tmp, &processes, list)
537 uloop_process_delete(p);
544 uloop_setup_signals();
545 while(!uloop_cancelled)
548 uloop_process_timeouts(&tv);
553 uloop_handle_processes();
554 uloop_run_events(uloop_get_next_timeout(&tv));
558 void uloop_done(void)
566 uloop_clear_timeouts();
567 uloop_clear_processes();