+void signal_del(event_loop_t *loop, signal_t *sig) {
+ if(!sig->cb)
+ return;
+
+ splay_unlink_node(&loop->signals, &sig->node);
+ sig->cb = NULL;
+}
+
+bool event_loop_run(event_loop_t *loop) {
+ loop->running = true;
+
+ fd_set readable;
+ fd_set writable;
+
+ while(loop->running) {
+ gettimeofday(&loop->now, NULL);
+ struct timeval diff, *tv = NULL;
+
+ while(loop->timeouts.head) {
+ timeout_t *timeout = loop->timeouts.head->data;
+ timersub(&timeout->tv, &loop->now, &diff);
+
+ if(diff.tv_sec < 0) {
+ timeout->cb(loop, timeout->data);
+ if(timercmp(&timeout->tv, &loop->now, <))
+ timeout_del(loop, timeout);
+ } else {
+ tv = &diff;
+ break;
+ }
+ }
+
+ 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;
+ }
+
+ int n = select(fds, &readable, &writable, NULL, tv);
+
+ if(n < 0) {
+ if(sockwouldblock(errno))
+ continue;
+ else
+ return false;
+ }
+
+ if(!n)
+ continue;
+
+ for splay_each(io_t, io, &loop->ios) {
+ if(FD_ISSET(io->fd, &writable))
+ io->cb(loop, io->data, IO_WRITE);
+ else if(FD_ISSET(io->fd, &readable))
+ io->cb(loop, io->data, IO_READ);