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 #define ULOOP_MAX_EVENTS 10
48 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
49 static struct list_head processes = LIST_HEAD_INIT(processes);
51 static int poll_fd = -1;
52 bool uloop_cancelled = false;
53 bool uloop_handle_sigchld = true;
54 static bool do_sigchld = false;
56 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
57 static int cur_fd, cur_nfds;
63 struct timespec timeout = { 0, 0 };
64 struct kevent ev = {};
73 EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
74 kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
80 static uint16_t get_flags(unsigned int flags, unsigned int mask)
88 if (flags & ULOOP_EDGE_TRIGGER)
94 static struct kevent events[ULOOP_MAX_EVENTS];
96 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
98 struct timespec timeout = { 0, 0 };
102 unsigned int changed;
105 if (flags & ULOOP_EDGE_DEFER)
106 flags &= ~ULOOP_EDGE_TRIGGER;
108 changed = flags ^ fd->flags;
109 if (changed & ULOOP_EDGE_TRIGGER)
112 if (changed & ULOOP_READ) {
113 kflags = get_flags(flags, ULOOP_READ);
114 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
117 if (changed & ULOOP_WRITE) {
118 kflags = get_flags(flags, ULOOP_WRITE);
119 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
126 if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
132 static int register_poll(struct uloop_fd *fd, unsigned int flags)
134 if (flags & ULOOP_EDGE_TRIGGER)
135 flags |= ULOOP_EDGE_DEFER;
137 flags &= ~ULOOP_EDGE_DEFER;
139 return register_kevent(fd, flags);
142 static int __uloop_fd_delete(struct uloop_fd *fd)
144 return register_poll(fd, 0);
147 static int uloop_fetch_events(int timeout)
153 ts.tv_sec = timeout / 1000;
154 ts.tv_nsec = (timeout % 1000) * 1000000;
157 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
158 for (n = 0; n < nfds; n++) {
159 struct uloop_fd_event *cur = &cur_fds[n];
160 struct uloop_fd *u = events[n].udata;
167 if (events[n].flags & EV_ERROR) {
172 if(events[n].filter == EVFILT_READ)
174 else if (events[n].filter == EVFILT_WRITE)
177 if (events[n].flags & EV_EOF)
182 if (u->flags & ULOOP_EDGE_DEFER) {
183 u->flags &= ~ULOOP_EDGE_DEFER;
184 u->flags |= ULOOP_EDGE_TRIGGER;
185 register_kevent(u, u->flags);
196 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
199 #define EPOLLRDHUP 0x2000
207 poll_fd = epoll_create(32);
211 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
215 static int register_poll(struct uloop_fd *fd, unsigned int flags)
217 struct epoll_event ev;
218 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
220 memset(&ev, 0, sizeof(struct epoll_event));
222 if (flags & ULOOP_READ)
223 ev.events |= EPOLLIN | EPOLLRDHUP;
225 if (flags & ULOOP_WRITE)
226 ev.events |= EPOLLOUT;
228 if (flags & ULOOP_EDGE_TRIGGER)
229 ev.events |= EPOLLET;
234 return epoll_ctl(poll_fd, op, fd->fd, &ev);
237 static struct epoll_event events[ULOOP_MAX_EVENTS];
239 static int __uloop_fd_delete(struct uloop_fd *sock)
241 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
244 static int uloop_fetch_events(int timeout)
248 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
249 for (n = 0; n < nfds; ++n) {
250 struct uloop_fd_event *cur = &cur_fds[n];
251 struct uloop_fd *u = events[n].data.ptr;
258 if (events[n].events & (EPOLLERR|EPOLLHUP)) {
263 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP))) {
268 if(events[n].events & EPOLLRDHUP)
271 if(events[n].events & EPOLLIN)
274 if(events[n].events & EPOLLOUT)
285 static void uloop_run_events(int timeout)
287 struct uloop_fd_event *cur;
292 cur_nfds = uloop_fetch_events(timeout);
297 while (cur_nfds > 0) {
298 cur = &cur_fds[cur_fd++];
308 fd->cb(fd, cur->events);
313 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
318 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
319 return uloop_fd_delete(sock);
321 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
322 fl = fcntl(sock->fd, F_GETFL, 0);
324 fcntl(sock->fd, F_SETFL, fl);
327 ret = register_poll(sock, flags);
331 sock->registered = true;
338 int uloop_fd_delete(struct uloop_fd *fd)
345 for (i = 0; i < cur_nfds; i++) {
346 if (cur_fds[cur_fd + i].fd != fd)
349 cur_fds[cur_fd + i].fd = NULL;
351 fd->registered = false;
352 return __uloop_fd_delete(fd);
355 static int tv_diff(struct timeval *t1, struct timeval *t2)
358 (t1->tv_sec - t2->tv_sec) * 1000 +
359 (t1->tv_usec - t2->tv_usec) / 1000;
362 int uloop_timeout_add(struct uloop_timeout *timeout)
364 struct uloop_timeout *tmp;
365 struct list_head *h = &timeouts;
367 if (timeout->pending)
370 list_for_each_entry(tmp, &timeouts, list) {
371 if (tv_diff(&tmp->time, &timeout->time) > 0) {
377 list_add_tail(&timeout->list, h);
378 timeout->pending = true;
383 static void uloop_gettime(struct timeval *tv)
387 clock_gettime(CLOCK_MONOTONIC, &ts);
388 tv->tv_sec = ts.tv_sec;
389 tv->tv_usec = ts.tv_nsec / 1000;
392 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
394 struct timeval *time = &timeout->time;
396 if (timeout->pending)
397 uloop_timeout_cancel(timeout);
399 uloop_gettime(&timeout->time);
401 time->tv_sec += msecs / 1000;
402 time->tv_usec += (msecs % 1000) * 1000;
404 if (time->tv_usec > 1000000) {
406 time->tv_usec %= 1000000;
409 return uloop_timeout_add(timeout);
412 int uloop_timeout_cancel(struct uloop_timeout *timeout)
414 if (!timeout->pending)
417 list_del(&timeout->list);
418 timeout->pending = false;
423 int uloop_timeout_remaining(struct uloop_timeout *timeout)
427 if (!timeout->pending)
432 return tv_diff(&timeout->time, &now);
435 int uloop_process_add(struct uloop_process *p)
437 struct uloop_process *tmp;
438 struct list_head *h = &processes;
443 list_for_each_entry(tmp, &processes, list) {
444 if (tmp->pid > p->pid) {
450 list_add_tail(&p->list, h);
456 int uloop_process_delete(struct uloop_process *p)
467 static void uloop_handle_processes(void)
469 struct uloop_process *p, *tmp;
476 pid = waitpid(-1, &ret, WNOHANG);
480 list_for_each_entry_safe(p, tmp, &processes, list) {
487 uloop_process_delete(p);
494 static void uloop_handle_sigint(int signo)
496 uloop_cancelled = true;
499 static void uloop_sigchld(int signo)
504 static void uloop_setup_signals(void)
508 memset(&s, 0, sizeof(struct sigaction));
509 s.sa_handler = uloop_handle_sigint;
511 sigaction(SIGINT, &s, NULL);
513 if (uloop_handle_sigchld) {
514 s.sa_handler = uloop_sigchld;
515 sigaction(SIGCHLD, &s, NULL);
519 static int uloop_get_next_timeout(struct timeval *tv)
521 struct uloop_timeout *timeout;
524 if (list_empty(&timeouts))
527 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
528 diff = tv_diff(&timeout->time, tv);
535 static void uloop_process_timeouts(struct timeval *tv)
537 struct uloop_timeout *t;
539 while (!list_empty(&timeouts)) {
540 t = list_first_entry(&timeouts, struct uloop_timeout, list);
542 if (tv_diff(&t->time, tv) > 0)
545 uloop_timeout_cancel(t);
551 static void uloop_clear_timeouts(void)
553 struct uloop_timeout *t, *tmp;
555 list_for_each_entry_safe(t, tmp, &timeouts, list)
556 uloop_timeout_cancel(t);
559 static void uloop_clear_processes(void)
561 struct uloop_process *p, *tmp;
563 list_for_each_entry_safe(p, tmp, &processes, list)
564 uloop_process_delete(p);
571 uloop_setup_signals();
572 while(!uloop_cancelled)
575 uloop_process_timeouts(&tv);
580 uloop_handle_processes();
581 uloop_run_events(uloop_get_next_timeout(&tv));
585 void uloop_done(void)
593 uloop_clear_timeouts();
594 uloop_clear_processes();