+bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
+ assert(mutex);
+
+ fd_set readable;
+ fd_set writable;
+
+ 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(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
+
+ pthread_mutex_lock(mutex);
+
+ clock_gettime(EVENT_CLOCK, &loop->now);
+
+ if(n < 0) {
+ if(sockwouldblock(errno)) {
+ continue;
+ } else {
+ return false;
+ }
+ }
+
+ 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);
+ }