+ 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) {
+ clock_gettime(EVENT_CLOCK, &loop->now);
+ struct timespec it, ts = {3600, 0};
+
+ while(loop->timeouts.head) {
+ timeout_t *timeout = loop->timeouts.head->data;
+
+ if(timespec_lt(&timeout->tv, &loop->now)) {
+ timeout_disable(loop, timeout);
+ timeout->cb(loop, timeout->data);
+ } else {
+ timespec_sub(&timeout->tv, &loop->now, &ts);
+ break;
+ }
+ }
+
+ 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;
+
+ if(loop->ios.tail) {
+ io_t *last = loop->ios.tail->data;
+ fds = last->fd + 1;
+ }
+
+ // 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)) {
+ continue;
+ } 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;
+ }
+ }
+
+ 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) && io->cb) {
+ io->cb(loop, io->data, IO_WRITE);
+ }
+
+ 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_loop_start(event_loop_t *loop) {
+ loop->running = true;
+}
+
+void event_loop_stop(event_loop_t *loop) {
+ loop->running = false;
+}
+
+void event_loop_init(event_loop_t *loop) {
+ loop->ios.compare = (splay_compare_t)io_compare;
+ loop->timeouts.compare = (splay_compare_t)timeout_compare;
+ loop->signals.compare = (splay_compare_t)signal_compare;
+ loop->pipefd[0] = -1;
+ loop->pipefd[1] = -1;
+ clock_gettime(EVENT_CLOCK, &loop->now);
+}
+
+void event_loop_exit(event_loop_t *loop) {
+ 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);
+ }