]> git.meshlink.io Git - meshlink/blob - src/event.c
Reduce how often we have to poll the packet queue.
[meshlink] / src / event.c
1 /*
2     event.c -- I/O, timeout and signal event handling
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "dropin.h"
23 #include "event.h"
24 #include "net.h"
25 #include "splay_tree.h"
26 #include "utils.h"
27 #include "xalloc.h"
28
29 static int io_compare(const io_t *a, const io_t *b) {
30         return a->fd - b->fd;
31 }
32
33 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
34         struct timeval diff;
35         timersub(&a->tv, &b->tv, &diff);
36
37         if(diff.tv_sec < 0) {
38                 return -1;
39         }
40
41         if(diff.tv_sec > 0) {
42                 return 1;
43         }
44
45         if(diff.tv_usec < 0) {
46                 return -1;
47         }
48
49         if(diff.tv_usec > 0) {
50                 return 1;
51         }
52
53         if(a < b) {
54                 return -1;
55         }
56
57         if(a > b) {
58                 return 1;
59         }
60
61         return 0;
62 }
63
64 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
65         assert(!io->cb);
66
67         io->fd = fd;
68         io->cb = cb;
69         io->data = data;
70         io->node.data = io;
71
72         io_set(loop, io, flags);
73
74         splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
75         assert(node);
76         (void)node;
77 }
78
79 void io_set(event_loop_t *loop, io_t *io, int flags) {
80         assert(io->cb);
81
82         io->flags = flags;
83
84         if(flags & IO_READ) {
85                 FD_SET(io->fd, &loop->readfds);
86         } else {
87                 FD_CLR(io->fd, &loop->readfds);
88         }
89
90         if(flags & IO_WRITE) {
91                 FD_SET(io->fd, &loop->writefds);
92         } else {
93                 FD_CLR(io->fd, &loop->writefds);
94         }
95 }
96
97 void io_del(event_loop_t *loop, io_t *io) {
98         assert(io->cb);
99
100         loop->deletion = true;
101
102         io_set(loop, io, 0);
103
104         splay_unlink_node(&loop->ios, &io->node);
105         io->cb = NULL;
106 }
107
108 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
109         timeout->cb = cb;
110         timeout->data = data;
111         timeout->node.data = timeout;
112
113         timeout_set(loop, timeout, tv);
114 }
115
116 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
117         assert(timeout->cb);
118
119         if(timerisset(&timeout->tv)) {
120                 splay_unlink_node(&loop->timeouts, &timeout->node);
121         }
122
123         if(!loop->now.tv_sec) {
124                 gettimeofday(&loop->now, NULL);
125         }
126
127         timeradd(&loop->now, tv, &timeout->tv);
128
129         if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
130                 abort();
131         }
132
133         loop->deletion = true;
134 }
135
136 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
137         splay_unlink_node(&loop->timeouts, &timeout->node);
138         timeout->tv = (struct timeval) {
139                 0, 0
140         };
141 }
142
143 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
144         if(!timeout->cb) {
145                 return;
146         }
147
148         if(timerisset(&timeout->tv)) {
149                 timeout_disable(loop, timeout);
150         }
151
152         timeout->cb = NULL;
153         loop->deletion = true;
154 }
155
156 static int signal_compare(const signal_t *a, const signal_t *b) {
157         return (int)a->signum - (int)b->signum;
158 }
159
160 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
161         (void)data;
162         (void)flags;
163         unsigned char signum;
164
165         if(read(loop->pipefd[0], &signum, 1) != 1) {
166                 return;
167         }
168
169         signal_t *sig = splay_search(&loop->signals, &(signal_t) {
170                 .signum = signum
171         });
172
173         if(sig) {
174                 sig->set = false;
175                 sig->cb(loop, sig->data);
176         }
177 }
178
179 static void pipe_init(event_loop_t *loop) {
180         int result = pipe(loop->pipefd);
181         assert(result == 0);
182
183         if(result == 0) {
184                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
185         }
186 }
187
188 static void pipe_exit(event_loop_t *loop) {
189         io_del(loop, &loop->signalio);
190
191         close(loop->pipefd[0]);
192         close(loop->pipefd[1]);
193
194         loop->pipefd[0] = -1;
195         loop->pipefd[1] = -1;
196 }
197
198 void signal_trigger(event_loop_t *loop, signal_t *sig) {
199         if(sig->set) {
200                 return;
201         }
202
203         uint8_t signum = sig->signum;
204         sig->set = true;
205         write(loop->pipefd[1], &signum, 1);
206         return;
207 }
208
209 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
210         assert(!sig->cb);
211
212         sig->cb = cb;
213         sig->data = data;
214         sig->signum = signum;
215         sig->set = false;
216         sig->node.data = sig;
217
218         if(loop->pipefd[0] == -1) {
219                 pipe_init(loop);
220         }
221
222         if(!splay_insert_node(&loop->signals, &sig->node)) {
223                 abort();
224         }
225 }
226
227 void signal_del(event_loop_t *loop, signal_t *sig) {
228         assert(sig->cb);
229
230         loop->deletion = true;
231
232         splay_unlink_node(&loop->signals, &sig->node);
233         sig->cb = NULL;
234
235         if(!loop->signals.count && loop->pipefd[0] != -1) {
236                 pipe_exit(loop);
237         }
238 }
239
240 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
241         loop->idle_cb = cb;
242         loop->idle_data = data;
243 }
244
245 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
246         assert(mutex);
247
248         fd_set readable;
249         fd_set writable;
250
251         while(loop->running) {
252                 gettimeofday(&loop->now, NULL);
253                 struct timeval diff, it, *tv = NULL;
254
255                 while(loop->timeouts.head) {
256                         timeout_t *timeout = loop->timeouts.head->data;
257                         timersub(&timeout->tv, &loop->now, &diff);
258
259                         if(diff.tv_sec < 0) {
260                                 timeout_disable(loop, timeout);
261                                 timeout->cb(loop, timeout->data);
262                         } else {
263                                 tv = &diff;
264                                 break;
265                         }
266                 }
267
268                 if(loop->idle_cb) {
269                         it = loop->idle_cb(loop, loop->idle_data);
270
271                         if(it.tv_sec >= 0 && (!tv || timercmp(&it, tv, <))) {
272                                 tv = &it;
273                         }
274                 }
275
276                 memcpy(&readable, &loop->readfds, sizeof(readable));
277                 memcpy(&writable, &loop->writefds, sizeof(writable));
278
279                 int fds = 0;
280
281                 if(loop->ios.tail) {
282                         io_t *last = loop->ios.tail->data;
283                         fds = last->fd + 1;
284                 }
285
286                 // release mesh mutex during select
287                 pthread_mutex_unlock(mutex);
288
289                 int n = select(fds, &readable, &writable, NULL, tv);
290
291                 pthread_mutex_lock(mutex);
292
293                 gettimeofday(&loop->now, NULL);
294
295                 if(n < 0) {
296                         if(sockwouldblock(errno)) {
297                                 continue;
298                         } else {
299                                 return false;
300                         }
301                 }
302
303                 if(!n) {
304                         continue;
305                 }
306
307                 // Normally, splay_each allows the current node to be deleted. However,
308                 // it can be that one io callback triggers the deletion of another io,
309                 // so we have to detect this and break the loop.
310
311                 loop->deletion = false;
312
313                 for splay_each(io_t, io, &loop->ios) {
314                         if(FD_ISSET(io->fd, &writable) && io->cb) {
315                                 io->cb(loop, io->data, IO_WRITE);
316                         }
317
318                         if(loop->deletion) {
319                                 break;
320                         }
321
322                         if(FD_ISSET(io->fd, &readable) && io->cb) {
323                                 io->cb(loop, io->data, IO_READ);
324                         }
325
326                         if(loop->deletion) {
327                                 break;
328                         }
329                 }
330         }
331
332         return true;
333 }
334
335 void event_flush_output(event_loop_t *loop) {
336         for splay_each(io_t, io, &loop->ios)
337                 if(FD_ISSET(io->fd, &loop->writefds)) {
338                         io->cb(loop, io->data, IO_WRITE);
339                 }
340 }
341
342 void event_loop_start(event_loop_t *loop) {
343         loop->running = true;
344 }
345
346 void event_loop_stop(event_loop_t *loop) {
347         loop->running = false;
348 }
349
350 void event_loop_init(event_loop_t *loop) {
351         loop->ios.compare = (splay_compare_t)io_compare;
352         loop->timeouts.compare = (splay_compare_t)timeout_compare;
353         loop->signals.compare = (splay_compare_t)signal_compare;
354         loop->pipefd[0] = -1;
355         loop->pipefd[1] = -1;
356         gettimeofday(&loop->now, NULL);
357 }
358
359 void event_loop_exit(event_loop_t *loop) {
360         assert(!loop->ios.count);
361         assert(!loop->timeouts.count);
362         assert(!loop->signals.count);
363
364         for splay_each(io_t, io, &loop->ios) {
365                 splay_unlink_node(&loop->ios, node);
366         }
367
368         for splay_each(timeout_t, timeout, &loop->timeouts) {
369                 splay_unlink_node(&loop->timeouts, node);
370         }
371
372         for splay_each(signal_t, signal, &loop->signals) {
373                 splay_unlink_node(&loop->signals, node);
374         }
375 }