-static void pipe_init(void) {
- if(!pipe(pipefd))
- io_add(&signalio, signalio_handler, NULL, pipefd[0], IO_READ);
-}
-
-void signal_add(signal_t *sig, signal_cb_t cb, void *data, int signum) {
- if(sig->cb)
- return;
-
- sig->cb = cb;
- sig->data = data;
- sig->signum = signum;
- sig->node.data = sig;
-
- if(pipefd[0] == -1)
- pipe_init();
-
- signal(sig->signum, signal_handler);
-
- splay_insert_node(&signal_tree, &sig->node);
-}
-
-void signal_del(signal_t *sig) {
- if(!sig->cb)
- return;
-
- signal(sig->signum, SIG_DFL);
-
- splay_unlink_node(&signal_tree, &sig->node);
- sig->cb = NULL;
-}
-#endif
-
-bool event_loop(void) {
- running = true;
-
- fd_set readable;
- fd_set writable;
-
- while(running) {
- gettimeofday(&now, NULL);
- struct timeval diff, *tv = NULL;
-
- while(timeout_tree.head) {
- timeout_t *timeout = timeout_tree.head->data;
- timersub(&timeout->tv, &now, &diff);
-
- if(diff.tv_sec < 0) {
- timeout->cb(timeout->data);
- if(timercmp(&timeout->tv, &now, <))
- timeout_del(timeout);
- } else {
- tv = &diff;
- break;