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>
35 #include <sys/event.h>
38 #include <sys/epoll.h>
39 #include <sys/timerfd.h>
43 struct uloop_fd_event
{
48 struct uloop_fd_stack
{
49 struct uloop_fd_stack
*next
;
54 static struct uloop_fd_stack
*fd_stack
= NULL
;
56 #define ULOOP_MAX_EVENTS 10
58 static struct list_head timeouts
= LIST_HEAD_INIT(timeouts
);
59 static struct list_head processes
= LIST_HEAD_INIT(processes
);
61 static int poll_fd
= -1;
62 bool uloop_cancelled
= false;
63 bool uloop_handle_sigchld
= true;
64 static int uloop_status
= 0;
65 static bool do_sigchld
= false;
67 static struct uloop_fd_event cur_fds
[ULOOP_MAX_EVENTS
];
68 static int cur_fd
, cur_nfds
;
69 static int uloop_run_depth
= 0;
71 uloop_fd_handler uloop_fd_set_cb
= NULL
;
73 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
);
76 #include "uloop-kqueue.c"
80 #include "uloop-epoll.c"
83 static void waker_consume(struct uloop_fd
*fd
, unsigned int events
)
87 while (read(fd
->fd
, buf
, 4) > 0)
91 static int waker_pipe
= -1;
92 static struct uloop_fd waker_fd
= {
97 static void waker_init_fd(int fd
)
99 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
100 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
103 static int waker_init(void)
113 waker_init_fd(fds
[0]);
114 waker_init_fd(fds
[1]);
117 waker_fd
.fd
= fds
[0];
118 waker_fd
.cb
= waker_consume
;
119 uloop_fd_add(&waker_fd
, ULOOP_READ
);
124 static void uloop_setup_signals(bool add
);
128 if (uloop_init_pollfd() < 0)
131 if (waker_init() < 0) {
136 uloop_setup_signals(true);
141 static bool uloop_fd_stack_event(struct uloop_fd
*fd
, int events
)
143 struct uloop_fd_stack
*cur
;
146 * Do not buffer events for level-triggered fds, they will keep firing.
147 * Caller needs to take care of recursion issues.
149 if (!(fd
->flags
& ULOOP_EDGE_TRIGGER
))
152 for (cur
= fd_stack
; cur
; cur
= cur
->next
) {
159 cur
->events
|= events
| ULOOP_EVENT_BUFFERED
;
167 static void uloop_run_events(int64_t timeout
)
169 struct uloop_fd_event
*cur
;
174 cur_nfds
= uloop_fetch_events(timeout
);
179 while (cur_nfds
> 0) {
180 struct uloop_fd_stack stack_cur
;
183 cur
= &cur_fds
[cur_fd
++];
187 events
= cur
->events
;
194 if (uloop_fd_stack_event(fd
, cur
->events
))
197 stack_cur
.next
= fd_stack
;
199 fd_stack
= &stack_cur
;
201 stack_cur
.events
= 0;
203 events
= stack_cur
.events
& ULOOP_EVENT_MASK
;
204 } while (stack_cur
.fd
&& events
);
205 fd_stack
= stack_cur
.next
;
211 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
216 if (!(flags
& (ULOOP_READ
| ULOOP_WRITE
)))
217 return uloop_fd_delete(sock
);
219 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
220 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
222 fcntl(sock
->fd
, F_SETFL
, fl
);
225 ret
= register_poll(sock
, flags
);
230 uloop_fd_set_cb(sock
, flags
);
233 sock
->registered
= true;
241 int uloop_fd_delete(struct uloop_fd
*fd
)
245 for (i
= 0; i
< cur_nfds
; i
++) {
246 if (cur_fds
[cur_fd
+ i
].fd
!= fd
)
249 cur_fds
[cur_fd
+ i
].fd
= NULL
;
256 uloop_fd_set_cb(fd
, 0);
258 fd
->registered
= false;
260 uloop_fd_stack_event(fd
, -1);
261 return __uloop_fd_delete(fd
);
264 static int64_t tv_diff(struct timeval
*t1
, struct timeval
*t2
)
267 (t1
->tv_sec
- t2
->tv_sec
) * 1000 +
268 (t1
->tv_usec
- t2
->tv_usec
) / 1000;
271 int uloop_timeout_add(struct uloop_timeout
*timeout
)
273 struct uloop_timeout
*tmp
;
274 struct list_head
*h
= &timeouts
;
276 if (timeout
->pending
)
279 list_for_each_entry(tmp
, &timeouts
, list
) {
280 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
286 list_add_tail(&timeout
->list
, h
);
287 timeout
->pending
= true;
292 static void uloop_gettime(struct timeval
*tv
)
296 clock_gettime(CLOCK_MONOTONIC
, &ts
);
297 tv
->tv_sec
= ts
.tv_sec
;
298 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
301 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
303 struct timeval
*time
= &timeout
->time
;
305 if (timeout
->pending
)
306 uloop_timeout_cancel(timeout
);
310 time
->tv_sec
+= msecs
/ 1000;
311 time
->tv_usec
+= (msecs
% 1000) * 1000;
313 if (time
->tv_usec
> 1000000) {
315 time
->tv_usec
-= 1000000;
318 return uloop_timeout_add(timeout
);
321 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
323 if (!timeout
->pending
)
326 list_del(&timeout
->list
);
327 timeout
->pending
= false;
332 int uloop_timeout_remaining(struct uloop_timeout
*timeout
)
337 if (!timeout
->pending
)
342 td
= tv_diff(&timeout
->time
, &now
);
346 else if (td
< INT_MIN
)
352 int64_t uloop_timeout_remaining64(struct uloop_timeout
*timeout
)
356 if (!timeout
->pending
)
361 return tv_diff(&timeout
->time
, &now
);
364 int uloop_process_add(struct uloop_process
*p
)
366 struct uloop_process
*tmp
;
367 struct list_head
*h
= &processes
;
372 list_for_each_entry(tmp
, &processes
, list
) {
373 if (tmp
->pid
> p
->pid
) {
379 list_add_tail(&p
->list
, h
);
385 int uloop_process_delete(struct uloop_process
*p
)
396 static void uloop_handle_processes(void)
398 struct uloop_process
*p
, *tmp
;
405 pid
= waitpid(-1, &ret
, WNOHANG
);
406 if (pid
< 0 && errno
== EINTR
)
412 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
419 uloop_process_delete(p
);
426 int uloop_interval_set(struct uloop_interval
*timer
, unsigned int msecs
)
428 return timer_register(timer
, msecs
);
431 int uloop_interval_cancel(struct uloop_interval
*timer
)
433 return timer_remove(timer
);
436 int64_t uloop_interval_remaining(struct uloop_interval
*timer
)
438 return timer_next(timer
);
441 static void uloop_signal_wake(void)
444 if (write(waker_pipe
, "w", 1) < 0) {
452 static void uloop_handle_sigint(int signo
)
454 uloop_status
= signo
;
455 uloop_cancelled
= true;
459 static void uloop_sigchld(int signo
)
465 static void uloop_install_handler(int signum
, void (*handler
)(int), struct sigaction
* old
, bool add
)
468 struct sigaction
*act
;
471 sigaction(signum
, NULL
, &s
);
474 if (s
.sa_handler
== SIG_DFL
) { /* Do not override existing custom signal handlers */
475 memcpy(old
, &s
, sizeof(struct sigaction
));
476 s
.sa_handler
= handler
;
481 else if (s
.sa_handler
== handler
) { /* Do not restore if someone modified our handler */
486 sigaction(signum
, act
, NULL
);
489 static void uloop_ignore_signal(int signum
, bool ignore
)
492 void *new_handler
= NULL
;
494 sigaction(signum
, NULL
, &s
);
497 if (s
.sa_handler
== SIG_DFL
) /* Ignore only if there isn't any custom handler */
498 new_handler
= SIG_IGN
;
500 if (s
.sa_handler
== SIG_IGN
) /* Restore only if noone modified our SIG_IGN */
501 new_handler
= SIG_DFL
;
505 s
.sa_handler
= new_handler
;
507 sigaction(signum
, &s
, NULL
);
511 static void uloop_setup_signals(bool add
)
513 static struct sigaction old_sigint
, old_sigchld
, old_sigterm
;
515 uloop_install_handler(SIGINT
, uloop_handle_sigint
, &old_sigint
, add
);
516 uloop_install_handler(SIGTERM
, uloop_handle_sigint
, &old_sigterm
, add
);
518 if (uloop_handle_sigchld
)
519 uloop_install_handler(SIGCHLD
, uloop_sigchld
, &old_sigchld
, add
);
521 uloop_ignore_signal(SIGPIPE
, add
);
524 int uloop_get_next_timeout(void)
526 struct uloop_timeout
*timeout
;
530 if (list_empty(&timeouts
))
535 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
536 diff
= tv_diff(&timeout
->time
, &tv
);
545 static void uloop_process_timeouts(void)
547 struct uloop_timeout
*t
;
550 if (list_empty(&timeouts
))
554 while (!list_empty(&timeouts
)) {
555 t
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
557 if (tv_diff(&t
->time
, &tv
) > 0)
560 uloop_timeout_cancel(t
);
566 static void uloop_clear_timeouts(void)
568 struct uloop_timeout
*t
, *tmp
;
570 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
571 uloop_timeout_cancel(t
);
574 static void uloop_clear_processes(void)
576 struct uloop_process
*p
, *tmp
;
578 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
579 uloop_process_delete(p
);
582 bool uloop_cancelling(void)
584 return uloop_run_depth
> 0 && uloop_cancelled
;
587 int uloop_run_timeout(int timeout
)
594 uloop_cancelled
= false;
596 uloop_process_timeouts();
599 uloop_handle_processes();
604 next_time
= uloop_get_next_timeout();
605 if (timeout
>= 0 && (next_time
< 0 || timeout
< next_time
))
607 uloop_run_events(next_time
);
608 } while (!uloop_cancelled
&& timeout
< 0);
615 void uloop_done(void)
617 uloop_setup_signals(false);
624 if (waker_pipe
>= 0) {
625 uloop_fd_delete(&waker_fd
);
631 uloop_clear_timeouts();
632 uloop_clear_processes();