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
);
121 if (uloop_init_pollfd() < 0)
124 if (waker_init() < 0) {
132 static bool uloop_fd_stack_event(struct uloop_fd
*fd
, int events
)
134 struct uloop_fd_stack
*cur
;
137 * Do not buffer events for level-triggered fds, they will keep firing.
138 * Caller needs to take care of recursion issues.
140 if (!(fd
->flags
& ULOOP_EDGE_TRIGGER
))
143 for (cur
= fd_stack
; cur
; cur
= cur
->next
) {
150 cur
->events
|= events
| ULOOP_EVENT_BUFFERED
;
158 static void uloop_run_events(int timeout
)
160 struct uloop_fd_event
*cur
;
165 cur_nfds
= uloop_fetch_events(timeout
);
170 while (cur_nfds
> 0) {
171 struct uloop_fd_stack stack_cur
;
174 cur
= &cur_fds
[cur_fd
++];
178 events
= cur
->events
;
185 if (uloop_fd_stack_event(fd
, cur
->events
))
188 stack_cur
.next
= fd_stack
;
190 fd_stack
= &stack_cur
;
192 stack_cur
.events
= 0;
194 events
= stack_cur
.events
& ULOOP_EVENT_MASK
;
195 } while (stack_cur
.fd
&& events
);
196 fd_stack
= stack_cur
.next
;
202 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
207 if (!(flags
& (ULOOP_READ
| ULOOP_WRITE
)))
208 return uloop_fd_delete(sock
);
210 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
211 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
213 fcntl(sock
->fd
, F_SETFL
, fl
);
216 ret
= register_poll(sock
, flags
);
220 sock
->registered
= true;
228 int uloop_fd_delete(struct uloop_fd
*fd
)
232 for (i
= 0; i
< cur_nfds
; i
++) {
233 if (cur_fds
[cur_fd
+ i
].fd
!= fd
)
236 cur_fds
[cur_fd
+ i
].fd
= NULL
;
242 fd
->registered
= false;
243 uloop_fd_stack_event(fd
, -1);
244 return __uloop_fd_delete(fd
);
247 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
250 (t1
->tv_sec
- t2
->tv_sec
) * 1000 +
251 (t1
->tv_usec
- t2
->tv_usec
) / 1000;
254 int uloop_timeout_add(struct uloop_timeout
*timeout
)
256 struct uloop_timeout
*tmp
;
257 struct list_head
*h
= &timeouts
;
259 if (timeout
->pending
)
262 list_for_each_entry(tmp
, &timeouts
, list
) {
263 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
269 list_add_tail(&timeout
->list
, h
);
270 timeout
->pending
= true;
275 static void uloop_gettime(struct timeval
*tv
)
279 clock_gettime(CLOCK_MONOTONIC
, &ts
);
280 tv
->tv_sec
= ts
.tv_sec
;
281 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
284 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
286 struct timeval
*time
= &timeout
->time
;
288 if (timeout
->pending
)
289 uloop_timeout_cancel(timeout
);
293 time
->tv_sec
+= msecs
/ 1000;
294 time
->tv_usec
+= (msecs
% 1000) * 1000;
296 if (time
->tv_usec
> 1000000) {
298 time
->tv_usec
-= 1000000;
301 return uloop_timeout_add(timeout
);
304 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
306 if (!timeout
->pending
)
309 list_del(&timeout
->list
);
310 timeout
->pending
= false;
315 int uloop_timeout_remaining(struct uloop_timeout
*timeout
)
319 if (!timeout
->pending
)
324 return tv_diff(&timeout
->time
, &now
);
327 int uloop_process_add(struct uloop_process
*p
)
329 struct uloop_process
*tmp
;
330 struct list_head
*h
= &processes
;
335 list_for_each_entry(tmp
, &processes
, list
) {
336 if (tmp
->pid
> p
->pid
) {
342 list_add_tail(&p
->list
, h
);
348 int uloop_process_delete(struct uloop_process
*p
)
359 static void uloop_handle_processes(void)
361 struct uloop_process
*p
, *tmp
;
368 pid
= waitpid(-1, &ret
, WNOHANG
);
369 if (pid
< 0 && errno
== EINTR
)
375 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
382 uloop_process_delete(p
);
389 static void uloop_signal_wake(void)
392 if (write(waker_pipe
, "w", 1) < 0) {
400 static void uloop_handle_sigint(int signo
)
402 uloop_status
= signo
;
403 uloop_cancelled
= true;
407 static void uloop_sigchld(int signo
)
413 static void uloop_install_handler(int signum
, void (*handler
)(int), struct sigaction
* old
, bool add
)
416 struct sigaction
*act
;
419 sigaction(signum
, NULL
, &s
);
422 if (s
.sa_handler
== SIG_DFL
) { /* Do not override existing custom signal handlers */
423 memcpy(old
, &s
, sizeof(struct sigaction
));
424 s
.sa_handler
= handler
;
429 else if (s
.sa_handler
== handler
) { /* Do not restore if someone modified our handler */
434 sigaction(signum
, act
, NULL
);
437 static void uloop_ignore_signal(int signum
, bool ignore
)
440 void *new_handler
= NULL
;
442 sigaction(signum
, NULL
, &s
);
445 if (s
.sa_handler
== SIG_DFL
) /* Ignore only if there isn't any custom handler */
446 new_handler
= SIG_IGN
;
448 if (s
.sa_handler
== SIG_IGN
) /* Restore only if noone modified our SIG_IGN */
449 new_handler
= SIG_DFL
;
453 s
.sa_handler
= new_handler
;
455 sigaction(signum
, &s
, NULL
);
459 static void uloop_setup_signals(bool add
)
461 static struct sigaction old_sigint
, old_sigchld
, old_sigterm
;
463 uloop_install_handler(SIGINT
, uloop_handle_sigint
, &old_sigint
, add
);
464 uloop_install_handler(SIGTERM
, uloop_handle_sigint
, &old_sigterm
, add
);
465 uloop_install_handler(SIGCHLD
, uloop_sigchld
, &old_sigchld
, add
);
467 uloop_ignore_signal(SIGPIPE
, add
);
470 static int uloop_get_next_timeout(struct timeval
*tv
)
472 struct uloop_timeout
*timeout
;
475 if (list_empty(&timeouts
))
478 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
479 diff
= tv_diff(&timeout
->time
, tv
);
486 static void uloop_process_timeouts(struct timeval
*tv
)
488 struct uloop_timeout
*t
;
490 while (!list_empty(&timeouts
)) {
491 t
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
493 if (tv_diff(&t
->time
, tv
) > 0)
496 uloop_timeout_cancel(t
);
502 static void uloop_clear_timeouts(void)
504 struct uloop_timeout
*t
, *tmp
;
506 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
507 uloop_timeout_cancel(t
);
510 static void uloop_clear_processes(void)
512 struct uloop_process
*p
, *tmp
;
514 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
515 uloop_process_delete(p
);
518 bool uloop_cancelling(void)
520 return uloop_run_depth
> 0 && uloop_cancelled
;
528 * Handlers are only updated for the first call to uloop_run() (and restored
529 * when this call is done).
531 if (!uloop_run_depth
++)
532 uloop_setup_signals(true);
535 uloop_cancelled
= false;
536 while (!uloop_cancelled
)
539 uloop_process_timeouts(&tv
);
542 uloop_handle_processes();
548 uloop_run_events(uloop_get_next_timeout(&tv
));
551 if (!--uloop_run_depth
)
552 uloop_setup_signals(false);
557 void uloop_done(void)
564 if (waker_pipe
>= 0) {
565 uloop_fd_delete(&waker_fd
);
571 uloop_clear_timeouts();
572 uloop_clear_processes();