]> git.meshlink.io Git - meshlink/blob - src/event.c
Avoid allocating packet buffers unnecessarily.
[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 #ifndef EVENT_CLOCK
30 #if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
31 #define EVENT_CLOCK CLOCK_MONOTONIC_RAW
32 #else
33 #define EVENT_CLOCK CLOCK_MONOTONIC
34 #endif
35 #endif
36
37 static void timespec_add(const struct timespec *a, const struct timespec *b, struct timespec *r) {
38         r->tv_sec = a->tv_sec + b->tv_sec;
39         r->tv_nsec = a->tv_nsec + b->tv_nsec;
40
41         if(r->tv_nsec > 1000000000) {
42                 r->tv_sec++, r->tv_nsec -= 1000000000;
43         }
44 }
45
46 static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) {
47         r->tv_sec = a->tv_sec - b->tv_sec;
48         r->tv_nsec = a->tv_nsec - b->tv_nsec;
49
50         if(r->tv_nsec < 0) {
51                 r->tv_sec--, r->tv_nsec += 1000000000;
52         }
53 }
54
55 static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
56         if(a->tv_sec == b->tv_sec) {
57                 return a->tv_nsec < b->tv_nsec;
58         } else {
59                 return a->tv_sec < b->tv_sec;
60         }
61 }
62
63 static void timespec_clear(struct timespec *a) {
64         a->tv_sec = 0;
65 }
66
67 static bool timespec_isset(const struct timespec *a) {
68         return a->tv_sec;
69 }
70
71 static int io_compare(const io_t *a, const io_t *b) {
72         return a->fd - b->fd;
73 }
74
75 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
76         if(a->tv.tv_sec < b->tv.tv_sec) {
77                 return -1;
78         } else if(a->tv.tv_sec > b->tv.tv_sec) {
79                 return 1;
80         } else if(a->tv.tv_nsec < b->tv.tv_nsec) {
81                 return -1;
82         } else if(a->tv.tv_nsec > b->tv.tv_nsec) {
83                 return 1;
84         } else if(a < b) {
85                 return -1;
86         } else if(a > b) {
87                 return 1;
88         } else {
89                 return 0;
90         }
91 }
92
93 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
94         assert(!io->cb);
95
96         io->fd = fd;
97         io->cb = cb;
98         io->data = data;
99         io->node.data = io;
100
101         io_set(loop, io, flags);
102
103         splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
104         assert(node);
105         (void)node;
106 }
107
108 void io_set(event_loop_t *loop, io_t *io, int flags) {
109         assert(io->cb);
110
111         io->flags = flags;
112
113         if(flags & IO_READ) {
114                 FD_SET(io->fd, &loop->readfds);
115         } else {
116                 FD_CLR(io->fd, &loop->readfds);
117         }
118
119         if(flags & IO_WRITE) {
120                 FD_SET(io->fd, &loop->writefds);
121         } else {
122                 FD_CLR(io->fd, &loop->writefds);
123         }
124 }
125
126 void io_del(event_loop_t *loop, io_t *io) {
127         assert(io->cb);
128
129         loop->deletion = true;
130
131         io_set(loop, io, 0);
132
133         splay_unlink_node(&loop->ios, &io->node);
134         io->cb = NULL;
135 }
136
137 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timespec *tv) {
138         timeout->cb = cb;
139         timeout->data = data;
140         timeout->node.data = timeout;
141
142         timeout_set(loop, timeout, tv);
143 }
144
145 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timespec *tv) {
146         assert(timeout->cb);
147
148         if(timespec_isset(&timeout->tv)) {
149                 splay_unlink_node(&loop->timeouts, &timeout->node);
150         }
151
152         if(!loop->now.tv_sec) {
153                 clock_gettime(EVENT_CLOCK, &loop->now);
154         }
155
156         timespec_add(&loop->now, tv, &timeout->tv);
157
158         if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
159                 abort();
160         }
161
162         loop->deletion = true;
163 }
164
165 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
166         splay_unlink_node(&loop->timeouts, &timeout->node);
167         timespec_clear(&timeout->tv);
168 }
169
170 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
171         if(!timeout->cb) {
172                 return;
173         }
174
175         if(timespec_isset(&timeout->tv)) {
176                 timeout_disable(loop, timeout);
177         }
178
179         timeout->cb = NULL;
180         loop->deletion = true;
181 }
182
183 static int signal_compare(const signal_t *a, const signal_t *b) {
184         return (int)a->signum - (int)b->signum;
185 }
186
187 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
188         (void)data;
189         (void)flags;
190         unsigned char signum;
191
192         if(read(loop->pipefd[0], &signum, 1) != 1) {
193                 return;
194         }
195
196         signal_t *sig = splay_search(&loop->signals, &(signal_t) {
197                 .signum = signum
198         });
199
200         if(sig) {
201                 sig->set = false;
202                 sig->cb(loop, sig->data);
203         }
204 }
205
206 static void pipe_init(event_loop_t *loop) {
207         int result = pipe(loop->pipefd);
208         assert(result == 0);
209
210         if(result == 0) {
211                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
212         }
213 }
214
215 static void pipe_exit(event_loop_t *loop) {
216         io_del(loop, &loop->signalio);
217
218         close(loop->pipefd[0]);
219         close(loop->pipefd[1]);
220
221         loop->pipefd[0] = -1;
222         loop->pipefd[1] = -1;
223 }
224
225 void signal_trigger(event_loop_t *loop, signal_t *sig) {
226         if(sig->set) {
227                 return;
228         }
229
230         uint8_t signum = sig->signum;
231         sig->set = true;
232         write(loop->pipefd[1], &signum, 1);
233         return;
234 }
235
236 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
237         assert(!sig->cb);
238
239         sig->cb = cb;
240         sig->data = data;
241         sig->signum = signum;
242         sig->set = false;
243         sig->node.data = sig;
244
245         if(loop->pipefd[0] == -1) {
246                 pipe_init(loop);
247         }
248
249         if(!splay_insert_node(&loop->signals, &sig->node)) {
250                 abort();
251         }
252 }
253
254 void signal_del(event_loop_t *loop, signal_t *sig) {
255         assert(sig->cb);
256
257         loop->deletion = true;
258
259         splay_unlink_node(&loop->signals, &sig->node);
260         sig->cb = NULL;
261
262         if(!loop->signals.count && loop->pipefd[0] != -1) {
263                 pipe_exit(loop);
264         }
265 }
266
267 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
268         loop->idle_cb = cb;
269         loop->idle_data = data;
270 }
271
272 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
273         assert(mutex);
274
275         fd_set readable;
276         fd_set writable;
277
278         while(loop->running) {
279                 clock_gettime(EVENT_CLOCK, &loop->now);
280                 struct timespec it, ts = {3600, 0};
281
282                 while(loop->timeouts.head) {
283                         timeout_t *timeout = loop->timeouts.head->data;
284
285                         if(timespec_lt(&timeout->tv, &loop->now)) {
286                                 timeout_disable(loop, timeout);
287                                 timeout->cb(loop, timeout->data);
288                         } else {
289                                 timespec_sub(&timeout->tv, &loop->now, &ts);
290                                 break;
291                         }
292                 }
293
294                 if(loop->idle_cb) {
295                         it = loop->idle_cb(loop, loop->idle_data);
296
297                         if(it.tv_sec >= 0 && timespec_lt(&it, &ts)) {
298                                 ts = it;
299                         }
300                 }
301
302                 memcpy(&readable, &loop->readfds, sizeof(readable));
303                 memcpy(&writable, &loop->writefds, sizeof(writable));
304
305                 int fds = 0;
306
307                 if(loop->ios.tail) {
308                         io_t *last = loop->ios.tail->data;
309                         fds = last->fd + 1;
310                 }
311
312                 // release mesh mutex during select
313                 pthread_mutex_unlock(mutex);
314
315 #ifdef HAVE_PSELECT
316                 int n = pselect(fds, &readable, &writable, NULL, &ts, NULL);
317 #else
318                 struct timeval tv = {ts.tv_sec, ts.tv_nsec / 1000};
319                 int n = select(fds, &readable, &writable, NULL, (struct timeval *)&tv);
320 #endif
321
322                 pthread_mutex_lock(mutex);
323
324                 clock_gettime(EVENT_CLOCK, &loop->now);
325
326                 if(n < 0) {
327                         if(sockwouldblock(errno)) {
328                                 continue;
329                         } else {
330                                 return false;
331                         }
332                 }
333
334                 if(!n) {
335                         continue;
336                 }
337
338                 // Normally, splay_each allows the current node to be deleted. However,
339                 // it can be that one io callback triggers the deletion of another io,
340                 // so we have to detect this and break the loop.
341
342                 loop->deletion = false;
343
344                 for splay_each(io_t, io, &loop->ios) {
345                         if(FD_ISSET(io->fd, &writable) && io->cb) {
346                                 io->cb(loop, io->data, IO_WRITE);
347                         }
348
349                         if(loop->deletion) {
350                                 break;
351                         }
352
353                         if(FD_ISSET(io->fd, &readable) && io->cb) {
354                                 io->cb(loop, io->data, IO_READ);
355                         }
356
357                         if(loop->deletion) {
358                                 break;
359                         }
360                 }
361         }
362
363         return true;
364 }
365
366 void event_flush_output(event_loop_t *loop) {
367         for splay_each(io_t, io, &loop->ios)
368                 if(FD_ISSET(io->fd, &loop->writefds)) {
369                         io->cb(loop, io->data, IO_WRITE);
370                 }
371 }
372
373 void event_loop_start(event_loop_t *loop) {
374         loop->running = true;
375 }
376
377 void event_loop_stop(event_loop_t *loop) {
378         loop->running = false;
379 }
380
381 void event_loop_init(event_loop_t *loop) {
382         loop->ios.compare = (splay_compare_t)io_compare;
383         loop->timeouts.compare = (splay_compare_t)timeout_compare;
384         loop->signals.compare = (splay_compare_t)signal_compare;
385         loop->pipefd[0] = -1;
386         loop->pipefd[1] = -1;
387         clock_gettime(EVENT_CLOCK, &loop->now);
388 }
389
390 void event_loop_exit(event_loop_t *loop) {
391         assert(!loop->ios.count);
392         assert(!loop->timeouts.count);
393         assert(!loop->signals.count);
394
395         for splay_each(io_t, io, &loop->ios) {
396                 splay_unlink_node(&loop->ios, node);
397         }
398
399         for splay_each(timeout_t, timeout, &loop->timeouts) {
400                 splay_unlink_node(&loop->timeouts, node);
401         }
402
403         for splay_each(signal_t, signal, &loop->signals) {
404                 splay_unlink_node(&loop->signals, node);
405         }
406 }