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 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
);
73 #include "uloop-kqueue.c"
77 #include "uloop-epoll.c"
80 static void waker_consume(struct uloop_fd
*fd
, unsigned int events
)
84 while (read(fd
->fd
, buf
, 4) > 0)
88 static int waker_pipe
= -1;
89 static struct uloop_fd waker_fd
= {
94 static void waker_init_fd(int fd
)
96 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
97 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
100 static int waker_init(void)
110 waker_init_fd(fds
[0]);
111 waker_init_fd(fds
[1]);
114 waker_fd
.fd
= fds
[0];
115 waker_fd
.cb
= waker_consume
;
116 uloop_fd_add(&waker_fd
, ULOOP_READ
);
121 static void uloop_setup_signals(bool add
);
125 if (uloop_init_pollfd() < 0)
128 if (waker_init() < 0) {
133 uloop_setup_signals(true);
138 static bool uloop_fd_stack_event(struct uloop_fd
*fd
, int events
)
140 struct uloop_fd_stack
*cur
;
143 * Do not buffer events for level-triggered fds, they will keep firing.
144 * Caller needs to take care of recursion issues.
146 if (!(fd
->flags
& ULOOP_EDGE_TRIGGER
))
149 for (cur
= fd_stack
; cur
; cur
= cur
->next
) {
156 cur
->events
|= events
| ULOOP_EVENT_BUFFERED
;
164 static void uloop_run_events(int timeout
)
166 struct uloop_fd_event
*cur
;
171 cur_nfds
= uloop_fetch_events(timeout
);
176 while (cur_nfds
> 0) {
177 struct uloop_fd_stack stack_cur
;
180 cur
= &cur_fds
[cur_fd
++];
184 events
= cur
->events
;
191 if (uloop_fd_stack_event(fd
, cur
->events
))
194 stack_cur
.next
= fd_stack
;
196 fd_stack
= &stack_cur
;
198 stack_cur
.events
= 0;
200 events
= stack_cur
.events
& ULOOP_EVENT_MASK
;
201 } while (stack_cur
.fd
&& events
);
202 fd_stack
= stack_cur
.next
;
208 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
213 if (!(flags
& (ULOOP_READ
| ULOOP_WRITE
)))
214 return uloop_fd_delete(sock
);
216 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
217 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
219 fcntl(sock
->fd
, F_SETFL
, fl
);
222 ret
= register_poll(sock
, flags
);
226 sock
->registered
= true;
234 int uloop_fd_delete(struct uloop_fd
*fd
)
238 for (i
= 0; i
< cur_nfds
; i
++) {
239 if (cur_fds
[cur_fd
+ i
].fd
!= fd
)
242 cur_fds
[cur_fd
+ i
].fd
= NULL
;
248 fd
->registered
= false;
249 uloop_fd_stack_event(fd
, -1);
250 return __uloop_fd_delete(fd
);
253 static int64_t tv_diff(struct timeval
*t1
, struct timeval
*t2
)
256 (t1
->tv_sec
- t2
->tv_sec
) * 1000 +
257 (t1
->tv_usec
- t2
->tv_usec
) / 1000;
260 int uloop_timeout_add(struct uloop_timeout
*timeout
)
262 struct uloop_timeout
*tmp
;
263 struct list_head
*h
= &timeouts
;
265 if (timeout
->pending
)
268 list_for_each_entry(tmp
, &timeouts
, list
) {
269 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
275 list_add_tail(&timeout
->list
, h
);
276 timeout
->pending
= true;
281 static void uloop_gettime(struct timeval
*tv
)
285 clock_gettime(CLOCK_MONOTONIC
, &ts
);
286 tv
->tv_sec
= ts
.tv_sec
;
287 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
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
);
299 time
->tv_sec
+= msecs
/ 1000;
300 time
->tv_usec
+= (msecs
% 1000) * 1000;
302 if (time
->tv_usec
> 1000000) {
304 time
->tv_usec
-= 1000000;
307 return uloop_timeout_add(timeout
);
310 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
312 if (!timeout
->pending
)
315 list_del(&timeout
->list
);
316 timeout
->pending
= false;
321 int uloop_timeout_remaining(struct uloop_timeout
*timeout
)
326 if (!timeout
->pending
)
331 td
= tv_diff(&timeout
->time
, &now
);
335 else if (td
< INT_MIN
)
341 int64_t uloop_timeout_remaining64(struct uloop_timeout
*timeout
)
345 if (!timeout
->pending
)
350 return tv_diff(&timeout
->time
, &now
);
353 int uloop_process_add(struct uloop_process
*p
)
355 struct uloop_process
*tmp
;
356 struct list_head
*h
= &processes
;
361 list_for_each_entry(tmp
, &processes
, list
) {
362 if (tmp
->pid
> p
->pid
) {
368 list_add_tail(&p
->list
, h
);
374 int uloop_process_delete(struct uloop_process
*p
)
385 static void uloop_handle_processes(void)
387 struct uloop_process
*p
, *tmp
;
394 pid
= waitpid(-1, &ret
, WNOHANG
);
395 if (pid
< 0 && errno
== EINTR
)
401 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
408 uloop_process_delete(p
);
415 static void uloop_signal_wake(void)
418 if (write(waker_pipe
, "w", 1) < 0) {
426 static void uloop_handle_sigint(int signo
)
428 uloop_status
= signo
;
429 uloop_cancelled
= true;
433 static void uloop_sigchld(int signo
)
439 static void uloop_install_handler(int signum
, void (*handler
)(int), struct sigaction
* old
, bool add
)
442 struct sigaction
*act
;
445 sigaction(signum
, NULL
, &s
);
448 if (s
.sa_handler
== SIG_DFL
) { /* Do not override existing custom signal handlers */
449 memcpy(old
, &s
, sizeof(struct sigaction
));
450 s
.sa_handler
= handler
;
455 else if (s
.sa_handler
== handler
) { /* Do not restore if someone modified our handler */
460 sigaction(signum
, act
, NULL
);
463 static void uloop_ignore_signal(int signum
, bool ignore
)
466 void *new_handler
= NULL
;
468 sigaction(signum
, NULL
, &s
);
471 if (s
.sa_handler
== SIG_DFL
) /* Ignore only if there isn't any custom handler */
472 new_handler
= SIG_IGN
;
474 if (s
.sa_handler
== SIG_IGN
) /* Restore only if noone modified our SIG_IGN */
475 new_handler
= SIG_DFL
;
479 s
.sa_handler
= new_handler
;
481 sigaction(signum
, &s
, NULL
);
485 static void uloop_setup_signals(bool add
)
487 static struct sigaction old_sigint
, old_sigchld
, old_sigterm
;
489 uloop_install_handler(SIGINT
, uloop_handle_sigint
, &old_sigint
, add
);
490 uloop_install_handler(SIGTERM
, uloop_handle_sigint
, &old_sigterm
, add
);
492 if (uloop_handle_sigchld
)
493 uloop_install_handler(SIGCHLD
, uloop_sigchld
, &old_sigchld
, add
);
495 uloop_ignore_signal(SIGPIPE
, add
);
498 static int uloop_get_next_timeout(struct timeval
*tv
)
500 struct uloop_timeout
*timeout
;
503 if (list_empty(&timeouts
))
506 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
507 diff
= tv_diff(&timeout
->time
, tv
);
514 static void uloop_process_timeouts(struct timeval
*tv
)
516 struct uloop_timeout
*t
;
518 while (!list_empty(&timeouts
)) {
519 t
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
521 if (tv_diff(&t
->time
, tv
) > 0)
524 uloop_timeout_cancel(t
);
530 static void uloop_clear_timeouts(void)
532 struct uloop_timeout
*t
, *tmp
;
534 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
535 uloop_timeout_cancel(t
);
538 static void uloop_clear_processes(void)
540 struct uloop_process
*p
, *tmp
;
542 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
543 uloop_process_delete(p
);
546 bool uloop_cancelling(void)
548 return uloop_run_depth
> 0 && uloop_cancelled
;
551 int uloop_run_timeout(int timeout
)
559 uloop_cancelled
= false;
560 while (!uloop_cancelled
)
563 uloop_process_timeouts(&tv
);
566 uloop_handle_processes();
573 next_time
= uloop_get_next_timeout(&tv
);
574 if (timeout
>= 0 && timeout
< next_time
)
576 uloop_run_events(next_time
);
584 void uloop_done(void)
586 uloop_setup_signals(false);
593 if (waker_pipe
>= 0) {
594 uloop_fd_delete(&waker_fd
);
600 uloop_clear_timeouts();
601 uloop_clear_processes();