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