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>
28 #include <sys/event.h>
31 #include <sys/epoll.h>
45 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
47 #define ULOOP_MAX_EVENTS 10
49 struct uloop_timeout *first_timeout;
51 bool uloop_cancelled = false;
65 static uint16_t get_flags(unsigned int flags, unsigned int mask)
73 if (flags & ULOOP_EDGE_TRIGGER)
79 static int register_poll(struct uloop_fd *fd, unsigned int flags)
81 struct timespec timeout = { 0, 0 };
86 changed = fd->kqflags ^ flags;
87 if (changed & ULOOP_EDGE_TRIGGER)
90 if (changed & ULOOP_READ) {
91 uint16_t kflags = get_flags(flags, ULOOP_READ);
92 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
95 if (changed & ULOOP_WRITE) {
96 uint16_t kflags = get_flags(flags, ULOOP_WRITE);
97 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
100 if (nev && (kevent(poll_fd, ev, nev, NULL, 0, &timeout) == -1))
107 int uloop_fd_delete(struct uloop_fd *sock)
109 sock->registered = false;
110 return register_poll(sock, 0);
113 static void uloop_run_events(int timeout)
115 struct kevent events[ULOOP_MAX_EVENTS];
120 ts.tv_sec = timeout / 1000;
121 ts.tv_nsec = timeout * 1000000;
124 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout > 0 ? &ts : NULL);
125 for(n = 0; n < nfds; ++n)
127 struct uloop_fd *u = events[n].udata;
130 if(events[n].flags & EV_ERROR) {
135 if(events[n].filter == EVFILT_READ)
137 else if (events[n].filter == EVFILT_WRITE)
140 if(events[n].flags & EV_EOF)
155 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
158 #define EPOLLRDHUP 0x2000
163 poll_fd = epoll_create(32);
167 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
171 static int register_poll(struct uloop_fd *fd, unsigned int flags)
173 struct epoll_event ev;
174 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
176 memset(&ev, 0, sizeof(struct epoll_event));
178 if (flags & ULOOP_READ)
179 ev.events |= EPOLLIN | EPOLLRDHUP;
181 if (flags & ULOOP_WRITE)
182 ev.events |= EPOLLOUT;
184 if (flags & ULOOP_EDGE_TRIGGER)
185 ev.events |= EPOLLET;
190 return epoll_ctl(poll_fd, op, fd->fd, &ev);
193 int uloop_fd_delete(struct uloop_fd *sock)
195 sock->registered = false;
196 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
199 static void uloop_run_events(int timeout)
201 struct epoll_event events[ULOOP_MAX_EVENTS];
204 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
205 for(n = 0; n < nfds; ++n)
207 struct uloop_fd *u = events[n].data.ptr;
210 if(events[n].events & EPOLLERR) {
215 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR)))
218 if(events[n].events & EPOLLRDHUP)
221 if(events[n].events & EPOLLIN)
224 if(events[n].events & EPOLLOUT)
234 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
239 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
240 fl = fcntl(sock->fd, F_GETFL, 0);
242 fcntl(sock->fd, F_SETFL, fl);
245 ret = register_poll(sock, flags);
249 sock->registered = true;
256 static int tv_diff(struct timeval *t1, struct timeval *t2)
258 if (t1->tv_sec != t2->tv_sec)
259 return (t1->tv_sec - t2->tv_sec) * 1000;
261 return (t1->tv_usec - t2->tv_usec) / 1000;
264 int uloop_timeout_add(struct uloop_timeout *timeout)
266 struct uloop_timeout **head = &first_timeout;
267 struct uloop_timeout *prev = NULL;
269 if (timeout->pending)
273 if (tv_diff(&(*head)->time, &timeout->time) > 0)
277 head = &(*head)->next;
280 timeout->prev = prev;
281 timeout->next = *head;
283 timeout->next->prev = timeout;
285 timeout->pending = true;
290 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
292 struct timeval *time = &timeout->time;
294 if (timeout->pending)
295 uloop_timeout_cancel(timeout);
297 gettimeofday(&timeout->time, NULL);
299 time->tv_sec += msecs / 1000;
300 time->tv_usec += msecs % 1000;
302 if (time->tv_usec > 1000000) {
304 time->tv_usec %= 100000;
307 return uloop_timeout_add(timeout);
310 int uloop_timeout_cancel(struct uloop_timeout *timeout)
312 if (!timeout->pending)
316 timeout->prev->next = timeout->next;
318 first_timeout = timeout->next;
321 timeout->next->prev = timeout->prev;
323 timeout->pending = false;
328 static void uloop_handle_sigint(int signo)
330 uloop_cancelled = true;
333 static void uloop_setup_signals(void)
336 memset(&s, 0, sizeof(struct sigaction));
337 s.sa_handler = uloop_handle_sigint;
339 sigaction(SIGINT, &s, NULL);
342 static int uloop_get_next_timeout(struct timeval *tv)
349 diff = tv_diff(&first_timeout->time, tv);
356 static void uloop_process_timeouts(struct timeval *tv)
358 struct uloop_timeout *timeout;
360 while (first_timeout) {
361 if (tv_diff(&first_timeout->time, tv) > 0)
364 timeout = first_timeout;
365 uloop_timeout_cancel(timeout);
367 timeout->cb(timeout);
373 uloop_cancelled = true;
380 uloop_setup_signals();
381 while(!uloop_cancelled)
383 gettimeofday(&tv, NULL);
384 uloop_process_timeouts(&tv);
385 uloop_run_events(uloop_get_next_timeout(&tv));
389 void uloop_done(void)