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 struct uloop_fd_event {
46 struct uloop_fd_stack {
47 struct uloop_fd_stack *next;
52 static struct uloop_fd_stack *fd_stack = NULL;
54 #define ULOOP_MAX_EVENTS 10
56 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
57 static struct list_head processes = LIST_HEAD_INIT(processes);
59 static int poll_fd = -1;
60 bool uloop_cancelled = false;
61 bool uloop_handle_sigchld = true;
62 static bool do_sigchld = false;
64 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
65 static int cur_fd, cur_nfds;
71 struct timespec timeout = { 0, 0 };
72 struct kevent ev = {};
81 EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
82 kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
88 static uint16_t get_flags(unsigned int flags, unsigned int mask)
96 if (flags & ULOOP_EDGE_TRIGGER)
102 static struct kevent events[ULOOP_MAX_EVENTS];
104 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
106 struct timespec timeout = { 0, 0 };
110 unsigned int changed;
113 if (flags & ULOOP_EDGE_DEFER)
114 flags &= ~ULOOP_EDGE_TRIGGER;
116 changed = flags ^ fd->flags;
117 if (changed & ULOOP_EDGE_TRIGGER)
120 if (changed & ULOOP_READ) {
121 kflags = get_flags(flags, ULOOP_READ);
122 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
125 if (changed & ULOOP_WRITE) {
126 kflags = get_flags(flags, ULOOP_WRITE);
127 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
134 if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
140 static int register_poll(struct uloop_fd *fd, unsigned int flags)
142 if (flags & ULOOP_EDGE_TRIGGER)
143 flags |= ULOOP_EDGE_DEFER;
145 flags &= ~ULOOP_EDGE_DEFER;
147 return register_kevent(fd, flags);
150 static int __uloop_fd_delete(struct uloop_fd *fd)
152 return register_poll(fd, 0);
155 static int uloop_fetch_events(int timeout)
161 ts.tv_sec = timeout / 1000;
162 ts.tv_nsec = (timeout % 1000) * 1000000;
165 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
166 for (n = 0; n < nfds; n++) {
167 struct uloop_fd_event *cur = &cur_fds[n];
168 struct uloop_fd *u = events[n].udata;
175 if (events[n].flags & EV_ERROR) {
180 if(events[n].filter == EVFILT_READ)
182 else if (events[n].filter == EVFILT_WRITE)
185 if (events[n].flags & EV_EOF)
191 if (u->flags & ULOOP_EDGE_DEFER) {
192 u->flags &= ~ULOOP_EDGE_DEFER;
193 u->flags |= ULOOP_EDGE_TRIGGER;
194 register_kevent(u, u->flags);
205 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
208 #define EPOLLRDHUP 0x2000
216 poll_fd = epoll_create(32);
220 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
224 static int register_poll(struct uloop_fd *fd, unsigned int flags)
226 struct epoll_event ev;
227 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
229 memset(&ev, 0, sizeof(struct epoll_event));
231 if (flags & ULOOP_READ)
232 ev.events |= EPOLLIN | EPOLLRDHUP;
234 if (flags & ULOOP_WRITE)
235 ev.events |= EPOLLOUT;
237 if (flags & ULOOP_EDGE_TRIGGER)
238 ev.events |= EPOLLET;
244 return epoll_ctl(poll_fd, op, fd->fd, &ev);
247 static struct epoll_event events[ULOOP_MAX_EVENTS];
249 static int __uloop_fd_delete(struct uloop_fd *sock)
252 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
255 static int uloop_fetch_events(int timeout)
259 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
260 for (n = 0; n < nfds; ++n) {
261 struct uloop_fd_event *cur = &cur_fds[n];
262 struct uloop_fd *u = events[n].data.ptr;
269 if (events[n].events & (EPOLLERR|EPOLLHUP)) {
274 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP))) {
279 if(events[n].events & EPOLLRDHUP)
282 if(events[n].events & EPOLLIN)
285 if(events[n].events & EPOLLOUT)
296 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
298 struct uloop_fd_stack *cur;
301 * Do not buffer events for level-triggered fds, they will keep firing.
302 * Caller needs to take care of recursion issues.
304 if (!(fd->flags & ULOOP_EDGE_TRIGGER))
307 for (cur = fd_stack; cur; cur = cur->next) {
314 cur->events |= events | ULOOP_EVENT_BUFFERED;
322 static void uloop_run_events(int timeout)
324 struct uloop_fd_event *cur;
329 cur_nfds = uloop_fetch_events(timeout);
334 while (cur_nfds > 0) {
335 struct uloop_fd_stack stack_cur;
338 cur = &cur_fds[cur_fd++];
342 events = cur->events;
349 if (uloop_fd_stack_event(fd, cur->events))
352 stack_cur.next = fd_stack;
354 fd_stack = &stack_cur;
356 stack_cur.events = 0;
358 events = stack_cur.events & ULOOP_EVENT_MASK;
359 } while (stack_cur.fd && events);
360 fd_stack = stack_cur.next;
366 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
371 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
372 return uloop_fd_delete(sock);
374 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
375 fl = fcntl(sock->fd, F_GETFL, 0);
377 fcntl(sock->fd, F_SETFL, fl);
380 ret = register_poll(sock, flags);
384 sock->registered = true;
391 int uloop_fd_delete(struct uloop_fd *fd)
395 for (i = 0; i < cur_nfds; i++) {
396 if (cur_fds[cur_fd + i].fd != fd)
399 cur_fds[cur_fd + i].fd = NULL;
405 fd->registered = false;
406 uloop_fd_stack_event(fd, -1);
407 return __uloop_fd_delete(fd);
410 static int tv_diff(struct timeval *t1, struct timeval *t2)
413 (t1->tv_sec - t2->tv_sec) * 1000 +
414 (t1->tv_usec - t2->tv_usec) / 1000;
417 int uloop_timeout_add(struct uloop_timeout *timeout)
419 struct uloop_timeout *tmp;
420 struct list_head *h = &timeouts;
422 if (timeout->pending)
425 list_for_each_entry(tmp, &timeouts, list) {
426 if (tv_diff(&tmp->time, &timeout->time) > 0) {
432 list_add_tail(&timeout->list, h);
433 timeout->pending = true;
438 static void uloop_gettime(struct timeval *tv)
442 clock_gettime(CLOCK_MONOTONIC, &ts);
443 tv->tv_sec = ts.tv_sec;
444 tv->tv_usec = ts.tv_nsec / 1000;
447 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
449 struct timeval *time = &timeout->time;
451 if (timeout->pending)
452 uloop_timeout_cancel(timeout);
454 uloop_gettime(&timeout->time);
456 time->tv_sec += msecs / 1000;
457 time->tv_usec += (msecs % 1000) * 1000;
459 if (time->tv_usec > 1000000) {
461 time->tv_usec %= 1000000;
464 return uloop_timeout_add(timeout);
467 int uloop_timeout_cancel(struct uloop_timeout *timeout)
469 if (!timeout->pending)
472 list_del(&timeout->list);
473 timeout->pending = false;
478 int uloop_timeout_remaining(struct uloop_timeout *timeout)
482 if (!timeout->pending)
487 return tv_diff(&timeout->time, &now);
490 int uloop_process_add(struct uloop_process *p)
492 struct uloop_process *tmp;
493 struct list_head *h = &processes;
498 list_for_each_entry(tmp, &processes, list) {
499 if (tmp->pid > p->pid) {
505 list_add_tail(&p->list, h);
511 int uloop_process_delete(struct uloop_process *p)
522 static void uloop_handle_processes(void)
524 struct uloop_process *p, *tmp;
531 pid = waitpid(-1, &ret, WNOHANG);
535 list_for_each_entry_safe(p, tmp, &processes, list) {
542 uloop_process_delete(p);
549 static void uloop_handle_sigint(int signo)
551 uloop_cancelled = true;
554 static void uloop_sigchld(int signo)
559 static void uloop_setup_signals(bool add)
561 static struct sigaction old_sigint, old_sigchld;
564 memset(&s, 0, sizeof(struct sigaction));
567 s.sa_handler = uloop_handle_sigint;
573 sigaction(SIGINT, &s, &old_sigint);
575 if (!uloop_handle_sigchld)
579 s.sa_handler = uloop_sigchld;
583 sigaction(SIGCHLD, &s, &old_sigchld);
586 static int uloop_get_next_timeout(struct timeval *tv)
588 struct uloop_timeout *timeout;
591 if (list_empty(&timeouts))
594 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
595 diff = tv_diff(&timeout->time, tv);
602 static void uloop_process_timeouts(struct timeval *tv)
604 struct uloop_timeout *t;
606 while (!list_empty(&timeouts)) {
607 t = list_first_entry(&timeouts, struct uloop_timeout, list);
609 if (tv_diff(&t->time, tv) > 0)
612 uloop_timeout_cancel(t);
618 static void uloop_clear_timeouts(void)
620 struct uloop_timeout *t, *tmp;
622 list_for_each_entry_safe(t, tmp, &timeouts, list)
623 uloop_timeout_cancel(t);
626 static void uloop_clear_processes(void)
628 struct uloop_process *p, *tmp;
630 list_for_each_entry_safe(p, tmp, &processes, list)
631 uloop_process_delete(p);
636 static int recursive_calls = 0;
640 * Handlers are only updated for the first call to uloop_run() (and restored
641 * when this call is done).
643 if (!recursive_calls++)
644 uloop_setup_signals(true);
646 while(!uloop_cancelled)
649 uloop_process_timeouts(&tv);
654 uloop_handle_processes();
656 uloop_run_events(uloop_get_next_timeout(&tv));
659 if (!--recursive_calls)
660 uloop_setup_signals(false);
663 void uloop_done(void)
671 uloop_clear_timeouts();
672 uloop_clear_processes();