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 if (!sock
->registered
) {
61 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
63 fcntl(sock
->fd
, F_SETFL
, fl
);
66 memset(&ev
, 0, sizeof(struct epoll_event
));
68 if (flags
& ULOOP_READ
)
69 ev
.events
|= EPOLLIN
| EPOLLRDHUP
;
71 if (flags
& ULOOP_WRITE
)
72 ev
.events
|= EPOLLOUT
;
74 if (flags
& ULOOP_EDGE_TRIGGER
)
77 ev
.data
.fd
= sock
->fd
;
80 ret
= epoll_ctl(epoll_fd
, op
, sock
->fd
, &ev
);
84 sock
->registered
= true;
91 int uloop_fd_delete(struct uloop_fd
*sock
)
93 sock
->registered
= false;
94 return epoll_ctl(epoll_fd
, EPOLL_CTL_DEL
, sock
->fd
, 0);
97 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
99 if (t1
->tv_sec
!= t2
->tv_sec
)
100 return (t1
->tv_sec
- t2
->tv_sec
) * 1000;
102 return (t1
->tv_usec
- t2
->tv_usec
) / 1000;
105 int uloop_timeout_add(struct uloop_timeout
*timeout
)
107 struct uloop_timeout
**head
= &first_timeout
;
108 struct uloop_timeout
*prev
= NULL
;
110 if (timeout
->pending
)
114 if (tv_diff(&(*head
)->time
, &timeout
->time
) > 0)
118 head
= &(*head
)->next
;
121 timeout
->prev
= prev
;
122 timeout
->next
= *head
;
124 timeout
->next
->prev
= timeout
;
126 timeout
->pending
= true;
131 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
133 struct timeval
*time
= &timeout
->time
;
135 if (timeout
->pending
)
136 uloop_timeout_cancel(timeout
);
138 gettimeofday(&timeout
->time
, NULL
);
140 time
->tv_sec
+= msecs
/ 1000;
141 time
->tv_usec
+= msecs
% 1000;
143 if (time
->tv_usec
> 1000000) {
145 time
->tv_usec
%= 100000;
148 return uloop_timeout_add(timeout
);
151 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
153 if (!timeout
->pending
)
157 timeout
->prev
->next
= timeout
->next
;
159 first_timeout
= timeout
->next
;
162 timeout
->next
->prev
= timeout
->prev
;
164 timeout
->pending
= false;
169 static void uloop_handle_sigint(int signo
)
174 static void uloop_setup_signals(void)
177 memset(&s
, 0, sizeof(struct sigaction
));
178 s
.sa_handler
= uloop_handle_sigint
;
180 sigaction(SIGINT
, &s
, NULL
);
183 static int uloop_get_next_timeout(struct timeval
*tv
)
190 diff
= tv_diff(&first_timeout
->time
, tv
);
197 static void uloop_process_timeouts(struct timeval
*tv
)
199 struct uloop_timeout
*timeout
;
201 while (first_timeout
) {
202 if (tv_diff(&first_timeout
->time
, tv
) > 0)
205 timeout
= first_timeout
;
206 uloop_timeout_cancel(timeout
);
208 timeout
->cb(timeout
);
219 epoll_fd
= epoll_create(32);
223 fcntl(epoll_fd
, F_SETFD
, fcntl(epoll_fd
, F_GETFD
) | FD_CLOEXEC
);
229 struct epoll_event events
[10];
234 uloop_setup_signals();
237 gettimeofday(&tv
, NULL
);
238 uloop_process_timeouts(&tv
);
239 timeout
= uloop_get_next_timeout(&tv
);
240 nfds
= epoll_wait(epoll_fd
, events
, ARRAY_SIZE(events
), timeout
);
241 for(n
= 0; n
< nfds
; ++n
)
243 struct uloop_fd
*u
= events
[n
].data
.ptr
;
246 if(events
[n
].events
& EPOLLERR
) {
251 if(!(events
[n
].events
& (EPOLLRDHUP
|EPOLLIN
|EPOLLOUT
|EPOLLERR
)))
254 if(events
[n
].events
& EPOLLRDHUP
)
257 if(events
[n
].events
& EPOLLIN
)
260 if(events
[n
].events
& EPOLLOUT
)
269 void uloop_done(void)