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)
398 for (i = 0; i < cur_nfds; i++) {
399 if (cur_fds[cur_fd + i].fd != fd)
402 cur_fds[cur_fd + i].fd = NULL;
404 fd->registered = false;
405 uloop_fd_stack_event(fd, -1);
406 return __uloop_fd_delete(fd);
409 static int tv_diff(struct timeval *t1, struct timeval *t2)
412 (t1->tv_sec - t2->tv_sec) * 1000 +
413 (t1->tv_usec - t2->tv_usec) / 1000;
416 int uloop_timeout_add(struct uloop_timeout *timeout)
418 struct uloop_timeout *tmp;
419 struct list_head *h = &timeouts;
421 if (timeout->pending)
424 list_for_each_entry(tmp, &timeouts, list) {
425 if (tv_diff(&tmp->time, &timeout->time) > 0) {
431 list_add_tail(&timeout->list, h);
432 timeout->pending = true;
437 static void uloop_gettime(struct timeval *tv)
441 clock_gettime(CLOCK_MONOTONIC, &ts);
442 tv->tv_sec = ts.tv_sec;
443 tv->tv_usec = ts.tv_nsec / 1000;
446 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
448 struct timeval *time = &timeout->time;
450 if (timeout->pending)
451 uloop_timeout_cancel(timeout);
453 uloop_gettime(&timeout->time);
455 time->tv_sec += msecs / 1000;
456 time->tv_usec += (msecs % 1000) * 1000;
458 if (time->tv_usec > 1000000) {
460 time->tv_usec %= 1000000;
463 return uloop_timeout_add(timeout);
466 int uloop_timeout_cancel(struct uloop_timeout *timeout)
468 if (!timeout->pending)
471 list_del(&timeout->list);
472 timeout->pending = false;
477 int uloop_timeout_remaining(struct uloop_timeout *timeout)
481 if (!timeout->pending)
486 return tv_diff(&timeout->time, &now);
489 int uloop_process_add(struct uloop_process *p)
491 struct uloop_process *tmp;
492 struct list_head *h = &processes;
497 list_for_each_entry(tmp, &processes, list) {
498 if (tmp->pid > p->pid) {
504 list_add_tail(&p->list, h);
510 int uloop_process_delete(struct uloop_process *p)
521 static void uloop_handle_processes(void)
523 struct uloop_process *p, *tmp;
530 pid = waitpid(-1, &ret, WNOHANG);
534 list_for_each_entry_safe(p, tmp, &processes, list) {
541 uloop_process_delete(p);
548 static void uloop_handle_sigint(int signo)
550 uloop_cancelled = true;
553 static void uloop_sigchld(int signo)
558 static void uloop_setup_signals(void)
562 memset(&s, 0, sizeof(struct sigaction));
563 s.sa_handler = uloop_handle_sigint;
565 sigaction(SIGINT, &s, NULL);
567 if (uloop_handle_sigchld) {
568 s.sa_handler = uloop_sigchld;
569 sigaction(SIGCHLD, &s, NULL);
573 static int uloop_get_next_timeout(struct timeval *tv)
575 struct uloop_timeout *timeout;
578 if (list_empty(&timeouts))
581 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
582 diff = tv_diff(&timeout->time, tv);
589 static void uloop_process_timeouts(struct timeval *tv)
591 struct uloop_timeout *t;
593 while (!list_empty(&timeouts)) {
594 t = list_first_entry(&timeouts, struct uloop_timeout, list);
596 if (tv_diff(&t->time, tv) > 0)
599 uloop_timeout_cancel(t);
605 static void uloop_clear_timeouts(void)
607 struct uloop_timeout *t, *tmp;
609 list_for_each_entry_safe(t, tmp, &timeouts, list)
610 uloop_timeout_cancel(t);
613 static void uloop_clear_processes(void)
615 struct uloop_process *p, *tmp;
617 list_for_each_entry_safe(p, tmp, &processes, list)
618 uloop_process_delete(p);
625 uloop_setup_signals();
626 while(!uloop_cancelled)
629 uloop_process_timeouts(&tv);
634 uloop_handle_processes();
636 uloop_run_events(uloop_get_next_timeout(&tv));
640 void uloop_done(void)
648 uloop_clear_timeouts();
649 uloop_clear_processes();