/*
event.c -- I/O, timeout and signal event handling
- Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
+ Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "dropin.h"
#include "event.h"
+#include "logger.h"
+#include "meshlink.h"
#include "net.h"
#include "splay_tree.h"
#include "utils.h"
#include "xalloc.h"
+#ifndef EVENT_CLOCK
+#if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
+#define EVENT_CLOCK CLOCK_MONOTONIC_RAW
+#else
+#define EVENT_CLOCK CLOCK_MONOTONIC
+#endif
+#endif
+
static int io_compare(const io_t *a, const io_t *b) {
return a->fd - b->fd;
}
static int timeout_compare(const timeout_t *a, const timeout_t *b) {
- struct timeval diff;
- timersub(&a->tv, &b->tv, &diff);
- if(diff.tv_sec < 0)
+ if(a->tv.tv_sec < b->tv.tv_sec) {
return -1;
- if(diff.tv_sec > 0)
+ } else if(a->tv.tv_sec > b->tv.tv_sec) {
return 1;
- if(diff.tv_usec < 0)
+ } else if(a->tv.tv_nsec < b->tv.tv_nsec) {
return -1;
- if(diff.tv_usec > 0)
+ } else if(a->tv.tv_nsec > b->tv.tv_nsec) {
return 1;
- if(a < b)
+ } else if(a < b) {
return -1;
- if(a > b)
+ } else if(a > b) {
return 1;
- return 0;
+ } else {
+ return 0;
+ }
}
void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
- if(io->cb)
- return;
+ assert(!io->cb);
io->fd = fd;
io->cb = cb;
io_set(loop, io, flags);
- if(!splay_insert_node(&loop->ios, &io->node))
- abort();
+ splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
+ assert(node);
+ (void)node;
}
void io_set(event_loop_t *loop, io_t *io, int flags) {
+ assert(io->cb);
+
io->flags = flags;
- if(flags & IO_READ)
+ if(flags & IO_READ) {
FD_SET(io->fd, &loop->readfds);
- else
+ } else {
FD_CLR(io->fd, &loop->readfds);
+ }
- if(flags & IO_WRITE)
+ if(flags & IO_WRITE) {
FD_SET(io->fd, &loop->writefds);
- else
+ } else {
FD_CLR(io->fd, &loop->writefds);
+ }
}
void io_del(event_loop_t *loop, io_t *io) {
- if(!io->cb)
- return;
+ assert(io->cb);
+
+ loop->deletion = true;
io_set(loop, io, 0);
io->cb = NULL;
}
-void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
+void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timespec *tv) {
timeout->cb = cb;
timeout->data = data;
- timeout->node.data = timeout;
timeout_set(loop, timeout, tv);
}
-void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
- if(timerisset(&timeout->tv))
+void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timespec *tv) {
+ assert(timeout->cb);
+
+ if(timeout->node.data) {
splay_unlink_node(&loop->timeouts, &timeout->node);
+ } else {
+ timeout->node.data = timeout;
+ }
- if(!loop->now.tv_sec)
- gettimeofday(&loop->now, NULL);
+ if(!loop->now.tv_sec) {
+ clock_gettime(EVENT_CLOCK, &loop->now);
+ }
- timeradd(&loop->now, tv, &timeout->tv);
+ timespec_add(&loop->now, tv, &timeout->tv);
- if(!splay_insert_node(&loop->timeouts, &timeout->node))
+ if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
abort();
+ }
+
+ loop->deletion = true;
+}
+
+static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
+ if(timeout->node.data) {
+ splay_unlink_node(&loop->timeouts, &timeout->node);
+ timeout->node.data = NULL;
+ }
+
+ timespec_clear(&timeout->tv);
}
void timeout_del(event_loop_t *loop, timeout_t *timeout) {
- if(!timeout->cb)
+ if(!timeout->cb) {
return;
+ }
+
+ if(timeout->node.data) {
+ timeout_disable(loop, timeout);
+ }
- splay_unlink_node(&loop->timeouts, &timeout->node);
- timeout->cb = 0;
- timeout->tv = (struct timeval){0, 0};
+ timeout->cb = NULL;
+ loop->deletion = true;
}
static int signal_compare(const signal_t *a, const signal_t *b) {
}
static void signalio_handler(event_loop_t *loop, void *data, int flags) {
+ (void)data;
+ (void)flags;
unsigned char signum;
- if(read(loop->pipefd[0], &signum, 1) != 1)
+
+ if(read(loop->pipefd[0], &signum, 1) != 1) {
return;
+ }
- signal_t *sig = splay_search(&loop->signals, &((signal_t){.signum = signum}));
- if(sig)
+ signal_t *sig = splay_search(&loop->signals, &(signal_t) {
+ .signum = signum
+ });
+
+ if(sig) {
+#ifdef HAVE_STDATOMIC_H
+ atomic_flag_clear(&sig->set);
+#endif
sig->cb(loop, sig->data);
+ }
}
static void pipe_init(event_loop_t *loop) {
- if(!pipe(loop->pipefd))
+ int result = pipe(loop->pipefd);
+ assert(result == 0);
+
+ if(result == 0) {
+#ifdef O_NONBLOCK
+ fcntl(loop->pipefd[0], F_SETFL, O_NONBLOCK);
+ fcntl(loop->pipefd[1], F_SETFL, O_NONBLOCK);
+#endif
io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
+ }
+}
+
+static void pipe_exit(event_loop_t *loop) {
+ io_del(loop, &loop->signalio);
+
+ close(loop->pipefd[0]);
+ close(loop->pipefd[1]);
+
+ loop->pipefd[0] = -1;
+ loop->pipefd[1] = -1;
}
void signal_trigger(event_loop_t *loop, signal_t *sig) {
+#ifdef HAVE_STDATOMIC_H
+
+ if(atomic_flag_test_and_set(&sig->set)) {
+ return;
+ }
+
+#endif
uint8_t signum = sig->signum;
write(loop->pipefd[1], &signum, 1);
return;
-
}
void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
- if(sig->cb)
- return;
+ assert(!sig->cb);
sig->cb = cb;
sig->data = data;
sig->signum = signum;
sig->node.data = sig;
- if(loop->pipefd[0] == -1)
+#ifdef HAVE_STDATOMIC_H
+ atomic_flag_clear(&sig->set);
+#endif
+
+ if(loop->pipefd[0] == -1) {
pipe_init(loop);
+ }
- if(!splay_insert_node(&loop->signals, &sig->node))
+ if(!splay_insert_node(&loop->signals, &sig->node)) {
abort();
+ }
}
void signal_del(event_loop_t *loop, signal_t *sig) {
- if(!sig->cb)
- return;
+ assert(sig->cb);
+
+ loop->deletion = true;
splay_unlink_node(&loop->signals, &sig->node);
sig->cb = NULL;
+
+ if(!loop->signals.count && loop->pipefd[0] != -1) {
+ pipe_exit(loop);
+ }
}
-bool event_loop_run(event_loop_t *loop) {
- loop->running = true;
+void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
+ loop->idle_cb = cb;
+ loop->idle_data = data;
+}
+
+static void check_bad_fds(event_loop_t *loop, meshlink_handle_t *mesh) {
+ // Just call all registered callbacks and have them check their fds
+
+ do {
+ loop->deletion = false;
+
+ for splay_each(io_t, io, &loop->ios) {
+ if(io->flags & IO_WRITE) {
+ io->cb(loop, io->data, IO_WRITE);
+ }
+
+ if(loop->deletion) {
+ break;
+ }
+
+ if(io->flags & IO_READ) {
+ io->cb(loop, io->data, IO_READ);
+ }
+
+ if(loop->deletion) {
+ break;
+ }
+ }
+ } while(loop->deletion);
+
+ // Rebuild the fdsets
+
+ fd_set old_readfds;
+ fd_set old_writefds;
+ memcpy(&old_readfds, &loop->readfds, sizeof(old_readfds));
+ memcpy(&old_writefds, &loop->writefds, sizeof(old_writefds));
+
+ memset(&loop->readfds, 0, sizeof(loop->readfds));
+ memset(&loop->writefds, 0, sizeof(loop->writefds));
+
+ for splay_each(io_t, io, &loop->ios) {
+ if(io->flags & IO_READ) {
+ FD_SET(io->fd, &loop->readfds);
+ io->cb(loop, io->data, IO_READ);
+ }
+
+ if(io->flags & IO_WRITE) {
+ FD_SET(io->fd, &loop->writefds);
+ io->cb(loop, io->data, IO_WRITE);
+ }
+ }
+
+ if(memcmp(&old_readfds, &loop->readfds, sizeof(old_readfds))) {
+ logger(mesh, MESHLINK_WARNING, "Incorrect readfds fixed");
+ }
+
+ if(memcmp(&old_writefds, &loop->writefds, sizeof(old_writefds))) {
+ logger(mesh, MESHLINK_WARNING, "Incorrect writefds fixed");
+ }
+}
+
+bool event_loop_run(event_loop_t *loop, meshlink_handle_t *mesh) {
+ assert(mesh);
fd_set readable;
fd_set writable;
+ int errors = 0;
while(loop->running) {
- gettimeofday(&loop->now, NULL);
- struct timeval diff, *tv = NULL;
+ clock_gettime(EVENT_CLOCK, &loop->now);
+ struct timespec it, ts = {3600, 0};
while(loop->timeouts.head) {
timeout_t *timeout = loop->timeouts.head->data;
- timersub(&timeout->tv, &loop->now, &diff);
- if(diff.tv_sec < 0) {
+ if(timespec_lt(&timeout->tv, &loop->now)) {
+ timeout_disable(loop, timeout);
timeout->cb(loop, timeout->data);
- if(timercmp(&timeout->tv, &loop->now, <))
- timeout_del(loop, timeout);
} else {
- tv = &diff;
+ timespec_sub(&timeout->tv, &loop->now, &ts);
break;
}
}
- memcpy(&readable, &loop->readfds, sizeof readable);
- memcpy(&writable, &loop->writefds, sizeof writable);
+ if(loop->idle_cb) {
+ it = loop->idle_cb(loop, loop->idle_data);
+
+ if(it.tv_sec >= 0 && timespec_lt(&it, &ts)) {
+ ts = it;
+ }
+ }
+
+ memcpy(&readable, &loop->readfds, sizeof(readable));
+ memcpy(&writable, &loop->writefds, sizeof(writable));
int fds = 0;
fds = last->fd + 1;
}
- int n = select(fds, &readable, &writable, NULL, tv);
+ // release mesh mutex during select
+ pthread_mutex_unlock(&mesh->mutex);
+
+#ifdef HAVE_PSELECT
+ int n = pselect(fds, &readable, &writable, NULL, &ts, NULL);
+#else
+ struct timeval tv = {ts.tv_sec, ts.tv_nsec / 1000};
+ int n = select(fds, &readable, &writable, NULL, (struct timeval *)&tv);
+#endif
+
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
+
+ clock_gettime(EVENT_CLOCK, &loop->now);
if(n < 0) {
- if(sockwouldblock(errno))
+ if(sockwouldblock(errno)) {
continue;
- else
- return false;
+ } else {
+ errors++;
+
+ if(errors > 10) {
+ logger(mesh, MESHLINK_ERROR, "Unrecoverable error from select(): %s", strerror(errno));
+ return false;
+ }
+
+ logger(mesh, MESHLINK_WARNING, "Error from select(), checking for bad fds: %s", strerror(errno));
+ check_bad_fds(loop, mesh);
+ continue;
+ }
}
- if(!n)
+ errors = 0;
+
+ if(!n) {
continue;
+ }
+
+ // Normally, splay_each allows the current node to be deleted. However,
+ // it can be that one io callback triggers the deletion of another io,
+ // so we have to detect this and break the loop.
+
+ loop->deletion = false;
for splay_each(io_t, io, &loop->ios) {
- if(FD_ISSET(io->fd, &writable))
+ if(FD_ISSET(io->fd, &writable) && io->cb) {
io->cb(loop, io->data, IO_WRITE);
- else if(FD_ISSET(io->fd, &readable))
+ }
+
+ if(loop->deletion) {
+ break;
+ }
+
+ if(FD_ISSET(io->fd, &readable) && io->cb) {
io->cb(loop, io->data, IO_READ);
+ }
+
+ if(loop->deletion) {
+ break;
+ }
}
}
return true;
}
-void event_flush_output(event_loop_t *loop) {
- for splay_each(io_t, io, &loop->ios)
- if(FD_ISSET(io->fd, &loop->writefds))
- io->cb(loop, io->data, IO_WRITE);
+void event_loop_start(event_loop_t *loop) {
+ loop->running = true;
}
void event_loop_stop(event_loop_t *loop) {
loop->signals.compare = (splay_compare_t)signal_compare;
loop->pipefd[0] = -1;
loop->pipefd[1] = -1;
- gettimeofday(&loop->now, NULL);
+ clock_gettime(EVENT_CLOCK, &loop->now);
}
void event_loop_exit(event_loop_t *loop) {
- for splay_each(io_t, io, &loop->ios)
- splay_free_node(&loop->ios, node);
- for splay_each(timeout_t, timeout, &loop->timeouts)
- splay_free_node(&loop->timeouts, node);
- for splay_each(signal_t, signal, &loop->signals)
- splay_free_node(&loop->signals, node);
+ assert(!loop->ios.count);
+ assert(!loop->timeouts.count);
+ assert(!loop->signals.count);
+
+ for splay_each(io_t, io, &loop->ios) {
+ splay_unlink_node(&loop->ios, splay_node);
+ }
+
+ for splay_each(timeout_t, timeout, &loop->timeouts) {
+ splay_unlink_node(&loop->timeouts, splay_node);
+ }
+
+ for splay_each(signal_t, signal, &loop->signals) {
+ splay_unlink_node(&loop->signals, splay_node);
+ }
}