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 register_kevent(u
, u
->flags
);
195 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
198 #define EPOLLRDHUP 0x2000
206 poll_fd
= epoll_create(32);
210 fcntl(poll_fd
, F_SETFD
, fcntl(poll_fd
, F_GETFD
) | FD_CLOEXEC
);
214 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
216 struct epoll_event ev
;
217 int op
= fd
->registered
? EPOLL_CTL_MOD
: EPOLL_CTL_ADD
;
219 memset(&ev
, 0, sizeof(struct epoll_event
));
221 if (flags
& ULOOP_READ
)
222 ev
.events
|= EPOLLIN
| EPOLLRDHUP
;
224 if (flags
& ULOOP_WRITE
)
225 ev
.events
|= EPOLLOUT
;
227 if (flags
& ULOOP_EDGE_TRIGGER
)
228 ev
.events
|= EPOLLET
;
233 return epoll_ctl(poll_fd
, op
, fd
->fd
, &ev
);
236 static struct epoll_event events
[ULOOP_MAX_EVENTS
];
238 static int __uloop_fd_delete(struct uloop_fd
*sock
)
240 return epoll_ctl(poll_fd
, EPOLL_CTL_DEL
, sock
->fd
, 0);
243 static int uloop_fetch_events(int timeout
)
247 nfds
= epoll_wait(poll_fd
, events
, ARRAY_SIZE(events
), timeout
);
248 for (n
= 0; n
< nfds
; ++n
) {
249 struct uloop_fd_event
*cur
= &cur_fds
[n
];
250 struct uloop_fd
*u
= events
[n
].data
.ptr
;
257 if (events
[n
].events
& (EPOLLERR
|EPOLLHUP
)) {
262 if(!(events
[n
].events
& (EPOLLRDHUP
|EPOLLIN
|EPOLLOUT
|EPOLLERR
|EPOLLHUP
))) {
267 if(events
[n
].events
& EPOLLRDHUP
)
270 if(events
[n
].events
& EPOLLIN
)
273 if(events
[n
].events
& EPOLLOUT
)
284 static void uloop_run_events(int timeout
)
286 struct uloop_fd_event
*cur
;
291 cur_nfds
= uloop_fetch_events(timeout
);
296 while (cur_nfds
> 0) {
297 cur
= &cur_fds
[cur_fd
++];
307 fd
->cb(fd
, cur
->events
);
312 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
317 if (!(flags
& (ULOOP_READ
| ULOOP_WRITE
)))
318 return uloop_fd_delete(sock
);
320 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
321 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
323 fcntl(sock
->fd
, F_SETFL
, fl
);
326 ret
= register_poll(sock
, flags
);
330 sock
->registered
= true;
337 int uloop_fd_delete(struct uloop_fd
*fd
)
344 for (i
= 0; i
< cur_nfds
; i
++) {
345 if (cur_fds
[cur_fd
+ i
].fd
!= fd
)
348 cur_fds
[cur_fd
+ i
].fd
= NULL
;
350 fd
->registered
= false;
351 return __uloop_fd_delete(fd
);
354 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
357 (t1
->tv_sec
- t2
->tv_sec
) * 1000 +
358 (t1
->tv_usec
- t2
->tv_usec
) / 1000;
361 int uloop_timeout_add(struct uloop_timeout
*timeout
)
363 struct uloop_timeout
*tmp
;
364 struct list_head
*h
= &timeouts
;
366 if (timeout
->pending
)
369 list_for_each_entry(tmp
, &timeouts
, list
) {
370 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
376 list_add_tail(&timeout
->list
, h
);
377 timeout
->pending
= true;
382 static void uloop_gettime(struct timeval
*tv
)
386 clock_gettime(CLOCK_MONOTONIC
, &ts
);
387 tv
->tv_sec
= ts
.tv_sec
;
388 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
391 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
393 struct timeval
*time
= &timeout
->time
;
395 if (timeout
->pending
)
396 uloop_timeout_cancel(timeout
);
398 uloop_gettime(&timeout
->time
);
400 time
->tv_sec
+= msecs
/ 1000;
401 time
->tv_usec
+= (msecs
% 1000) * 1000;
403 if (time
->tv_usec
> 1000000) {
405 time
->tv_usec
%= 1000000;
408 return uloop_timeout_add(timeout
);
411 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
413 if (!timeout
->pending
)
416 list_del(&timeout
->list
);
417 timeout
->pending
= false;
422 int uloop_timeout_remaining(struct uloop_timeout
*timeout
)
426 if (!timeout
->pending
)
431 return tv_diff(&timeout
->time
, &now
);
434 int uloop_process_add(struct uloop_process
*p
)
436 struct uloop_process
*tmp
;
437 struct list_head
*h
= &processes
;
442 list_for_each_entry(tmp
, &processes
, list
) {
443 if (tmp
->pid
> p
->pid
) {
449 list_add_tail(&p
->list
, h
);
455 int uloop_process_delete(struct uloop_process
*p
)
466 static void uloop_handle_processes(void)
468 struct uloop_process
*p
, *tmp
;
475 pid
= waitpid(-1, &ret
, WNOHANG
);
479 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
486 uloop_process_delete(p
);
493 static void uloop_handle_sigint(int signo
)
495 uloop_cancelled
= true;
498 static void uloop_sigchld(int signo
)
503 static void uloop_setup_signals(void)
507 memset(&s
, 0, sizeof(struct sigaction
));
508 s
.sa_handler
= uloop_handle_sigint
;
510 sigaction(SIGINT
, &s
, NULL
);
512 if (uloop_handle_sigchld
) {
513 s
.sa_handler
= uloop_sigchld
;
514 sigaction(SIGCHLD
, &s
, NULL
);
518 static int uloop_get_next_timeout(struct timeval
*tv
)
520 struct uloop_timeout
*timeout
;
523 if (list_empty(&timeouts
))
526 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
527 diff
= tv_diff(&timeout
->time
, tv
);
534 static void uloop_process_timeouts(struct timeval
*tv
)
536 struct uloop_timeout
*t
;
538 while (!list_empty(&timeouts
)) {
539 t
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
541 if (tv_diff(&t
->time
, tv
) > 0)
544 uloop_timeout_cancel(t
);
550 static void uloop_clear_timeouts(void)
552 struct uloop_timeout
*t
, *tmp
;
554 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
555 uloop_timeout_cancel(t
);
558 static void uloop_clear_processes(void)
560 struct uloop_process
*p
, *tmp
;
562 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
563 uloop_process_delete(p
);
570 uloop_setup_signals();
571 while(!uloop_cancelled
)
574 uloop_process_timeouts(&tv
);
579 uloop_handle_processes();
580 uloop_run_events(uloop_get_next_timeout(&tv
));
584 void uloop_done(void)
592 uloop_clear_timeouts();
593 uloop_clear_processes();