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 static struct uloop_timeout
*first_timeout
;
50 static int poll_fd
= -1;
51 bool uloop_cancelled
= false;
68 static uint16_t get_flags(unsigned int flags
, unsigned int mask
)
76 if (flags
& ULOOP_EDGE_TRIGGER
)
82 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
84 struct timespec timeout
= { 0, 0 };
89 changed
= fd
->kqflags
^ flags
;
90 if (changed
& ULOOP_EDGE_TRIGGER
)
93 if (changed
& ULOOP_READ
) {
94 uint16_t kflags
= get_flags(flags
, ULOOP_READ
);
95 EV_SET(&ev
[nev
++], fd
->fd
, EVFILT_READ
, kflags
, 0, 0, fd
);
98 if (changed
& ULOOP_WRITE
) {
99 uint16_t kflags
= get_flags(flags
, ULOOP_WRITE
);
100 EV_SET(&ev
[nev
++], fd
->fd
, EVFILT_WRITE
, kflags
, 0, 0, fd
);
103 if (nev
&& (kevent(poll_fd
, ev
, nev
, NULL
, 0, &timeout
) == -1))
110 int uloop_fd_delete(struct uloop_fd
*sock
)
112 sock
->registered
= false;
113 return register_poll(sock
, 0);
116 static void uloop_run_events(int timeout
)
118 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
), timeout
> 0 ? &ts
: NULL
);
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
169 poll_fd
= epoll_create(32);
173 fcntl(poll_fd
, F_SETFD
, fcntl(poll_fd
, F_GETFD
) | FD_CLOEXEC
);
177 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
179 struct epoll_event ev
;
180 int op
= fd
->registered
? EPOLL_CTL_MOD
: EPOLL_CTL_ADD
;
182 memset(&ev
, 0, sizeof(struct epoll_event
));
184 if (flags
& ULOOP_READ
)
185 ev
.events
|= EPOLLIN
| EPOLLRDHUP
;
187 if (flags
& ULOOP_WRITE
)
188 ev
.events
|= EPOLLOUT
;
190 if (flags
& ULOOP_EDGE_TRIGGER
)
191 ev
.events
|= EPOLLET
;
196 return epoll_ctl(poll_fd
, op
, fd
->fd
, &ev
);
199 int uloop_fd_delete(struct uloop_fd
*sock
)
201 sock
->registered
= false;
202 return epoll_ctl(poll_fd
, EPOLL_CTL_DEL
, sock
->fd
, 0);
205 static void uloop_run_events(int timeout
)
207 struct epoll_event events
[ULOOP_MAX_EVENTS
];
210 nfds
= epoll_wait(poll_fd
, events
, ARRAY_SIZE(events
), timeout
);
211 for(n
= 0; n
< nfds
; ++n
)
213 struct uloop_fd
*u
= events
[n
].data
.ptr
;
216 if(events
[n
].events
& EPOLLERR
) {
221 if(!(events
[n
].events
& (EPOLLRDHUP
|EPOLLIN
|EPOLLOUT
|EPOLLERR
)))
224 if(events
[n
].events
& EPOLLRDHUP
)
227 if(events
[n
].events
& EPOLLIN
)
230 if(events
[n
].events
& EPOLLOUT
)
240 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
245 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
246 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
248 fcntl(sock
->fd
, F_SETFL
, fl
);
251 ret
= register_poll(sock
, flags
);
255 sock
->registered
= true;
262 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
264 if (t1
->tv_sec
!= t2
->tv_sec
)
265 return (t1
->tv_sec
- t2
->tv_sec
) * 1000;
267 return (t1
->tv_usec
- t2
->tv_usec
) / 1000;
270 int uloop_timeout_add(struct uloop_timeout
*timeout
)
272 struct uloop_timeout
**head
= &first_timeout
;
273 struct uloop_timeout
*prev
= NULL
;
275 if (timeout
->pending
)
279 if (tv_diff(&(*head
)->time
, &timeout
->time
) > 0)
283 head
= &(*head
)->next
;
286 timeout
->prev
= prev
;
287 timeout
->next
= *head
;
289 timeout
->next
->prev
= timeout
;
291 timeout
->pending
= true;
296 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
298 struct timeval
*time
= &timeout
->time
;
300 if (timeout
->pending
)
301 uloop_timeout_cancel(timeout
);
303 gettimeofday(&timeout
->time
, NULL
);
305 time
->tv_sec
+= msecs
/ 1000;
306 time
->tv_usec
+= msecs
% 1000;
308 if (time
->tv_usec
> 1000000) {
310 time
->tv_usec
%= 100000;
313 return uloop_timeout_add(timeout
);
316 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
318 if (!timeout
->pending
)
322 timeout
->prev
->next
= timeout
->next
;
324 first_timeout
= timeout
->next
;
327 timeout
->next
->prev
= timeout
->prev
;
329 timeout
->pending
= false;
334 static void uloop_handle_sigint(int signo
)
336 uloop_cancelled
= true;
339 static void uloop_setup_signals(void)
342 memset(&s
, 0, sizeof(struct sigaction
));
343 s
.sa_handler
= uloop_handle_sigint
;
345 sigaction(SIGINT
, &s
, NULL
);
348 static int uloop_get_next_timeout(struct timeval
*tv
)
355 diff
= tv_diff(&first_timeout
->time
, tv
);
362 static void uloop_process_timeouts(struct timeval
*tv
)
364 struct uloop_timeout
*timeout
;
366 while (first_timeout
) {
367 if (tv_diff(&first_timeout
->time
, tv
) > 0)
370 timeout
= first_timeout
;
371 uloop_timeout_cancel(timeout
);
373 timeout
->cb(timeout
);
381 uloop_setup_signals();
382 while(!uloop_cancelled
)
384 gettimeofday(&tv
, NULL
);
385 uloop_process_timeouts(&tv
);
388 uloop_run_events(uloop_get_next_timeout(&tv
));
392 void uloop_done(void)