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