2 * uloop - event loop implementation
4 * Copyright (C) 2010-2016 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 static bool do_sigchld = false;
63 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
64 static int cur_fd, cur_nfds;
67 #include "uloop-kqueue.c"
71 #include "uloop-epoll.c"
74 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
76 struct uloop_fd_stack *cur;
79 * Do not buffer events for level-triggered fds, they will keep firing.
80 * Caller needs to take care of recursion issues.
82 if (!(fd->flags & ULOOP_EDGE_TRIGGER))
85 for (cur = fd_stack; cur; cur = cur->next) {
92 cur->events |= events | ULOOP_EVENT_BUFFERED;
100 static void uloop_run_events(int timeout)
102 struct uloop_fd_event *cur;
107 cur_nfds = uloop_fetch_events(timeout);
112 while (cur_nfds > 0) {
113 struct uloop_fd_stack stack_cur;
116 cur = &cur_fds[cur_fd++];
120 events = cur->events;
127 if (uloop_fd_stack_event(fd, cur->events))
130 stack_cur.next = fd_stack;
132 fd_stack = &stack_cur;
134 stack_cur.events = 0;
136 events = stack_cur.events & ULOOP_EVENT_MASK;
137 } while (stack_cur.fd && events);
138 fd_stack = stack_cur.next;
144 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
149 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
150 return uloop_fd_delete(sock);
152 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
153 fl = fcntl(sock->fd, F_GETFL, 0);
155 fcntl(sock->fd, F_SETFL, fl);
158 ret = register_poll(sock, flags);
162 sock->registered = true;
170 int uloop_fd_delete(struct uloop_fd *fd)
174 for (i = 0; i < cur_nfds; i++) {
175 if (cur_fds[cur_fd + i].fd != fd)
178 cur_fds[cur_fd + i].fd = NULL;
184 fd->registered = false;
185 uloop_fd_stack_event(fd, -1);
186 return __uloop_fd_delete(fd);
189 static int tv_diff(struct timeval *t1, struct timeval *t2)
192 (t1->tv_sec - t2->tv_sec) * 1000 +
193 (t1->tv_usec - t2->tv_usec) / 1000;
196 int uloop_timeout_add(struct uloop_timeout *timeout)
198 struct uloop_timeout *tmp;
199 struct list_head *h = &timeouts;
201 if (timeout->pending)
204 list_for_each_entry(tmp, &timeouts, list) {
205 if (tv_diff(&tmp->time, &timeout->time) > 0) {
211 list_add_tail(&timeout->list, h);
212 timeout->pending = true;
217 static void uloop_gettime(struct timeval *tv)
221 clock_gettime(CLOCK_MONOTONIC, &ts);
222 tv->tv_sec = ts.tv_sec;
223 tv->tv_usec = ts.tv_nsec / 1000;
226 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
228 struct timeval *time = &timeout->time;
230 if (timeout->pending)
231 uloop_timeout_cancel(timeout);
235 time->tv_sec += msecs / 1000;
236 time->tv_usec += (msecs % 1000) * 1000;
238 if (time->tv_usec > 1000000) {
240 time->tv_usec -= 1000000;
243 return uloop_timeout_add(timeout);
246 int uloop_timeout_cancel(struct uloop_timeout *timeout)
248 if (!timeout->pending)
251 list_del(&timeout->list);
252 timeout->pending = false;
257 int uloop_timeout_remaining(struct uloop_timeout *timeout)
261 if (!timeout->pending)
266 return tv_diff(&timeout->time, &now);
269 int uloop_process_add(struct uloop_process *p)
271 struct uloop_process *tmp;
272 struct list_head *h = &processes;
277 list_for_each_entry(tmp, &processes, list) {
278 if (tmp->pid > p->pid) {
284 list_add_tail(&p->list, h);
290 int uloop_process_delete(struct uloop_process *p)
301 static void uloop_handle_processes(void)
303 struct uloop_process *p, *tmp;
310 pid = waitpid(-1, &ret, WNOHANG);
314 list_for_each_entry_safe(p, tmp, &processes, list) {
321 uloop_process_delete(p);
328 static void uloop_handle_sigint(int signo)
330 uloop_cancelled = true;
333 static void uloop_sigchld(int signo)
338 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
341 struct sigaction *act;
344 sigaction(signum, NULL, &s);
347 if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
348 memcpy(old, &s, sizeof(struct sigaction));
349 s.sa_handler = handler;
354 else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
359 sigaction(signum, act, NULL);
362 static void uloop_ignore_signal(int signum, bool ignore)
365 void *new_handler = NULL;
367 sigaction(signum, NULL, &s);
370 if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
371 new_handler = SIG_IGN;
373 if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
374 new_handler = SIG_DFL;
378 s.sa_handler = new_handler;
380 sigaction(signum, &s, NULL);
384 static void uloop_setup_signals(bool add)
386 static struct sigaction old_sigint, old_sigchld, old_sigterm;
388 uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
389 uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
390 uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
392 uloop_ignore_signal(SIGPIPE, add);
395 static int uloop_get_next_timeout(struct timeval *tv)
397 struct uloop_timeout *timeout;
400 if (list_empty(&timeouts))
403 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
404 diff = tv_diff(&timeout->time, tv);
411 static void uloop_process_timeouts(struct timeval *tv)
413 struct uloop_timeout *t;
415 while (!list_empty(&timeouts)) {
416 t = list_first_entry(&timeouts, struct uloop_timeout, list);
418 if (tv_diff(&t->time, tv) > 0)
421 uloop_timeout_cancel(t);
427 static void uloop_clear_timeouts(void)
429 struct uloop_timeout *t, *tmp;
431 list_for_each_entry_safe(t, tmp, &timeouts, list)
432 uloop_timeout_cancel(t);
435 static void uloop_clear_processes(void)
437 struct uloop_process *p, *tmp;
439 list_for_each_entry_safe(p, tmp, &processes, list)
440 uloop_process_delete(p);
445 static int recursive_calls = 0;
449 * Handlers are only updated for the first call to uloop_run() (and restored
450 * when this call is done).
452 if (!recursive_calls++)
453 uloop_setup_signals(true);
455 uloop_cancelled = false;
456 while(!uloop_cancelled)
459 uloop_process_timeouts(&tv);
462 uloop_handle_processes();
468 uloop_run_events(uloop_get_next_timeout(&tv));
471 if (!--recursive_calls)
472 uloop_setup_signals(false);
475 void uloop_done(void)
483 uloop_clear_timeouts();
484 uloop_clear_processes();