2 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
4 * Copyright (C) 2010 Steven Barth <steven@midlink.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
38 #include <sys/event.h>
41 #include <sys/epoll.h>
45 #define ULOOP_MAX_EVENTS 10
47 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
48 static struct list_head processes = LIST_HEAD_INIT(processes);
50 static int poll_fd = -1;
51 bool uloop_cancelled = false;
52 bool uloop_handle_sigchld = true;
53 static bool do_sigchld = false;
54 static int cur_fd, cur_nfds;
60 struct timespec timeout = { 0, 0 };
61 struct kevent ev = {};
70 EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
71 kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
77 static uint16_t get_flags(unsigned int flags, unsigned int mask)
85 if (flags & ULOOP_EDGE_TRIGGER)
91 static struct kevent events[ULOOP_MAX_EVENTS];
93 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
95 struct timespec timeout = { 0, 0 };
102 if (flags & ULOOP_EDGE_DEFER)
103 flags &= ~ULOOP_EDGE_TRIGGER;
105 changed = flags ^ fd->flags;
106 if (changed & ULOOP_EDGE_TRIGGER)
109 if (changed & ULOOP_READ) {
110 kflags = get_flags(flags, ULOOP_READ);
111 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
114 if (changed & ULOOP_WRITE) {
115 kflags = get_flags(flags, ULOOP_WRITE);
116 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
123 if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
129 static int register_poll(struct uloop_fd *fd, unsigned int flags)
131 if (flags & ULOOP_EDGE_TRIGGER)
132 flags |= ULOOP_EDGE_DEFER;
134 flags &= ~ULOOP_EDGE_DEFER;
136 return register_kevent(fd, flags);
139 int uloop_fd_delete(struct uloop_fd *sock)
143 for (i = cur_fd + 1; i < cur_nfds; i++) {
144 if (events[i].udata != sock)
147 events[i].udata = NULL;
150 sock->registered = false;
151 return register_poll(sock, 0);
154 static void uloop_run_events(int timeout)
160 ts.tv_sec = timeout / 1000;
161 ts.tv_nsec = (timeout % 1000) * 1000000;
164 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
165 for(n = 0; n < nfds; ++n)
167 struct uloop_fd *u = events[n].udata;
173 if (events[n].flags & EV_ERROR) {
178 if(events[n].filter == EVFILT_READ)
180 else if (events[n].filter == EVFILT_WRITE)
183 if (events[n].flags & EV_EOF)
192 if (u->flags & ULOOP_EDGE_DEFER) {
193 u->flags &= ~ULOOP_EDGE_DEFER;
194 register_kevent(u, u->flags);
206 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
209 #define EPOLLRDHUP 0x2000
217 poll_fd = epoll_create(32);
221 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
225 static int register_poll(struct uloop_fd *fd, unsigned int flags)
227 struct epoll_event ev;
228 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
230 memset(&ev, 0, sizeof(struct epoll_event));
232 if (flags & ULOOP_READ)
233 ev.events |= EPOLLIN | EPOLLRDHUP;
235 if (flags & ULOOP_WRITE)
236 ev.events |= EPOLLOUT;
238 if (flags & ULOOP_EDGE_TRIGGER)
239 ev.events |= EPOLLET;
244 return epoll_ctl(poll_fd, op, fd->fd, &ev);
247 static struct epoll_event events[ULOOP_MAX_EVENTS];
249 int uloop_fd_delete(struct uloop_fd *sock)
253 for (i = cur_fd + 1; i < cur_nfds; i++) {
254 if (events[i].data.ptr != sock)
257 events[i].data.ptr = NULL;
259 sock->registered = false;
260 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
263 static void uloop_run_events(int timeout)
267 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
268 for(n = 0; n < nfds; ++n)
270 struct uloop_fd *u = events[n].data.ptr;
276 if(events[n].events & (EPOLLERR|EPOLLHUP)) {
281 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)))
284 if(events[n].events & EPOLLRDHUP)
287 if(events[n].events & EPOLLIN)
290 if(events[n].events & EPOLLOUT)
304 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
309 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
310 fl = fcntl(sock->fd, F_GETFL, 0);
312 fcntl(sock->fd, F_SETFL, fl);
315 ret = register_poll(sock, flags);
319 sock->registered = true;
326 static int tv_diff(struct timeval *t1, struct timeval *t2)
329 (t1->tv_sec - t2->tv_sec) * 1000 +
330 (t1->tv_usec - t2->tv_usec) / 1000;
333 int uloop_timeout_add(struct uloop_timeout *timeout)
335 struct uloop_timeout *tmp;
336 struct list_head *h = &timeouts;
338 if (timeout->pending)
341 list_for_each_entry(tmp, &timeouts, list) {
342 if (tv_diff(&tmp->time, &timeout->time) > 0) {
348 list_add_tail(&timeout->list, h);
349 timeout->pending = true;
354 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
356 struct timeval *time = &timeout->time;
358 if (timeout->pending)
359 uloop_timeout_cancel(timeout);
361 gettimeofday(&timeout->time, NULL);
363 time->tv_sec += msecs / 1000;
364 time->tv_usec += (msecs % 1000) * 1000;
366 if (time->tv_usec > 1000000) {
368 time->tv_usec %= 1000000;
371 return uloop_timeout_add(timeout);
374 int uloop_timeout_cancel(struct uloop_timeout *timeout)
376 if (!timeout->pending)
379 list_del(&timeout->list);
380 timeout->pending = false;
385 int uloop_process_add(struct uloop_process *p)
387 struct uloop_process *tmp;
388 struct list_head *h = &processes;
393 list_for_each_entry(tmp, &processes, list) {
394 if (tmp->pid > p->pid) {
400 list_add_tail(&p->list, h);
406 int uloop_process_delete(struct uloop_process *p)
417 static void uloop_handle_processes(void)
419 struct uloop_process *p, *tmp;
426 pid = waitpid(-1, &ret, WNOHANG);
430 list_for_each_entry_safe(p, tmp, &processes, list) {
437 uloop_process_delete(p);
444 static void uloop_handle_sigint(int signo)
446 uloop_cancelled = true;
449 static void uloop_sigchld(int signo)
454 static void uloop_setup_signals(void)
458 memset(&s, 0, sizeof(struct sigaction));
459 s.sa_handler = uloop_handle_sigint;
461 sigaction(SIGINT, &s, NULL);
463 if (uloop_handle_sigchld) {
464 s.sa_handler = uloop_sigchld;
465 sigaction(SIGCHLD, &s, NULL);
469 static int uloop_get_next_timeout(struct timeval *tv)
471 struct uloop_timeout *timeout;
474 if (list_empty(&timeouts))
477 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
478 diff = tv_diff(&timeout->time, tv);
485 static void uloop_process_timeouts(struct timeval *tv)
487 struct uloop_timeout *t;
489 while (!list_empty(&timeouts)) {
490 t = list_first_entry(&timeouts, struct uloop_timeout, list);
492 if (tv_diff(&t->time, tv) > 0)
495 uloop_timeout_cancel(t);
501 static void uloop_clear_timeouts(void)
503 struct uloop_timeout *t, *tmp;
505 list_for_each_entry_safe(t, tmp, &timeouts, list)
506 uloop_timeout_cancel(t);
509 static void uloop_clear_processes(void)
511 struct uloop_process *p, *tmp;
513 list_for_each_entry_safe(p, tmp, &processes, list)
514 uloop_process_delete(p);
521 uloop_setup_signals();
522 while(!uloop_cancelled)
524 gettimeofday(&tv, NULL);
525 uloop_process_timeouts(&tv);
530 uloop_handle_processes();
531 uloop_run_events(uloop_get_next_timeout(&tv));
535 void uloop_done(void)
543 uloop_clear_timeouts();
544 uloop_clear_processes();