+ 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);
+ }
+
+ if(loop->deletion) {
+ break;
+ }
+
+ if(FD_ISSET(io->fd, &readable) && io->cb) {
+ io->cb(loop, io->data, IO_READ);
+ }
+
+ if(loop->deletion) {
+ break;
+ }