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>
42 struct uloop_fd_event
{
47 struct uloop_fd_stack
{
48 struct uloop_fd_stack
*next
;
53 static struct uloop_fd_stack
*fd_stack
= NULL
;
55 #define ULOOP_MAX_EVENTS 10
57 static struct list_head timeouts
= LIST_HEAD_INIT(timeouts
);
58 static struct list_head processes
= LIST_HEAD_INIT(processes
);
60 static int poll_fd
= -1;
61 bool uloop_cancelled
= false;
62 bool uloop_handle_sigchld
= true;
63 static int uloop_status
= 0;
64 static bool do_sigchld
= false;
66 static struct uloop_fd_event cur_fds
[ULOOP_MAX_EVENTS
];
67 static int cur_fd
, cur_nfds
;
68 static int uloop_run_depth
= 0;
70 uloop_fd_handler uloop_fd_set_cb
= NULL
;
72 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
);
75 #include "uloop-kqueue.c"
79 #include "uloop-epoll.c"
82 static void waker_consume(struct uloop_fd
*fd
, unsigned int events
)
86 while (read(fd
->fd
, buf
, 4) > 0)
90 static int waker_pipe
= -1;
91 static struct uloop_fd waker_fd
= {
96 static void waker_init_fd(int fd
)
98 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
99 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
102 static int waker_init(void)
112 waker_init_fd(fds
[0]);
113 waker_init_fd(fds
[1]);
116 waker_fd
.fd
= fds
[0];
117 waker_fd
.cb
= waker_consume
;
118 uloop_fd_add(&waker_fd
, ULOOP_READ
);
123 static void uloop_setup_signals(bool add
);
127 if (uloop_init_pollfd() < 0)
130 if (waker_init() < 0) {
135 uloop_setup_signals(true);
140 static bool uloop_fd_stack_event(struct uloop_fd
*fd
, int events
)
142 struct uloop_fd_stack
*cur
;
145 * Do not buffer events for level-triggered fds, they will keep firing.
146 * Caller needs to take care of recursion issues.
148 if (!(fd
->flags
& ULOOP_EDGE_TRIGGER
))
151 for (cur
= fd_stack
; cur
; cur
= cur
->next
) {
158 cur
->events
|= events
| ULOOP_EVENT_BUFFERED
;
166 static void uloop_run_events(int64_t timeout
)
168 struct uloop_fd_event
*cur
;
173 cur_nfds
= uloop_fetch_events(timeout
);
178 while (cur_nfds
> 0) {
179 struct uloop_fd_stack stack_cur
;
182 cur
= &cur_fds
[cur_fd
++];
186 events
= cur
->events
;
193 if (uloop_fd_stack_event(fd
, cur
->events
))
196 stack_cur
.next
= fd_stack
;
198 fd_stack
= &stack_cur
;
200 stack_cur
.events
= 0;
202 events
= stack_cur
.events
& ULOOP_EVENT_MASK
;
203 } while (stack_cur
.fd
&& events
);
204 fd_stack
= stack_cur
.next
;
210 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
215 if (!(flags
& (ULOOP_READ
| ULOOP_WRITE
)))
216 return uloop_fd_delete(sock
);
218 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
219 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
221 fcntl(sock
->fd
, F_SETFL
, fl
);
224 ret
= register_poll(sock
, flags
);
229 uloop_fd_set_cb(sock
, flags
);
232 sock
->registered
= true;
240 int uloop_fd_delete(struct uloop_fd
*fd
)
244 for (i
= 0; i
< cur_nfds
; i
++) {
245 if (cur_fds
[cur_fd
+ i
].fd
!= fd
)
248 cur_fds
[cur_fd
+ i
].fd
= NULL
;
255 uloop_fd_set_cb(fd
, 0);
257 fd
->registered
= false;
259 uloop_fd_stack_event(fd
, -1);
260 return __uloop_fd_delete(fd
);
263 static int64_t tv_diff(struct timeval
*t1
, struct timeval
*t2
)
266 (t1
->tv_sec
- t2
->tv_sec
) * 1000 +
267 (t1
->tv_usec
- t2
->tv_usec
) / 1000;
270 int uloop_timeout_add(struct uloop_timeout
*timeout
)
272 struct uloop_timeout
*tmp
;
273 struct list_head
*h
= &timeouts
;
275 if (timeout
->pending
)
278 list_for_each_entry(tmp
, &timeouts
, list
) {
279 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
285 list_add_tail(&timeout
->list
, h
);
286 timeout
->pending
= true;
291 static void uloop_gettime(struct timeval
*tv
)
295 clock_gettime(CLOCK_MONOTONIC
, &ts
);
296 tv
->tv_sec
= ts
.tv_sec
;
297 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
300 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
302 struct timeval
*time
= &timeout
->time
;
304 if (timeout
->pending
)
305 uloop_timeout_cancel(timeout
);
309 time
->tv_sec
+= msecs
/ 1000;
310 time
->tv_usec
+= (msecs
% 1000) * 1000;
312 if (time
->tv_usec
> 1000000) {
314 time
->tv_usec
-= 1000000;
317 return uloop_timeout_add(timeout
);
320 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
322 if (!timeout
->pending
)
325 list_del(&timeout
->list
);
326 timeout
->pending
= false;
331 int uloop_timeout_remaining(struct uloop_timeout
*timeout
)
336 if (!timeout
->pending
)
341 td
= tv_diff(&timeout
->time
, &now
);
345 else if (td
< INT_MIN
)
351 int64_t uloop_timeout_remaining64(struct uloop_timeout
*timeout
)
355 if (!timeout
->pending
)
360 return tv_diff(&timeout
->time
, &now
);
363 int uloop_process_add(struct uloop_process
*p
)
365 struct uloop_process
*tmp
;
366 struct list_head
*h
= &processes
;
371 list_for_each_entry(tmp
, &processes
, list
) {
372 if (tmp
->pid
> p
->pid
) {
378 list_add_tail(&p
->list
, h
);
384 int uloop_process_delete(struct uloop_process
*p
)
395 static void uloop_handle_processes(void)
397 struct uloop_process
*p
, *tmp
;
404 pid
= waitpid(-1, &ret
, WNOHANG
);
405 if (pid
< 0 && errno
== EINTR
)
411 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
418 uloop_process_delete(p
);
425 static void uloop_signal_wake(void)
428 if (write(waker_pipe
, "w", 1) < 0) {
436 static void uloop_handle_sigint(int signo
)
438 uloop_status
= signo
;
439 uloop_cancelled
= true;
443 static void uloop_sigchld(int signo
)
449 static void uloop_install_handler(int signum
, void (*handler
)(int), struct sigaction
* old
, bool add
)
452 struct sigaction
*act
;
455 sigaction(signum
, NULL
, &s
);
458 if (s
.sa_handler
== SIG_DFL
) { /* Do not override existing custom signal handlers */
459 memcpy(old
, &s
, sizeof(struct sigaction
));
460 s
.sa_handler
= handler
;
465 else if (s
.sa_handler
== handler
) { /* Do not restore if someone modified our handler */
470 sigaction(signum
, act
, NULL
);
473 static void uloop_ignore_signal(int signum
, bool ignore
)
476 void *new_handler
= NULL
;
478 sigaction(signum
, NULL
, &s
);
481 if (s
.sa_handler
== SIG_DFL
) /* Ignore only if there isn't any custom handler */
482 new_handler
= SIG_IGN
;
484 if (s
.sa_handler
== SIG_IGN
) /* Restore only if noone modified our SIG_IGN */
485 new_handler
= SIG_DFL
;
489 s
.sa_handler
= new_handler
;
491 sigaction(signum
, &s
, NULL
);
495 static void uloop_setup_signals(bool add
)
497 static struct sigaction old_sigint
, old_sigchld
, old_sigterm
;
499 uloop_install_handler(SIGINT
, uloop_handle_sigint
, &old_sigint
, add
);
500 uloop_install_handler(SIGTERM
, uloop_handle_sigint
, &old_sigterm
, add
);
502 if (uloop_handle_sigchld
)
503 uloop_install_handler(SIGCHLD
, uloop_sigchld
, &old_sigchld
, add
);
505 uloop_ignore_signal(SIGPIPE
, add
);
508 int uloop_get_next_timeout(void)
510 struct uloop_timeout
*timeout
;
514 if (list_empty(&timeouts
))
519 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
520 diff
= tv_diff(&timeout
->time
, &tv
);
529 static void uloop_process_timeouts(void)
531 struct uloop_timeout
*t
;
534 if (list_empty(&timeouts
))
538 while (!list_empty(&timeouts
)) {
539 t
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
541 if (tv_diff(&t
->time
, &tv
) > 0)
544 uloop_timeout_cancel(t
);
550 static void uloop_clear_timeouts(void)
552 struct uloop_timeout
*t
, *tmp
;
554 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
555 uloop_timeout_cancel(t
);
558 static void uloop_clear_processes(void)
560 struct uloop_process
*p
, *tmp
;
562 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
563 uloop_process_delete(p
);
566 bool uloop_cancelling(void)
568 return uloop_run_depth
> 0 && uloop_cancelled
;
571 int uloop_run_timeout(int timeout
)
578 uloop_cancelled
= false;
580 uloop_process_timeouts();
583 uloop_handle_processes();
588 next_time
= uloop_get_next_timeout();
589 if (timeout
>= 0 && (next_time
< 0 || timeout
< next_time
))
591 uloop_run_events(next_time
);
592 } while (!uloop_cancelled
&& timeout
< 0);
599 void uloop_done(void)
601 uloop_setup_signals(false);
608 if (waker_pipe
>= 0) {
609 uloop_fd_delete(&waker_fd
);
615 uloop_clear_timeouts();
616 uloop_clear_processes();