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>
24 #include <sys/epoll.h>
39 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
42 #define EPOLLRDHUP 0x2000
46 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
49 struct uloop_timeout
*first_timeout
;
53 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
55 struct epoll_event ev
;
56 int op
= sock
->registered
? EPOLL_CTL_MOD
: EPOLL_CTL_ADD
;
60 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
62 fcntl(sock
->fd
, F_SETFL
, fl
);
64 memset(&ev
, 0, sizeof(struct epoll_event
));
66 if (flags
& ULOOP_READ
)
67 ev
.events
|= EPOLLIN
| EPOLLRDHUP
;
69 if (flags
& ULOOP_WRITE
)
70 ev
.events
|= EPOLLOUT
;
72 if (flags
& ULOOP_EDGE_TRIGGER
)
75 ev
.data
.fd
= sock
->fd
;
78 ret
= epoll_ctl(epoll_fd
, op
, sock
->fd
, &ev
);
82 sock
->registered
= true;
89 int uloop_fd_delete(struct uloop_fd
*sock
)
91 sock
->registered
= false;
92 return epoll_ctl(epoll_fd
, EPOLL_CTL_DEL
, sock
->fd
, 0);
95 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
97 if (t1
->tv_sec
!= t2
->tv_sec
)
98 return (t1
->tv_sec
- t2
->tv_sec
) * 1000;
100 return (t1
->tv_usec
- t2
->tv_usec
) / 1000;
103 int uloop_timeout_add(struct uloop_timeout
*timeout
)
105 struct uloop_timeout
**head
= &first_timeout
;
106 struct uloop_timeout
*prev
= NULL
;
108 if (timeout
->pending
)
112 if (tv_diff(&(*head
)->time
, &timeout
->time
) > 0)
116 head
= &(*head
)->next
;
119 timeout
->prev
= prev
;
120 timeout
->next
= *head
;
122 timeout
->next
->prev
= timeout
;
124 timeout
->pending
= true;
129 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
131 struct timeval
*time
= &timeout
->time
;
133 if (timeout
->pending
)
134 uloop_timeout_cancel(timeout
);
136 gettimeofday(&timeout
->time
, NULL
);
138 time
->tv_sec
+= msecs
/ 1000;
139 time
->tv_usec
+= msecs
% 1000;
141 if (time
->tv_usec
> 1000000) {
143 time
->tv_usec
%= 100000;
146 return uloop_timeout_add(timeout
);
149 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
151 if (!timeout
->pending
)
155 timeout
->prev
->next
= timeout
->next
;
157 first_timeout
= timeout
->next
;
160 timeout
->next
->prev
= timeout
->prev
;
162 timeout
->pending
= false;
167 static void uloop_handle_sigint(int signo
)
172 static void uloop_setup_signals(void)
175 memset(&s
, 0, sizeof(struct sigaction
));
176 s
.sa_handler
= uloop_handle_sigint
;
178 sigaction(SIGINT
, &s
, NULL
);
181 static int uloop_get_next_timeout(struct timeval
*tv
)
188 diff
= tv_diff(&first_timeout
->time
, tv
);
195 static void uloop_process_timeouts(struct timeval
*tv
)
197 struct uloop_timeout
*timeout
;
199 while (first_timeout
) {
200 if (tv_diff(&first_timeout
->time
, tv
) > 0)
203 timeout
= first_timeout
;
204 uloop_timeout_cancel(timeout
);
206 timeout
->cb(timeout
);
217 epoll_fd
= epoll_create(32);
221 fcntl(epoll_fd
, F_SETFD
, fcntl(epoll_fd
, F_GETFD
) | FD_CLOEXEC
);
227 struct epoll_event events
[10];
232 uloop_setup_signals();
235 gettimeofday(&tv
, NULL
);
236 uloop_process_timeouts(&tv
);
237 timeout
= uloop_get_next_timeout(&tv
);
238 nfds
= epoll_wait(epoll_fd
, events
, ARRAY_SIZE(events
), timeout
);
239 for(n
= 0; n
< nfds
; ++n
)
241 struct uloop_fd
*u
= events
[n
].data
.ptr
;
244 if(events
[n
].events
& EPOLLERR
) {
249 if(!(events
[n
].events
& (EPOLLRDHUP
|EPOLLIN
|EPOLLOUT
|EPOLLERR
)))
252 if(events
[n
].events
& EPOLLRDHUP
)
255 if(events
[n
].events
& EPOLLIN
)
258 if(events
[n
].events
& EPOLLOUT
)
267 void uloop_done(void)