2 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
4 * Copyright (C) 2010 Steven Barth <steven@midlink.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
28 #include <sys/event.h>
31 #include <sys/epoll.h>
45 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
47 #define ULOOP_MAX_EVENTS 10
49 struct uloop_timeout
*first_timeout
;
65 static uint16_t get_flags(unsigned int flags
, unsigned int mask
)
73 if (flags
& ULOOP_EDGE_TRIGGER
)
79 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
81 struct timespec timeout
= { 0, 0 };
86 changed
= fd
->kqflags
^ flags
;
87 if (changed
& ULOOP_EDGE_TRIGGER
)
90 if (changed
& ULOOP_READ
) {
91 uint16_t kflags
= get_flags(flags
, ULOOP_READ
);
92 EV_SET(&ev
[nev
++], fd
->fd
, EVFILT_READ
, kflags
, 0, 0, fd
);
95 if (changed
& ULOOP_WRITE
) {
96 uint16_t kflags
= get_flags(flags
, ULOOP_WRITE
);
97 EV_SET(&ev
[nev
++], fd
->fd
, EVFILT_READ
, kflags
, 0, 0, fd
);
100 if (nev
&& (kevent(poll_fd
, ev
, nev
, NULL
, 0, &timeout
) == -1))
107 int uloop_fd_delete(struct uloop_fd
*sock
)
109 sock
->registered
= false;
110 return register_poll(sock
, 0);
113 static void uloop_run_events(int timeout
)
115 struct kevent events
[ULOOP_MAX_EVENTS
];
123 ts
.tv_sec
= timeout
/ 1000;
124 ts
.tv_nsec
= timeout
* 1000000;
127 nfds
= kevent(poll_fd
, NULL
, 0, events
, ARRAY_SIZE(events
), &ts
);
128 for(n
= 0; n
< nfds
; ++n
)
130 struct uloop_fd
*u
= events
[n
].udata
;
133 if(events
[n
].flags
& EV_ERROR
) {
138 if(events
[n
].filter
== EVFILT_READ
)
140 else if (events
[n
].filter
== EVFILT_WRITE
)
143 if(events
[n
].flags
& EV_EOF
)
158 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
161 #define EPOLLRDHUP 0x2000
166 poll_fd
= epoll_create(32);
170 fcntl(poll_fd
, F_SETFD
, fcntl(poll_fd
, F_GETFD
) | FD_CLOEXEC
);
174 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
176 struct epoll_event ev
;
177 int op
= fd
->registered
? EPOLL_CTL_MOD
: EPOLL_CTL_ADD
;
179 memset(&ev
, 0, sizeof(struct epoll_event
));
181 if (flags
& ULOOP_READ
)
182 ev
.events
|= EPOLLIN
| EPOLLRDHUP
;
184 if (flags
& ULOOP_WRITE
)
185 ev
.events
|= EPOLLOUT
;
187 if (flags
& ULOOP_EDGE_TRIGGER
)
188 ev
.events
|= EPOLLET
;
193 return epoll_ctl(poll_fd
, op
, fd
->fd
, &ev
);
196 int uloop_fd_delete(struct uloop_fd
*sock
)
198 sock
->registered
= false;
199 return epoll_ctl(poll_fd
, EPOLL_CTL_DEL
, sock
->fd
, 0);
202 static void uloop_run_events(int timeout
)
204 struct epoll_event events
[ULOOP_MAX_EVENTS
];
207 nfds
= epoll_wait(poll_fd
, events
, ARRAY_SIZE(events
), timeout
);
208 for(n
= 0; n
< nfds
; ++n
)
210 struct uloop_fd
*u
= events
[n
].data
.ptr
;
213 if(events
[n
].events
& EPOLLERR
) {
218 if(!(events
[n
].events
& (EPOLLRDHUP
|EPOLLIN
|EPOLLOUT
|EPOLLERR
)))
221 if(events
[n
].events
& EPOLLRDHUP
)
224 if(events
[n
].events
& EPOLLIN
)
227 if(events
[n
].events
& EPOLLOUT
)
237 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
242 if (!sock
->registered
) {
243 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
245 fcntl(sock
->fd
, F_SETFL
, fl
);
248 ret
= register_poll(sock
, flags
);
252 sock
->registered
= true;
259 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
261 if (t1
->tv_sec
!= t2
->tv_sec
)
262 return (t1
->tv_sec
- t2
->tv_sec
) * 1000;
264 return (t1
->tv_usec
- t2
->tv_usec
) / 1000;
267 int uloop_timeout_add(struct uloop_timeout
*timeout
)
269 struct uloop_timeout
**head
= &first_timeout
;
270 struct uloop_timeout
*prev
= NULL
;
272 if (timeout
->pending
)
276 if (tv_diff(&(*head
)->time
, &timeout
->time
) > 0)
280 head
= &(*head
)->next
;
283 timeout
->prev
= prev
;
284 timeout
->next
= *head
;
286 timeout
->next
->prev
= timeout
;
288 timeout
->pending
= true;
293 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
295 struct timeval
*time
= &timeout
->time
;
297 if (timeout
->pending
)
298 uloop_timeout_cancel(timeout
);
300 gettimeofday(&timeout
->time
, NULL
);
302 time
->tv_sec
+= msecs
/ 1000;
303 time
->tv_usec
+= msecs
% 1000;
305 if (time
->tv_usec
> 1000000) {
307 time
->tv_usec
%= 100000;
310 return uloop_timeout_add(timeout
);
313 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
315 if (!timeout
->pending
)
319 timeout
->prev
->next
= timeout
->next
;
321 first_timeout
= timeout
->next
;
324 timeout
->next
->prev
= timeout
->prev
;
326 timeout
->pending
= false;
331 static void uloop_handle_sigint(int signo
)
336 static void uloop_setup_signals(void)
339 memset(&s
, 0, sizeof(struct sigaction
));
340 s
.sa_handler
= uloop_handle_sigint
;
342 sigaction(SIGINT
, &s
, NULL
);
345 static int uloop_get_next_timeout(struct timeval
*tv
)
352 diff
= tv_diff(&first_timeout
->time
, tv
);
359 static void uloop_process_timeouts(struct timeval
*tv
)
361 struct uloop_timeout
*timeout
;
363 while (first_timeout
) {
364 if (tv_diff(&first_timeout
->time
, tv
) > 0)
367 timeout
= first_timeout
;
368 uloop_timeout_cancel(timeout
);
370 timeout
->cb(timeout
);
383 uloop_setup_signals();
386 gettimeofday(&tv
, NULL
);
387 uloop_process_timeouts(&tv
);
388 uloop_run_events(uloop_get_next_timeout(&tv
));
392 void uloop_done(void)