2 * uloop - event loop implementation
4 * Copyright (C) 2010-2013 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 #define ULOOP_MAX_EVENTS 10
48 static struct list_head timeouts
= LIST_HEAD_INIT(timeouts
);
49 static struct list_head processes
= LIST_HEAD_INIT(processes
);
51 static int poll_fd
= -1;
52 bool uloop_cancelled
= false;
53 bool uloop_handle_sigchld
= true;
54 static bool do_sigchld
= false;
56 static struct uloop_fd_event cur_fds
[ULOOP_MAX_EVENTS
];
57 static int cur_fd
, cur_nfds
;
63 struct timespec timeout
= { 0, 0 };
64 struct kevent ev
= {};
73 EV_SET(&ev
, SIGCHLD
, EVFILT_SIGNAL
, EV_ADD
, 0, 0, 0);
74 kevent(poll_fd
, &ev
, 1, NULL
, 0, &timeout
);
80 static uint16_t get_flags(unsigned int flags
, unsigned int mask
)
88 if (flags
& ULOOP_EDGE_TRIGGER
)
94 static struct kevent events
[ULOOP_MAX_EVENTS
];
96 static int register_kevent(struct uloop_fd
*fd
, unsigned int flags
)
98 struct timespec timeout
= { 0, 0 };
102 unsigned int changed
;
105 if (flags
& ULOOP_EDGE_DEFER
)
106 flags
&= ~ULOOP_EDGE_TRIGGER
;
108 changed
= flags
^ fd
->flags
;
109 if (changed
& ULOOP_EDGE_TRIGGER
)
112 if (changed
& ULOOP_READ
) {
113 kflags
= get_flags(flags
, ULOOP_READ
);
114 EV_SET(&ev
[nev
++], fd
->fd
, EVFILT_READ
, kflags
, 0, 0, fd
);
117 if (changed
& ULOOP_WRITE
) {
118 kflags
= get_flags(flags
, ULOOP_WRITE
);
119 EV_SET(&ev
[nev
++], fd
->fd
, EVFILT_WRITE
, kflags
, 0, 0, fd
);
126 if (kevent(poll_fd
, ev
, nev
, NULL
, fl
, &timeout
) == -1)
132 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
134 if (flags
& ULOOP_EDGE_TRIGGER
)
135 flags
|= ULOOP_EDGE_DEFER
;
137 flags
&= ~ULOOP_EDGE_DEFER
;
139 return register_kevent(fd
, flags
);
142 static int __uloop_fd_delete(struct uloop_fd
*fd
)
144 return register_poll(fd
, 0);
147 static int uloop_fetch_events(int timeout
)
153 ts
.tv_sec
= timeout
/ 1000;
154 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
157 nfds
= kevent(poll_fd
, NULL
, 0, events
, ARRAY_SIZE(events
), timeout
>= 0 ? &ts
: NULL
);
158 for (n
= 0; n
< nfds
; n
++) {
159 struct uloop_fd_event
*cur
= &cur_fds
[n
];
160 struct uloop_fd
*u
= events
[n
].udata
;
167 if (events
[n
].flags
& EV_ERROR
) {
172 if(events
[n
].filter
== EVFILT_READ
)
174 else if (events
[n
].filter
== EVFILT_WRITE
)
177 if (events
[n
].flags
& EV_EOF
)
182 if (u
->flags
& ULOOP_EDGE_DEFER
) {
183 u
->flags
&= ~ULOOP_EDGE_DEFER
;
184 u
->flags
|= ULOOP_EDGE_TRIGGER
;
185 register_kevent(u
, u
->flags
);
196 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
199 #define EPOLLRDHUP 0x2000
207 poll_fd
= epoll_create(32);
211 fcntl(poll_fd
, F_SETFD
, fcntl(poll_fd
, F_GETFD
) | FD_CLOEXEC
);
215 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
217 struct epoll_event ev
;
218 int op
= fd
->registered
? EPOLL_CTL_MOD
: EPOLL_CTL_ADD
;
220 memset(&ev
, 0, sizeof(struct epoll_event
));
222 if (flags
& ULOOP_READ
)
223 ev
.events
|= EPOLLIN
| EPOLLRDHUP
;
225 if (flags
& ULOOP_WRITE
)
226 ev
.events
|= EPOLLOUT
;
228 if (flags
& ULOOP_EDGE_TRIGGER
)
229 ev
.events
|= EPOLLET
;
235 return epoll_ctl(poll_fd
, op
, fd
->fd
, &ev
);
238 static struct epoll_event events
[ULOOP_MAX_EVENTS
];
240 static int __uloop_fd_delete(struct uloop_fd
*sock
)
243 return epoll_ctl(poll_fd
, EPOLL_CTL_DEL
, sock
->fd
, 0);
246 static int uloop_fetch_events(int timeout
)
250 nfds
= epoll_wait(poll_fd
, events
, ARRAY_SIZE(events
), timeout
);
251 for (n
= 0; n
< nfds
; ++n
) {
252 struct uloop_fd_event
*cur
= &cur_fds
[n
];
253 struct uloop_fd
*u
= events
[n
].data
.ptr
;
260 if (events
[n
].events
& (EPOLLERR
|EPOLLHUP
)) {
265 if(!(events
[n
].events
& (EPOLLRDHUP
|EPOLLIN
|EPOLLOUT
|EPOLLERR
|EPOLLHUP
))) {
270 if(events
[n
].events
& EPOLLRDHUP
)
273 if(events
[n
].events
& EPOLLIN
)
276 if(events
[n
].events
& EPOLLOUT
)
287 static void uloop_run_events(int timeout
)
289 struct uloop_fd_event
*cur
;
294 cur_nfds
= uloop_fetch_events(timeout
);
299 while (cur_nfds
> 0) {
300 cur
= &cur_fds
[cur_fd
++];
310 fd
->cb(fd
, cur
->events
);
315 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
320 if (!(flags
& (ULOOP_READ
| ULOOP_WRITE
)))
321 return uloop_fd_delete(sock
);
323 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
324 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
326 fcntl(sock
->fd
, F_SETFL
, fl
);
329 ret
= register_poll(sock
, flags
);
333 sock
->registered
= true;
340 int uloop_fd_delete(struct uloop_fd
*fd
)
347 for (i
= 0; i
< cur_nfds
; i
++) {
348 if (cur_fds
[cur_fd
+ i
].fd
!= fd
)
351 cur_fds
[cur_fd
+ i
].fd
= NULL
;
353 fd
->registered
= false;
354 return __uloop_fd_delete(fd
);
357 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
360 (t1
->tv_sec
- t2
->tv_sec
) * 1000 +
361 (t1
->tv_usec
- t2
->tv_usec
) / 1000;
364 int uloop_timeout_add(struct uloop_timeout
*timeout
)
366 struct uloop_timeout
*tmp
;
367 struct list_head
*h
= &timeouts
;
369 if (timeout
->pending
)
372 list_for_each_entry(tmp
, &timeouts
, list
) {
373 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
379 list_add_tail(&timeout
->list
, h
);
380 timeout
->pending
= true;
385 static void uloop_gettime(struct timeval
*tv
)
389 clock_gettime(CLOCK_MONOTONIC
, &ts
);
390 tv
->tv_sec
= ts
.tv_sec
;
391 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
394 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
396 struct timeval
*time
= &timeout
->time
;
398 if (timeout
->pending
)
399 uloop_timeout_cancel(timeout
);
401 uloop_gettime(&timeout
->time
);
403 time
->tv_sec
+= msecs
/ 1000;
404 time
->tv_usec
+= (msecs
% 1000) * 1000;
406 if (time
->tv_usec
> 1000000) {
408 time
->tv_usec
%= 1000000;
411 return uloop_timeout_add(timeout
);
414 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
416 if (!timeout
->pending
)
419 list_del(&timeout
->list
);
420 timeout
->pending
= false;
425 int uloop_timeout_remaining(struct uloop_timeout
*timeout
)
429 if (!timeout
->pending
)
434 return tv_diff(&timeout
->time
, &now
);
437 int uloop_process_add(struct uloop_process
*p
)
439 struct uloop_process
*tmp
;
440 struct list_head
*h
= &processes
;
445 list_for_each_entry(tmp
, &processes
, list
) {
446 if (tmp
->pid
> p
->pid
) {
452 list_add_tail(&p
->list
, h
);
458 int uloop_process_delete(struct uloop_process
*p
)
469 static void uloop_handle_processes(void)
471 struct uloop_process
*p
, *tmp
;
478 pid
= waitpid(-1, &ret
, WNOHANG
);
482 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
489 uloop_process_delete(p
);
496 static void uloop_handle_sigint(int signo
)
498 uloop_cancelled
= true;
501 static void uloop_sigchld(int signo
)
506 static void uloop_setup_signals(void)
510 memset(&s
, 0, sizeof(struct sigaction
));
511 s
.sa_handler
= uloop_handle_sigint
;
513 sigaction(SIGINT
, &s
, NULL
);
515 if (uloop_handle_sigchld
) {
516 s
.sa_handler
= uloop_sigchld
;
517 sigaction(SIGCHLD
, &s
, NULL
);
521 static int uloop_get_next_timeout(struct timeval
*tv
)
523 struct uloop_timeout
*timeout
;
526 if (list_empty(&timeouts
))
529 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
530 diff
= tv_diff(&timeout
->time
, tv
);
537 static void uloop_process_timeouts(struct timeval
*tv
)
539 struct uloop_timeout
*t
;
541 while (!list_empty(&timeouts
)) {
542 t
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
544 if (tv_diff(&t
->time
, tv
) > 0)
547 uloop_timeout_cancel(t
);
553 static void uloop_clear_timeouts(void)
555 struct uloop_timeout
*t
, *tmp
;
557 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
558 uloop_timeout_cancel(t
);
561 static void uloop_clear_processes(void)
563 struct uloop_process
*p
, *tmp
;
565 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
566 uloop_process_delete(p
);
573 uloop_setup_signals();
574 while(!uloop_cancelled
)
577 uloop_process_timeouts(&tv
);
582 uloop_handle_processes();
583 uloop_run_events(uloop_get_next_timeout(&tv
));
587 void uloop_done(void)
595 uloop_clear_timeouts();
596 uloop_clear_processes();