2 event.c -- I/O, timeout and signal event handling
3 Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
27 #include "splay_tree.h"
32 #if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
33 #define EVENT_CLOCK CLOCK_MONOTONIC_RAW
35 #define EVENT_CLOCK CLOCK_MONOTONIC
39 static void timespec_add(const struct timespec *a, const struct timespec *b, struct timespec *r) {
40 r->tv_sec = a->tv_sec + b->tv_sec;
41 r->tv_nsec = a->tv_nsec + b->tv_nsec;
43 if(r->tv_nsec > 1000000000) {
44 r->tv_sec++, r->tv_nsec -= 1000000000;
48 static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) {
49 r->tv_sec = a->tv_sec - b->tv_sec;
50 r->tv_nsec = a->tv_nsec - b->tv_nsec;
53 r->tv_sec--, r->tv_nsec += 1000000000;
57 static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
58 if(a->tv_sec == b->tv_sec) {
59 return a->tv_nsec < b->tv_nsec;
61 return a->tv_sec < b->tv_sec;
65 static void timespec_clear(struct timespec *a) {
69 static int io_compare(const io_t *a, const io_t *b) {
73 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
74 if(a->tv.tv_sec < b->tv.tv_sec) {
76 } else if(a->tv.tv_sec > b->tv.tv_sec) {
78 } else if(a->tv.tv_nsec < b->tv.tv_nsec) {
80 } else if(a->tv.tv_nsec > b->tv.tv_nsec) {
91 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
99 io_set(loop, io, flags);
101 splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
106 void io_set(event_loop_t *loop, io_t *io, int flags) {
111 if(flags & IO_READ) {
112 FD_SET(io->fd, &loop->readfds);
114 FD_CLR(io->fd, &loop->readfds);
117 if(flags & IO_WRITE) {
118 FD_SET(io->fd, &loop->writefds);
120 FD_CLR(io->fd, &loop->writefds);
124 void io_del(event_loop_t *loop, io_t *io) {
127 loop->deletion = true;
131 splay_unlink_node(&loop->ios, &io->node);
135 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timespec *tv) {
137 timeout->data = data;
139 timeout_set(loop, timeout, tv);
142 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timespec *tv) {
145 if(timeout->node.data) {
146 splay_unlink_node(&loop->timeouts, &timeout->node);
148 timeout->node.data = timeout;
151 if(!loop->now.tv_sec) {
152 clock_gettime(EVENT_CLOCK, &loop->now);
155 timespec_add(&loop->now, tv, &timeout->tv);
157 if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
161 loop->deletion = true;
164 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
165 if(timeout->node.data) {
166 splay_unlink_node(&loop->timeouts, &timeout->node);
167 timeout->node.data = NULL;
170 timespec_clear(&timeout->tv);
173 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
178 if(timeout->node.data) {
179 timeout_disable(loop, timeout);
183 loop->deletion = true;
186 static int signal_compare(const signal_t *a, const signal_t *b) {
187 return (int)a->signum - (int)b->signum;
190 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
193 unsigned char signum;
195 if(read(loop->pipefd[0], &signum, 1) != 1) {
199 signal_t *sig = splay_search(&loop->signals, &(signal_t) {
204 #ifdef HAVE_STDATOMIC_H
205 atomic_flag_clear(&sig->set);
207 sig->cb(loop, sig->data);
211 static void pipe_init(event_loop_t *loop) {
212 int result = pipe(loop->pipefd);
217 fcntl(loop->pipefd[0], F_SETFL, O_NONBLOCK);
218 fcntl(loop->pipefd[1], F_SETFL, O_NONBLOCK);
220 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
224 static void pipe_exit(event_loop_t *loop) {
225 io_del(loop, &loop->signalio);
227 close(loop->pipefd[0]);
228 close(loop->pipefd[1]);
230 loop->pipefd[0] = -1;
231 loop->pipefd[1] = -1;
234 void signal_trigger(event_loop_t *loop, signal_t *sig) {
235 #ifdef HAVE_STDATOMIC_H
237 if(atomic_flag_test_and_set(&sig->set)) {
243 uint8_t signum = sig->signum;
244 write(loop->pipefd[1], &signum, 1);
248 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
253 sig->signum = signum;
254 sig->node.data = sig;
256 #ifdef HAVE_STDATOMIC_H
257 atomic_flag_clear(&sig->set);
260 if(loop->pipefd[0] == -1) {
264 if(!splay_insert_node(&loop->signals, &sig->node)) {
269 void signal_del(event_loop_t *loop, signal_t *sig) {
272 loop->deletion = true;
274 splay_unlink_node(&loop->signals, &sig->node);
277 if(!loop->signals.count && loop->pipefd[0] != -1) {
282 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
284 loop->idle_data = data;
287 static void check_bad_fds(event_loop_t *loop, meshlink_handle_t *mesh) {
288 // Just call all registered callbacks and have them check their fds
291 loop->deletion = false;
293 for splay_each(io_t, io, &loop->ios) {
294 if(io->flags & IO_WRITE) {
295 io->cb(loop, io->data, IO_WRITE);
302 if(io->flags & IO_READ) {
303 io->cb(loop, io->data, IO_READ);
310 } while(loop->deletion);
312 // Rebuild the fdsets
316 memcpy(&old_readfds, &loop->readfds, sizeof(old_readfds));
317 memcpy(&old_writefds, &loop->writefds, sizeof(old_writefds));
319 memset(&loop->readfds, 0, sizeof(loop->readfds));
320 memset(&loop->writefds, 0, sizeof(loop->writefds));
322 for splay_each(io_t, io, &loop->ios) {
323 if(io->flags & IO_READ) {
324 FD_SET(io->fd, &loop->readfds);
325 io->cb(loop, io->data, IO_READ);
328 if(io->flags & IO_WRITE) {
329 FD_SET(io->fd, &loop->writefds);
330 io->cb(loop, io->data, IO_WRITE);
334 if(memcmp(&old_readfds, &loop->readfds, sizeof(old_readfds))) {
335 logger(mesh, MESHLINK_WARNING, "Incorrect readfds fixed");
338 if(memcmp(&old_writefds, &loop->writefds, sizeof(old_writefds))) {
339 logger(mesh, MESHLINK_WARNING, "Incorrect writefds fixed");
343 bool event_loop_run(event_loop_t *loop, meshlink_handle_t *mesh) {
350 while(loop->running) {
351 clock_gettime(EVENT_CLOCK, &loop->now);
352 struct timespec it, ts = {3600, 0};
354 while(loop->timeouts.head) {
355 timeout_t *timeout = loop->timeouts.head->data;
357 if(timespec_lt(&timeout->tv, &loop->now)) {
358 timeout_disable(loop, timeout);
359 timeout->cb(loop, timeout->data);
361 timespec_sub(&timeout->tv, &loop->now, &ts);
367 it = loop->idle_cb(loop, loop->idle_data);
369 if(it.tv_sec >= 0 && timespec_lt(&it, &ts)) {
374 memcpy(&readable, &loop->readfds, sizeof(readable));
375 memcpy(&writable, &loop->writefds, sizeof(writable));
380 io_t *last = loop->ios.tail->data;
384 // release mesh mutex during select
385 pthread_mutex_unlock(&mesh->mutex);
388 int n = pselect(fds, &readable, &writable, NULL, &ts, NULL);
390 struct timeval tv = {ts.tv_sec, ts.tv_nsec / 1000};
391 int n = select(fds, &readable, &writable, NULL, (struct timeval *)&tv);
394 if(pthread_mutex_lock(&mesh->mutex) != 0) {
398 clock_gettime(EVENT_CLOCK, &loop->now);
401 if(sockwouldblock(errno)) {
407 logger(mesh, MESHLINK_ERROR, "Unrecoverable error from select(): %s", strerror(errno));
411 logger(mesh, MESHLINK_WARNING, "Error from select(), checking for bad fds: %s", strerror(errno));
412 check_bad_fds(loop, mesh);
423 // Normally, splay_each allows the current node to be deleted. However,
424 // it can be that one io callback triggers the deletion of another io,
425 // so we have to detect this and break the loop.
427 loop->deletion = false;
429 for splay_each(io_t, io, &loop->ios) {
430 if(FD_ISSET(io->fd, &writable) && io->cb) {
431 io->cb(loop, io->data, IO_WRITE);
438 if(FD_ISSET(io->fd, &readable) && io->cb) {
439 io->cb(loop, io->data, IO_READ);
451 void event_loop_start(event_loop_t *loop) {
452 loop->running = true;
455 void event_loop_stop(event_loop_t *loop) {
456 loop->running = false;
459 void event_loop_init(event_loop_t *loop) {
460 loop->ios.compare = (splay_compare_t)io_compare;
461 loop->timeouts.compare = (splay_compare_t)timeout_compare;
462 loop->signals.compare = (splay_compare_t)signal_compare;
463 loop->pipefd[0] = -1;
464 loop->pipefd[1] = -1;
465 clock_gettime(EVENT_CLOCK, &loop->now);
468 void event_loop_exit(event_loop_t *loop) {
469 assert(!loop->ios.count);
470 assert(!loop->timeouts.count);
471 assert(!loop->signals.count);
473 for splay_each(io_t, io, &loop->ios) {
474 splay_unlink_node(&loop->ios, splay_node);
477 for splay_each(timeout_t, timeout, &loop->timeouts) {
478 splay_unlink_node(&loop->timeouts, splay_node);
481 for splay_each(signal_t, signal, &loop->signals) {
482 splay_unlink_node(&loop->signals, splay_node);