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 #define ULOOP_MAX_EVENTS 10
43 static struct list_head timeouts
= LIST_HEAD_INIT(timeouts
);
44 static struct list_head processes
= LIST_HEAD_INIT(processes
);
46 static int poll_fd
= -1;
47 bool uloop_cancelled
= false;
48 bool uloop_handle_sigchld
= true;
49 static bool do_sigchld
= false;
50 static int cur_fd
, cur_nfds
;
56 struct timespec timeout
= { 0, 0 };
57 struct kevent ev
= {};
66 EV_SET(&ev
, SIGCHLD
, EVFILT_SIGNAL
, EV_ADD
, 0, 0, 0);
67 kevent(poll_fd
, &ev
, 1, NULL
, 0, &timeout
);
73 static uint16_t get_flags(unsigned int flags
, unsigned int mask
)
81 if (flags
& ULOOP_EDGE_TRIGGER
)
87 static struct kevent events
[ULOOP_MAX_EVENTS
];
89 static int register_kevent(struct uloop_fd
*fd
, unsigned int flags
)
91 struct timespec timeout
= { 0, 0 };
98 if (flags
& ULOOP_EDGE_DEFER
)
99 flags
&= ~ULOOP_EDGE_TRIGGER
;
101 changed
= flags
^ fd
->flags
;
102 if (changed
& ULOOP_EDGE_TRIGGER
)
105 if (changed
& ULOOP_READ
) {
106 kflags
= get_flags(flags
, ULOOP_READ
);
107 EV_SET(&ev
[nev
++], fd
->fd
, EVFILT_READ
, kflags
, 0, 0, fd
);
110 if (changed
& ULOOP_WRITE
) {
111 kflags
= get_flags(flags
, ULOOP_WRITE
);
112 EV_SET(&ev
[nev
++], fd
->fd
, EVFILT_WRITE
, kflags
, 0, 0, fd
);
119 if (kevent(poll_fd
, ev
, nev
, NULL
, fl
, &timeout
) == -1)
125 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
127 if (flags
& ULOOP_EDGE_TRIGGER
)
128 flags
|= ULOOP_EDGE_DEFER
;
130 flags
&= ~ULOOP_EDGE_DEFER
;
132 return register_kevent(fd
, flags
);
135 int uloop_fd_delete(struct uloop_fd
*sock
)
139 for (i
= cur_fd
+ 1; i
< cur_nfds
; i
++) {
140 if (events
[i
].udata
!= sock
)
143 events
[i
].udata
= NULL
;
146 sock
->registered
= false;
147 return register_poll(sock
, 0);
150 static void uloop_run_events(int timeout
)
156 ts
.tv_sec
= timeout
/ 1000;
157 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
160 nfds
= kevent(poll_fd
, NULL
, 0, events
, ARRAY_SIZE(events
), timeout
>= 0 ? &ts
: NULL
);
161 for(n
= 0; n
< nfds
; ++n
)
163 struct uloop_fd
*u
= events
[n
].udata
;
169 if (events
[n
].flags
& EV_ERROR
) {
174 if(events
[n
].filter
== EVFILT_READ
)
176 else if (events
[n
].filter
== EVFILT_WRITE
)
179 if (events
[n
].flags
& EV_EOF
)
188 if (u
->flags
& ULOOP_EDGE_DEFER
) {
189 u
->flags
&= ~ULOOP_EDGE_DEFER
;
190 register_kevent(u
, u
->flags
);
202 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
205 #define EPOLLRDHUP 0x2000
213 poll_fd
= epoll_create(32);
217 fcntl(poll_fd
, F_SETFD
, fcntl(poll_fd
, F_GETFD
) | FD_CLOEXEC
);
221 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
223 struct epoll_event ev
;
224 int op
= fd
->registered
? EPOLL_CTL_MOD
: EPOLL_CTL_ADD
;
226 memset(&ev
, 0, sizeof(struct epoll_event
));
228 if (flags
& ULOOP_READ
)
229 ev
.events
|= EPOLLIN
| EPOLLRDHUP
;
231 if (flags
& ULOOP_WRITE
)
232 ev
.events
|= EPOLLOUT
;
234 if (flags
& ULOOP_EDGE_TRIGGER
)
235 ev
.events
|= EPOLLET
;
240 return epoll_ctl(poll_fd
, op
, fd
->fd
, &ev
);
243 static struct epoll_event events
[ULOOP_MAX_EVENTS
];
245 int uloop_fd_delete(struct uloop_fd
*sock
)
249 for (i
= cur_fd
+ 1; i
< cur_nfds
; i
++) {
250 if (events
[i
].data
.ptr
!= sock
)
253 events
[i
].data
.ptr
= NULL
;
255 sock
->registered
= false;
256 return epoll_ctl(poll_fd
, EPOLL_CTL_DEL
, sock
->fd
, 0);
259 static void uloop_run_events(int timeout
)
263 nfds
= epoll_wait(poll_fd
, events
, ARRAY_SIZE(events
), timeout
);
264 for(n
= 0; n
< nfds
; ++n
)
266 struct uloop_fd
*u
= events
[n
].data
.ptr
;
272 if(events
[n
].events
& (EPOLLERR
|EPOLLHUP
)) {
277 if(!(events
[n
].events
& (EPOLLRDHUP
|EPOLLIN
|EPOLLOUT
|EPOLLERR
|EPOLLHUP
)))
280 if(events
[n
].events
& EPOLLRDHUP
)
283 if(events
[n
].events
& EPOLLIN
)
286 if(events
[n
].events
& EPOLLOUT
)
300 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
305 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
306 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
308 fcntl(sock
->fd
, F_SETFL
, fl
);
311 ret
= register_poll(sock
, flags
);
315 sock
->registered
= true;
322 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
325 (t1
->tv_sec
- t2
->tv_sec
) * 1000 +
326 (t1
->tv_usec
- t2
->tv_usec
) / 1000;
329 int uloop_timeout_add(struct uloop_timeout
*timeout
)
331 struct uloop_timeout
*tmp
;
332 struct list_head
*h
= &timeouts
;
334 if (timeout
->pending
)
337 list_for_each_entry(tmp
, &timeouts
, list
) {
338 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
344 list_add_tail(&timeout
->list
, h
);
345 timeout
->pending
= true;
350 static void uloop_gettime(struct timeval
*tv
)
354 clock_gettime(CLOCK_MONOTONIC
, &ts
);
355 tv
->tv_sec
= ts
.tv_sec
;
356 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
359 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
361 struct timeval
*time
= &timeout
->time
;
363 if (timeout
->pending
)
364 uloop_timeout_cancel(timeout
);
366 uloop_gettime(&timeout
->time
);
368 time
->tv_sec
+= msecs
/ 1000;
369 time
->tv_usec
+= (msecs
% 1000) * 1000;
371 if (time
->tv_usec
> 1000000) {
373 time
->tv_usec
%= 1000000;
376 return uloop_timeout_add(timeout
);
379 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
381 if (!timeout
->pending
)
384 list_del(&timeout
->list
);
385 timeout
->pending
= false;
390 int uloop_timeout_remaining(struct uloop_timeout
*timeout
)
394 if (!timeout
->pending
)
399 return tv_diff(&timeout
->time
, &now
);
402 int uloop_process_add(struct uloop_process
*p
)
404 struct uloop_process
*tmp
;
405 struct list_head
*h
= &processes
;
410 list_for_each_entry(tmp
, &processes
, list
) {
411 if (tmp
->pid
> p
->pid
) {
417 list_add_tail(&p
->list
, h
);
423 int uloop_process_delete(struct uloop_process
*p
)
434 static void uloop_handle_processes(void)
436 struct uloop_process
*p
, *tmp
;
443 pid
= waitpid(-1, &ret
, WNOHANG
);
447 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
454 uloop_process_delete(p
);
461 static void uloop_handle_sigint(int signo
)
463 uloop_cancelled
= true;
466 static void uloop_sigchld(int signo
)
471 static void uloop_setup_signals(void)
475 memset(&s
, 0, sizeof(struct sigaction
));
476 s
.sa_handler
= uloop_handle_sigint
;
478 sigaction(SIGINT
, &s
, NULL
);
480 if (uloop_handle_sigchld
) {
481 s
.sa_handler
= uloop_sigchld
;
482 sigaction(SIGCHLD
, &s
, NULL
);
486 static int uloop_get_next_timeout(struct timeval
*tv
)
488 struct uloop_timeout
*timeout
;
491 if (list_empty(&timeouts
))
494 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
495 diff
= tv_diff(&timeout
->time
, tv
);
502 static void uloop_process_timeouts(struct timeval
*tv
)
504 struct uloop_timeout
*t
;
506 while (!list_empty(&timeouts
)) {
507 t
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
509 if (tv_diff(&t
->time
, tv
) > 0)
512 uloop_timeout_cancel(t
);
518 static void uloop_clear_timeouts(void)
520 struct uloop_timeout
*t
, *tmp
;
522 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
523 uloop_timeout_cancel(t
);
526 static void uloop_clear_processes(void)
528 struct uloop_process
*p
, *tmp
;
530 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
531 uloop_process_delete(p
);
538 uloop_setup_signals();
539 while(!uloop_cancelled
)
542 uloop_process_timeouts(&tv
);
547 uloop_handle_processes();
548 uloop_run_events(uloop_get_next_timeout(&tv
));
552 void uloop_done(void)
560 uloop_clear_timeouts();
561 uloop_clear_processes();