Initial import
[project/libubox.git] / uloop.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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.
19 *
20 */
21
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #include <sys/epoll.h>
25
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <poll.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <stdbool.h>
35
36 #include "uloop.h"
37
38 /**
39 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
40 */
41 #ifndef EPOLLRDHUP
42 #define EPOLLRDHUP 0x2000
43 #endif
44
45 #ifndef ARRAY_SIZE
46 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
47 #endif
48
49 struct uloop_timeout *first_timeout;
50 static int epoll_fd;
51 static bool cancel;
52
53 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
54 {
55 struct epoll_event ev;
56 int op = sock->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
57 unsigned int fl;
58 int ret;
59
60 if (!sock->registered) {
61 fl = fcntl(sock->fd, F_GETFL, 0);
62 fl |= O_NONBLOCK;
63 fcntl(sock->fd, F_SETFL, fl);
64 }
65
66 memset(&ev, 0, sizeof(struct epoll_event));
67
68 if (flags & ULOOP_READ)
69 ev.events |= EPOLLIN | EPOLLRDHUP;
70
71 if (flags & ULOOP_WRITE)
72 ev.events |= EPOLLOUT;
73
74 if (flags & ULOOP_EDGE_TRIGGER)
75 ev.events |= EPOLLET;
76
77 ev.data.fd = sock->fd;
78 ev.data.ptr = sock;
79
80 ret = epoll_ctl(epoll_fd, op, sock->fd, &ev);
81 if (ret < 0)
82 goto out;
83
84 sock->registered = true;
85 sock->eof = false;
86
87 out:
88 return ret;
89 }
90
91 int uloop_fd_delete(struct uloop_fd *sock)
92 {
93 sock->registered = false;
94 return epoll_ctl(epoll_fd, EPOLL_CTL_DEL, sock->fd, 0);
95 }
96
97 static int tv_diff(struct timeval *t1, struct timeval *t2)
98 {
99 if (t1->tv_sec != t2->tv_sec)
100 return (t1->tv_sec - t2->tv_sec) * 1000;
101 else
102 return (t1->tv_usec - t2->tv_usec) / 1000;
103 }
104
105 int uloop_timeout_add(struct uloop_timeout *timeout)
106 {
107 struct uloop_timeout **head = &first_timeout;
108 struct uloop_timeout *prev = NULL;
109
110 if (timeout->pending)
111 return -1;
112
113 while (*head) {
114 if (tv_diff(&(*head)->time, &timeout->time) > 0)
115 break;
116
117 prev = *head;
118 head = &(*head)->next;
119 }
120
121 timeout->prev = prev;
122 timeout->next = *head;
123 if (timeout->next)
124 timeout->next->prev = timeout;
125 *head = timeout;
126 timeout->pending = true;
127
128 return 0;
129 }
130
131 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
132 {
133 struct timeval *time = &timeout->time;
134
135 if (timeout->pending)
136 uloop_timeout_cancel(timeout);
137
138 gettimeofday(&timeout->time, NULL);
139
140 time->tv_sec += msecs / 1000;
141 time->tv_usec += msecs % 1000;
142
143 if (time->tv_usec > 1000000) {
144 time->tv_sec++;
145 time->tv_usec %= 100000;
146 }
147
148 return uloop_timeout_add(timeout);
149 }
150
151 int uloop_timeout_cancel(struct uloop_timeout *timeout)
152 {
153 if (!timeout->pending)
154 return -1;
155
156 if (timeout->prev)
157 timeout->prev->next = timeout->next;
158 else
159 first_timeout = timeout->next;
160
161 if (timeout->next)
162 timeout->next->prev = timeout->prev;
163
164 timeout->pending = false;
165
166 return 0;
167 }
168
169 static void uloop_handle_sigint(int signo)
170 {
171 cancel = true;
172 }
173
174 static void uloop_setup_signals(void)
175 {
176 struct sigaction s;
177 memset(&s, 0, sizeof(struct sigaction));
178 s.sa_handler = uloop_handle_sigint;
179 s.sa_flags = 0;
180 sigaction(SIGINT, &s, NULL);
181 }
182
183 static int uloop_get_next_timeout(struct timeval *tv)
184 {
185 int diff;
186
187 if (!first_timeout)
188 return -1;
189
190 diff = tv_diff(&first_timeout->time, tv);
191 if (diff < 0)
192 return 0;
193
194 return diff;
195 }
196
197 static void uloop_process_timeouts(struct timeval *tv)
198 {
199 struct uloop_timeout *timeout;
200
201 while (first_timeout) {
202 if (tv_diff(&first_timeout->time, tv) > 0)
203 break;
204
205 timeout = first_timeout;
206 uloop_timeout_cancel(timeout);
207 if (timeout->cb)
208 timeout->cb(timeout);
209 }
210 }
211
212 void uloop_end(void)
213 {
214 cancel = true;
215 }
216
217 int uloop_init(void)
218 {
219 epoll_fd = epoll_create(32);
220 if (epoll_fd < 0)
221 return -1;
222
223 fcntl(epoll_fd, F_SETFD, fcntl(epoll_fd, F_GETFD) | FD_CLOEXEC);
224 return 0;
225 }
226
227 void uloop_run(void)
228 {
229 struct epoll_event events[10];
230 struct timeval tv;
231 int timeout;
232 int nfds, n;
233
234 uloop_setup_signals();
235 while(!cancel)
236 {
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)
242 {
243 struct uloop_fd *u = events[n].data.ptr;
244 unsigned int ev = 0;
245
246 if(events[n].events & EPOLLERR) {
247 u->error = true;
248 uloop_fd_delete(u);
249 }
250
251 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR)))
252 continue;
253
254 if(events[n].events & EPOLLRDHUP)
255 u->eof = true;
256
257 if(events[n].events & EPOLLIN)
258 ev |= ULOOP_READ;
259
260 if(events[n].events & EPOLLOUT)
261 ev |= ULOOP_WRITE;
262
263 if(u->cb)
264 u->cb(u, ev);
265 }
266 }
267 }
268
269 void uloop_done(void)
270 {
271 close(epoll_fd);
272 }