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 int uloop_status
= 0;
62 static bool do_sigchld
= false;
64 static struct uloop_fd_event cur_fds
[ULOOP_MAX_EVENTS
];
65 static int cur_fd
, cur_nfds
;
66 static int uloop_run_depth
= 0;
68 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
);
71 #include "uloop-kqueue.c"
75 #include "uloop-epoll.c"
78 static void waker_consume(struct uloop_fd
*fd
, unsigned int events
)
82 while (read(fd
->fd
, buf
, 4) > 0)
86 static int waker_pipe
= -1;
87 static struct uloop_fd waker_fd
= {
92 static void waker_init_fd(int fd
)
94 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
95 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
98 static int waker_init(void)
108 waker_init_fd(fds
[0]);
109 waker_init_fd(fds
[1]);
112 waker_fd
.fd
= fds
[0];
113 waker_fd
.cb
= waker_consume
;
114 uloop_fd_add(&waker_fd
, ULOOP_READ
);
119 static void uloop_setup_signals(bool add
);
123 if (uloop_init_pollfd() < 0)
126 if (waker_init() < 0) {
131 uloop_setup_signals(true);
136 static bool uloop_fd_stack_event(struct uloop_fd
*fd
, int events
)
138 struct uloop_fd_stack
*cur
;
141 * Do not buffer events for level-triggered fds, they will keep firing.
142 * Caller needs to take care of recursion issues.
144 if (!(fd
->flags
& ULOOP_EDGE_TRIGGER
))
147 for (cur
= fd_stack
; cur
; cur
= cur
->next
) {
154 cur
->events
|= events
| ULOOP_EVENT_BUFFERED
;
162 static void uloop_run_events(int timeout
)
164 struct uloop_fd_event
*cur
;
169 cur_nfds
= uloop_fetch_events(timeout
);
174 while (cur_nfds
> 0) {
175 struct uloop_fd_stack stack_cur
;
178 cur
= &cur_fds
[cur_fd
++];
182 events
= cur
->events
;
189 if (uloop_fd_stack_event(fd
, cur
->events
))
192 stack_cur
.next
= fd_stack
;
194 fd_stack
= &stack_cur
;
196 stack_cur
.events
= 0;
198 events
= stack_cur
.events
& ULOOP_EVENT_MASK
;
199 } while (stack_cur
.fd
&& events
);
200 fd_stack
= stack_cur
.next
;
206 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
211 if (!(flags
& (ULOOP_READ
| ULOOP_WRITE
)))
212 return uloop_fd_delete(sock
);
214 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
215 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
217 fcntl(sock
->fd
, F_SETFL
, fl
);
220 ret
= register_poll(sock
, flags
);
224 sock
->registered
= true;
232 int uloop_fd_delete(struct uloop_fd
*fd
)
236 for (i
= 0; i
< cur_nfds
; i
++) {
237 if (cur_fds
[cur_fd
+ i
].fd
!= fd
)
240 cur_fds
[cur_fd
+ i
].fd
= NULL
;
246 fd
->registered
= false;
247 uloop_fd_stack_event(fd
, -1);
248 return __uloop_fd_delete(fd
);
251 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
254 (t1
->tv_sec
- t2
->tv_sec
) * 1000 +
255 (t1
->tv_usec
- t2
->tv_usec
) / 1000;
258 int uloop_timeout_add(struct uloop_timeout
*timeout
)
260 struct uloop_timeout
*tmp
;
261 struct list_head
*h
= &timeouts
;
263 if (timeout
->pending
)
266 list_for_each_entry(tmp
, &timeouts
, list
) {
267 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
273 list_add_tail(&timeout
->list
, h
);
274 timeout
->pending
= true;
279 static void uloop_gettime(struct timeval
*tv
)
283 clock_gettime(CLOCK_MONOTONIC
, &ts
);
284 tv
->tv_sec
= ts
.tv_sec
;
285 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
288 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
290 struct timeval
*time
= &timeout
->time
;
292 if (timeout
->pending
)
293 uloop_timeout_cancel(timeout
);
297 time
->tv_sec
+= msecs
/ 1000;
298 time
->tv_usec
+= (msecs
% 1000) * 1000;
300 if (time
->tv_usec
> 1000000) {
302 time
->tv_usec
-= 1000000;
305 return uloop_timeout_add(timeout
);
308 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
310 if (!timeout
->pending
)
313 list_del(&timeout
->list
);
314 timeout
->pending
= false;
319 int uloop_timeout_remaining(struct uloop_timeout
*timeout
)
323 if (!timeout
->pending
)
328 return tv_diff(&timeout
->time
, &now
);
331 int uloop_process_add(struct uloop_process
*p
)
333 struct uloop_process
*tmp
;
334 struct list_head
*h
= &processes
;
339 list_for_each_entry(tmp
, &processes
, list
) {
340 if (tmp
->pid
> p
->pid
) {
346 list_add_tail(&p
->list
, h
);
352 int uloop_process_delete(struct uloop_process
*p
)
363 static void uloop_handle_processes(void)
365 struct uloop_process
*p
, *tmp
;
372 pid
= waitpid(-1, &ret
, WNOHANG
);
373 if (pid
< 0 && errno
== EINTR
)
379 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
386 uloop_process_delete(p
);
393 static void uloop_signal_wake(void)
396 if (write(waker_pipe
, "w", 1) < 0) {
404 static void uloop_handle_sigint(int signo
)
406 uloop_status
= signo
;
407 uloop_cancelled
= true;
411 static void uloop_sigchld(int signo
)
417 static void uloop_install_handler(int signum
, void (*handler
)(int), struct sigaction
* old
, bool add
)
420 struct sigaction
*act
;
423 sigaction(signum
, NULL
, &s
);
426 if (s
.sa_handler
== SIG_DFL
) { /* Do not override existing custom signal handlers */
427 memcpy(old
, &s
, sizeof(struct sigaction
));
428 s
.sa_handler
= handler
;
433 else if (s
.sa_handler
== handler
) { /* Do not restore if someone modified our handler */
438 sigaction(signum
, act
, NULL
);
441 static void uloop_ignore_signal(int signum
, bool ignore
)
444 void *new_handler
= NULL
;
446 sigaction(signum
, NULL
, &s
);
449 if (s
.sa_handler
== SIG_DFL
) /* Ignore only if there isn't any custom handler */
450 new_handler
= SIG_IGN
;
452 if (s
.sa_handler
== SIG_IGN
) /* Restore only if noone modified our SIG_IGN */
453 new_handler
= SIG_DFL
;
457 s
.sa_handler
= new_handler
;
459 sigaction(signum
, &s
, NULL
);
463 static void uloop_setup_signals(bool add
)
465 static struct sigaction old_sigint
, old_sigchld
, old_sigterm
;
467 uloop_install_handler(SIGINT
, uloop_handle_sigint
, &old_sigint
, add
);
468 uloop_install_handler(SIGTERM
, uloop_handle_sigint
, &old_sigterm
, add
);
469 uloop_install_handler(SIGCHLD
, uloop_sigchld
, &old_sigchld
, add
);
471 uloop_ignore_signal(SIGPIPE
, add
);
474 static int uloop_get_next_timeout(struct timeval
*tv
)
476 struct uloop_timeout
*timeout
;
479 if (list_empty(&timeouts
))
482 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
483 diff
= tv_diff(&timeout
->time
, tv
);
490 static void uloop_process_timeouts(struct timeval
*tv
)
492 struct uloop_timeout
*t
;
494 while (!list_empty(&timeouts
)) {
495 t
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
497 if (tv_diff(&t
->time
, tv
) > 0)
500 uloop_timeout_cancel(t
);
506 static void uloop_clear_timeouts(void)
508 struct uloop_timeout
*t
, *tmp
;
510 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
511 uloop_timeout_cancel(t
);
514 static void uloop_clear_processes(void)
516 struct uloop_process
*p
, *tmp
;
518 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
519 uloop_process_delete(p
);
522 bool uloop_cancelling(void)
524 return uloop_run_depth
> 0 && uloop_cancelled
;
527 int uloop_run_timeout(int timeout
)
535 uloop_cancelled
= false;
536 while (!uloop_cancelled
)
539 uloop_process_timeouts(&tv
);
542 uloop_handle_processes();
549 next_time
= uloop_get_next_timeout(&tv
);
550 if (timeout
>= 0 && timeout
< next_time
)
552 uloop_run_events(next_time
);
560 void uloop_done(void)
562 uloop_setup_signals(false);
569 if (waker_pipe
>= 0) {
570 uloop_fd_delete(&waker_fd
);
576 uloop_clear_timeouts();
577 uloop_clear_processes();