port uloop to BSD kqueue
[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
25 #include "uloop.h"
26
27 #ifdef USE_KQUEUE
28 #include <sys/event.h>
29 #endif
30 #ifdef USE_EPOLL
31 #include <sys/epoll.h>
32 #endif
33
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <poll.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <signal.h>
42 #include <stdbool.h>
43
44 #ifndef ARRAY_SIZE
45 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
46 #endif
47 #define ULOOP_MAX_EVENTS 10
48
49 struct uloop_timeout *first_timeout;
50 static int poll_fd;
51 static bool cancel;
52
53 #ifdef USE_KQUEUE
54
55 int uloop_init(void)
56 {
57         poll_fd = kqueue();
58         if (poll_fd < 0)
59                 return -1;
60
61         return 0;
62 }
63
64
65 static uint16_t get_flags(unsigned int flags, unsigned int mask)
66 {
67         uint16_t kflags = 0;
68
69         if (!(flags & mask))
70                 return EV_DELETE;
71
72         kflags = EV_ADD;
73         if (flags & ULOOP_EDGE_TRIGGER)
74                 kflags |= EV_CLEAR;
75
76         return kflags;
77 }
78
79 static int register_poll(struct uloop_fd *fd, unsigned int flags)
80 {
81         struct timespec timeout = { 0, 0 };
82         struct kevent ev[2];
83         unsigned int changed;
84         int nev = 0;
85
86         changed = fd->kqflags ^ flags;
87         if (changed & ULOOP_EDGE_TRIGGER)
88                 changed |= flags;
89
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);
93         }
94
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);
98         }
99
100         if (nev && (kevent(poll_fd, ev, nev, NULL, 0, &timeout) == -1))
101                 return -1;
102
103         fd->kqflags = flags;
104         return 0;
105 }
106
107 int uloop_fd_delete(struct uloop_fd *sock)
108 {
109         sock->registered = false;
110         return register_poll(sock, 0);
111 }
112
113 static void uloop_run_events(int timeout)
114 {
115         struct kevent events[ULOOP_MAX_EVENTS];
116         struct timespec ts;
117         int nfds, n;
118
119         if (timeout < 0) {
120                 ts.tv_sec = 0;
121                 ts.tv_nsec = 0;
122         } else {
123                 ts.tv_sec = timeout / 1000;
124                 ts.tv_nsec = timeout * 1000000;
125         }
126
127         nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), &ts);
128         for(n = 0; n < nfds; ++n)
129         {
130                 struct uloop_fd *u = events[n].udata;
131                 unsigned int ev = 0;
132
133                 if(events[n].flags & EV_ERROR) {
134                         u->error = true;
135                         uloop_fd_delete(u);
136                 }
137
138                 if(events[n].filter == EVFILT_READ)
139                         ev |= ULOOP_READ;
140                 else if (events[n].filter == EVFILT_WRITE)
141                         ev |= ULOOP_WRITE;
142
143                 if(events[n].flags & EV_EOF)
144                         u->eof = true;
145                 else if (!ev)
146                         continue;
147
148                 if(u->cb)
149                         u->cb(u, ev);
150         }
151 }
152
153 #endif
154
155 #ifdef USE_EPOLL
156
157 /**
158  * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
159  */
160 #ifndef EPOLLRDHUP
161 #define EPOLLRDHUP 0x2000
162 #endif
163
164 int uloop_init(void)
165 {
166         poll_fd = epoll_create(32);
167         if (poll_fd < 0)
168                 return -1;
169
170         fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
171         return 0;
172 }
173
174 static int register_poll(struct uloop_fd *fd, unsigned int flags)
175 {
176         struct epoll_event ev;
177         int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
178
179         memset(&ev, 0, sizeof(struct epoll_event));
180
181         if (flags & ULOOP_READ)
182                 ev.events |= EPOLLIN | EPOLLRDHUP;
183
184         if (flags & ULOOP_WRITE)
185                 ev.events |= EPOLLOUT;
186
187         if (flags & ULOOP_EDGE_TRIGGER)
188                 ev.events |= EPOLLET;
189
190         ev.data.fd = fd->fd;
191         ev.data.ptr = fd;
192
193         return epoll_ctl(poll_fd, op, fd->fd, &ev);
194 }
195
196 int uloop_fd_delete(struct uloop_fd *sock)
197 {
198         sock->registered = false;
199         return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
200 }
201
202 static void uloop_run_events(int timeout)
203 {
204         struct epoll_event events[ULOOP_MAX_EVENTS];
205         int nfds, n;
206
207         nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
208         for(n = 0; n < nfds; ++n)
209         {
210                 struct uloop_fd *u = events[n].data.ptr;
211                 unsigned int ev = 0;
212
213                 if(events[n].events & EPOLLERR) {
214                         u->error = true;
215                         uloop_fd_delete(u);
216                 }
217
218                 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR)))
219                         continue;
220
221                 if(events[n].events & EPOLLRDHUP)
222                         u->eof = true;
223
224                 if(events[n].events & EPOLLIN)
225                         ev |= ULOOP_READ;
226
227                 if(events[n].events & EPOLLOUT)
228                         ev |= ULOOP_WRITE;
229
230                 if(u->cb)
231                         u->cb(u, ev);
232         }
233 }
234
235 #endif
236
237 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
238 {
239         unsigned int fl;
240         int ret;
241
242         if (!sock->registered) {
243                 fl = fcntl(sock->fd, F_GETFL, 0);
244                 fl |= O_NONBLOCK;
245                 fcntl(sock->fd, F_SETFL, fl);
246         }
247
248         ret = register_poll(sock, flags);
249         if (ret < 0)
250                 goto out;
251
252         sock->registered = true;
253         sock->eof = false;
254
255 out:
256         return ret;
257 }
258
259 static int tv_diff(struct timeval *t1, struct timeval *t2)
260 {
261         if (t1->tv_sec != t2->tv_sec)
262                 return (t1->tv_sec - t2->tv_sec) * 1000;
263         else
264                 return (t1->tv_usec - t2->tv_usec) / 1000;
265 }
266
267 int uloop_timeout_add(struct uloop_timeout *timeout)
268 {
269         struct uloop_timeout **head = &first_timeout;
270         struct uloop_timeout *prev = NULL;
271
272         if (timeout->pending)
273                 return -1;
274
275         while (*head) {
276                 if (tv_diff(&(*head)->time, &timeout->time) > 0)
277                         break;
278
279                 prev = *head;
280                 head = &(*head)->next;
281         }
282
283         timeout->prev = prev;
284         timeout->next = *head;
285         if (timeout->next)
286                 timeout->next->prev = timeout;
287         *head = timeout;
288         timeout->pending = true;
289
290         return 0;
291 }
292
293 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
294 {
295         struct timeval *time = &timeout->time;
296
297         if (timeout->pending)
298                 uloop_timeout_cancel(timeout);
299
300         gettimeofday(&timeout->time, NULL);
301
302         time->tv_sec += msecs / 1000;
303         time->tv_usec += msecs % 1000;
304
305         if (time->tv_usec > 1000000) {
306                 time->tv_sec++;
307                 time->tv_usec %= 100000;
308         }
309
310         return uloop_timeout_add(timeout);
311 }
312
313 int uloop_timeout_cancel(struct uloop_timeout *timeout)
314 {
315         if (!timeout->pending)
316                 return -1;
317
318         if (timeout->prev)
319                 timeout->prev->next = timeout->next;
320         else
321                 first_timeout = timeout->next;
322
323         if (timeout->next)
324                 timeout->next->prev = timeout->prev;
325
326         timeout->pending = false;
327
328         return 0;
329 }
330
331 static void uloop_handle_sigint(int signo)
332 {
333         cancel = true;
334 }
335
336 static void uloop_setup_signals(void)
337 {
338         struct sigaction s;
339         memset(&s, 0, sizeof(struct sigaction));
340         s.sa_handler = uloop_handle_sigint;
341         s.sa_flags = 0;
342         sigaction(SIGINT, &s, NULL);
343 }
344
345 static int uloop_get_next_timeout(struct timeval *tv)
346 {
347         int diff;
348
349         if (!first_timeout)
350                 return -1;
351
352         diff = tv_diff(&first_timeout->time, tv);
353         if (diff < 0)
354                 return 0;
355
356         return diff;
357 }
358
359 static void uloop_process_timeouts(struct timeval *tv)
360 {
361         struct uloop_timeout *timeout;
362
363         while (first_timeout) {
364                 if (tv_diff(&first_timeout->time, tv) > 0)
365                         break;
366
367                 timeout = first_timeout;
368                 uloop_timeout_cancel(timeout);
369                 if (timeout->cb)
370                         timeout->cb(timeout);
371         }
372 }
373
374 void uloop_end(void)
375 {
376         cancel = true;
377 }
378
379 void uloop_run(void)
380 {
381         struct timeval tv;
382
383         uloop_setup_signals();
384         while(!cancel)
385         {
386                 gettimeofday(&tv, NULL);
387                 uloop_process_timeouts(&tv);
388                 uloop_run_events(uloop_get_next_timeout(&tv));
389         }
390 }
391
392 void uloop_done(void)
393 {
394         close(poll_fd);
395 }