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>
24 #include <sys/epoll.h>
39 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
42 #define EPOLLRDHUP 0x2000
46 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
49 struct uloop_timeout *first_timeout;
53 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
55 struct epoll_event ev;
56 int op = sock->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
60 fl = fcntl(sock->fd, F_GETFL, 0);
62 fcntl(sock->fd, F_SETFL, fl);
64 memset(&ev, 0, sizeof(struct epoll_event));
66 if (flags & ULOOP_READ)
67 ev.events |= EPOLLIN | EPOLLRDHUP;
69 if (flags & ULOOP_WRITE)
70 ev.events |= EPOLLOUT;
72 if (flags & ULOOP_EDGE_TRIGGER)
75 ev.data.fd = sock->fd;
78 ret = epoll_ctl(epoll_fd, op, sock->fd, &ev);
82 sock->registered = true;
89 int uloop_fd_delete(struct uloop_fd *sock)
91 sock->registered = false;
92 return epoll_ctl(epoll_fd, EPOLL_CTL_DEL, sock->fd, 0);
95 static int tv_diff(struct timeval *t1, struct timeval *t2)
97 if (t1->tv_sec != t2->tv_sec)
98 return (t1->tv_sec - t2->tv_sec) * 1000;
100 return (t1->tv_usec - t2->tv_usec) / 1000;
103 int uloop_timeout_add(struct uloop_timeout *timeout)
105 struct uloop_timeout **head = &first_timeout;
106 struct uloop_timeout *prev = NULL;
108 if (timeout->pending)
112 if (tv_diff(&(*head)->time, &timeout->time) > 0)
116 head = &(*head)->next;
119 timeout->prev = prev;
120 timeout->next = *head;
122 timeout->pending = true;
127 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
129 struct timeval *time = &timeout->time;
131 if (timeout->pending)
132 uloop_timeout_cancel(timeout);
134 gettimeofday(&timeout->time, NULL);
136 time->tv_sec += msecs / 1000;
137 time->tv_usec += msecs % 1000;
139 if (time->tv_usec > 1000000) {
141 time->tv_usec %= 100000;
144 return uloop_timeout_add(timeout);
147 int uloop_timeout_cancel(struct uloop_timeout *timeout)
149 if (!timeout->pending)
153 timeout->prev->next = timeout->next;
155 first_timeout = timeout->next;
158 timeout->next->prev = timeout->prev;
160 timeout->pending = false;
165 static void uloop_handle_sigint(int signo)
170 static void uloop_setup_signals(void)
173 memset(&s, 0, sizeof(struct sigaction));
174 s.sa_handler = uloop_handle_sigint;
176 sigaction(SIGINT, &s, NULL);
179 static int uloop_get_next_timeout(struct timeval *tv)
186 diff = tv_diff(&first_timeout->time, tv);
193 static void uloop_process_timeouts(struct timeval *tv)
195 struct uloop_timeout *timeout;
197 while (first_timeout) {
198 if (tv_diff(&first_timeout->time, tv) > 0)
201 timeout = first_timeout;
202 uloop_timeout_cancel(timeout);
204 timeout->cb(timeout);
215 epoll_fd = epoll_create(32);
219 fcntl(epoll_fd, F_SETFD, fcntl(epoll_fd, F_GETFD) | FD_CLOEXEC);
225 struct epoll_event events[10];
230 uloop_setup_signals();
233 gettimeofday(&tv, NULL);
234 uloop_process_timeouts(&tv);
235 timeout = uloop_get_next_timeout(&tv);
236 nfds = epoll_wait(epoll_fd, events, ARRAY_SIZE(events), timeout);
237 for(n = 0; n < nfds; ++n)
239 struct uloop_fd *u = events[n].data.ptr;
242 if(events[n].events & EPOLLERR) {
247 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR)))
250 if(events[n].events & EPOLLRDHUP)
253 if(events[n].events & EPOLLIN)
256 if(events[n].events & EPOLLOUT)
265 void uloop_done(void)