]> git.meshlink.io Git - meshlink/blob - src/event.c
Update mesh->loop.now right after select() returns.
[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         if(io->cb) {
66                 return;
67         }
68
69         io->fd = fd;
70         io->cb = cb;
71         io->data = data;
72         io->node.data = io;
73
74         io_set(loop, io, flags);
75
76         if(!splay_insert_node(&loop->ios, &io->node)) {
77                 abort();
78         }
79 }
80
81 void io_set(event_loop_t *loop, io_t *io, int flags) {
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         if(!io->cb) {
99                 return;
100         }
101
102         loop->deletion = true;
103
104         io_set(loop, io, 0);
105
106         splay_unlink_node(&loop->ios, &io->node);
107         io->cb = NULL;
108 }
109
110 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
111         timeout->cb = cb;
112         timeout->data = data;
113         timeout->node.data = timeout;
114
115         timeout_set(loop, timeout, tv);
116 }
117
118 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
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
134 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
135         if(!timeout->cb) {
136                 return;
137         }
138
139         loop->deletion = true;
140
141         splay_unlink_node(&loop->timeouts, &timeout->node);
142         timeout->cb = 0;
143         timeout->tv = (struct timeval) {
144                 0, 0
145         };
146 }
147
148 static int signal_compare(const signal_t *a, const signal_t *b) {
149         return (int)a->signum - (int)b->signum;
150 }
151
152 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
153         (void)data;
154         (void)flags;
155         unsigned char signum;
156
157         if(read(loop->pipefd[0], &signum, 1) != 1) {
158                 return;
159         }
160
161         signal_t *sig = splay_search(&loop->signals, &((signal_t) {
162                 .signum = signum
163         }));
164
165         if(sig) {
166                 sig->cb(loop, sig->data);
167         }
168 }
169
170 static void pipe_init(event_loop_t *loop) {
171         if(!pipe(loop->pipefd)) {
172                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
173         }
174 }
175
176 void signal_trigger(event_loop_t *loop, signal_t *sig) {
177         uint8_t signum = sig->signum;
178         write(loop->pipefd[1], &signum, 1);
179         return;
180 }
181
182 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
183         if(sig->cb) {
184                 return;
185         }
186
187         sig->cb = cb;
188         sig->data = data;
189         sig->signum = signum;
190         sig->node.data = sig;
191
192         if(loop->pipefd[0] == -1) {
193                 pipe_init(loop);
194         }
195
196         if(!splay_insert_node(&loop->signals, &sig->node)) {
197                 abort();
198         }
199 }
200
201 void signal_del(event_loop_t *loop, signal_t *sig) {
202         if(!sig->cb) {
203                 return;
204         }
205
206         loop->deletion = true;
207
208         splay_unlink_node(&loop->signals, &sig->node);
209         sig->cb = NULL;
210 }
211
212 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
213         loop->idle_cb = cb;
214         loop->idle_data = data;
215 }
216
217 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
218         fd_set readable;
219         fd_set writable;
220
221
222         while(loop->running) {
223                 gettimeofday(&loop->now, NULL);
224                 struct timeval diff, it, *tv = NULL;
225
226                 while(loop->timeouts.head) {
227                         timeout_t *timeout = loop->timeouts.head->data;
228                         timersub(&timeout->tv, &loop->now, &diff);
229
230                         if(diff.tv_sec < 0) {
231                                 timeout->cb(loop, timeout->data);
232
233                                 if(timercmp(&timeout->tv, &loop->now, <)) {
234                                         timeout_del(loop, timeout);
235                                 }
236                         } else {
237                                 tv = &diff;
238                                 break;
239                         }
240                 }
241
242                 if(loop->idle_cb) {
243                         it = loop->idle_cb(loop, loop->idle_data);
244
245                         if(it.tv_sec >= 0 && (!tv || timercmp(&it, tv, <))) {
246                                 tv = &it;
247                         }
248                 }
249
250                 memcpy(&readable, &loop->readfds, sizeof(readable));
251                 memcpy(&writable, &loop->writefds, sizeof(writable));
252
253                 int fds = 0;
254
255                 if(loop->ios.tail) {
256                         io_t *last = loop->ios.tail->data;
257                         fds = last->fd + 1;
258                 }
259
260                 // release mesh mutex during select
261                 if(mutex) {
262                         pthread_mutex_unlock(mutex);
263                 }
264
265                 int n = select(fds, &readable, &writable, NULL, tv);
266
267                 if(mutex) {
268                         pthread_mutex_lock(mutex);
269                 }
270
271                 gettimeofday(&loop->now, NULL);
272
273                 if(n < 0) {
274                         if(sockwouldblock(errno)) {
275                                 continue;
276                         } else {
277                                 return false;
278                         }
279                 }
280
281                 if(!n) {
282                         continue;
283                 }
284
285                 // Normally, splay_each allows the current node to be deleted. However,
286                 // it can be that one io callback triggers the deletion of another io,
287                 // so we have to detect this and break the loop.
288
289                 loop->deletion = false;
290
291                 for splay_each(io_t, io, &loop->ios) {
292                         if(FD_ISSET(io->fd, &writable) && io->cb) {
293                                 io->cb(loop, io->data, IO_WRITE);
294                         }
295
296                         if(loop->deletion) {
297                                 break;
298                         }
299
300                         if(FD_ISSET(io->fd, &readable) && io->cb) {
301                                 io->cb(loop, io->data, IO_READ);
302                         }
303
304                         if(loop->deletion) {
305                                 break;
306                         }
307                 }
308         }
309
310         return true;
311 }
312
313 void event_flush_output(event_loop_t *loop) {
314         for splay_each(io_t, io, &loop->ios)
315                 if(FD_ISSET(io->fd, &loop->writefds)) {
316                         io->cb(loop, io->data, IO_WRITE);
317                 }
318 }
319
320 void event_loop_start(event_loop_t *loop) {
321         loop->running = true;
322 }
323
324 void event_loop_stop(event_loop_t *loop) {
325         loop->running = false;
326 }
327
328 void event_loop_init(event_loop_t *loop) {
329         loop->ios.compare = (splay_compare_t)io_compare;
330         loop->timeouts.compare = (splay_compare_t)timeout_compare;
331         loop->signals.compare = (splay_compare_t)signal_compare;
332         loop->pipefd[0] = -1;
333         loop->pipefd[1] = -1;
334         gettimeofday(&loop->now, NULL);
335 }
336
337 void event_loop_exit(event_loop_t *loop) {
338         for splay_each(io_t, io, &loop->ios) {
339                 splay_unlink_node(&loop->ios, node);
340         }
341
342         for splay_each(timeout_t, timeout, &loop->timeouts) {
343                 splay_unlink_node(&loop->timeouts, node);
344         }
345
346         for splay_each(signal_t, signal, &loop->signals) {
347                 splay_unlink_node(&loop->signals, node);
348         }
349 }