+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)
+ return -1;
+ if(diff.tv_sec > 0)
+ return 1;
+ if(diff.tv_usec < 0)
+ return -1;
+ if(diff.tv_usec > 0)
+ return 1;
+ if(a < b)
+ return -1;
+ if(a > b)
+ return 1;
+ 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;
+
+ io->fd = fd;
+ io->cb = cb;
+ io->data = data;
+ io->node.data = io;
+
+ io_set(loop, io, flags);
+
+ if(!splay_insert_node(&loop->ios, &io->node))
+ abort();
+}
+
+void io_set(event_loop_t *loop, io_t *io, int flags) {
+ io->flags = flags;
+
+ if(flags & IO_READ)
+ FD_SET(io->fd, &loop->readfds);
+ else
+ FD_CLR(io->fd, &loop->readfds);
+
+ if(flags & IO_WRITE)
+ FD_SET(io->fd, &loop->writefds);
+ else
+ FD_CLR(io->fd, &loop->writefds);
+}
+
+void io_del(event_loop_t *loop, io_t *io) {
+ if(!io->cb)
+ return;
+
+ io_set(loop, io, 0);
+
+ splay_unlink_node(&loop->ios, &io->node);
+ io->cb = NULL;
+}
+
+void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *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))
+ splay_unlink_node(&loop->timeouts, &timeout->node);
+
+ if(!loop->now.tv_sec)
+ gettimeofday(&loop->now, NULL);
+
+ timeradd(&loop->now, tv, &timeout->tv);
+
+ if(!splay_insert_node(&loop->timeouts, &timeout->node))
+ abort();
+}
+
+void timeout_del(event_loop_t *loop, timeout_t *timeout) {
+ if(!timeout->cb)
+ return;
+
+ splay_unlink_node(&loop->timeouts, &timeout->node);
+ timeout->cb = 0;
+ timeout->tv = (struct timeval){0, 0};
+}
+
+static int signal_compare(const signal_t *a, const signal_t *b) {
+ return (int)a->signum - (int)b->signum;
+}
+
+static void signalio_handler(event_loop_t *loop, void *data, int flags) {
+ unsigned char signum;
+ if(read(loop->pipefd[0], &signum, 1) != 1)
+ return;
+
+ signal_t *sig = splay_search(&loop->signals, &((signal_t){.signum = signum}));
+ if(sig)
+ sig->cb(loop, sig->data);
+}
+
+static void pipe_init(event_loop_t *loop) {
+ if(!pipe(loop->pipefd))
+ io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
+}
+
+void signal_trigger(event_loop_t *loop, signal_t *sig) {
+
+ 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;
+
+ sig->cb = cb;
+ sig->data = data;
+ sig->signum = signum;
+ sig->node.data = sig;
+
+ if(loop->pipefd[0] == -1)
+ pipe_init(loop);
+
+ if(!splay_insert_node(&loop->signals, &sig->node))
+ abort();
+}
+
+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);
+ }
+ }
+
+ 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_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;
+ gettimeofday(&loop->now, NULL);
+}