2 event.c -- I/O, timeout and signal event handling
3 Copyright (C) 2012 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 static fd_set readfds;
29 static fd_set writefds;
30 static volatile bool running;
32 static int io_compare(const io_t *a, const io_t *b) {
36 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
38 timersub(&a->tv, &b->tv, &diff);
54 static int signal_compare(const signal_t *a, const signal_t *b) {
55 return a->signum - b->signum;
58 static splay_tree_t io_tree = {.compare = (splay_compare_t)io_compare};
59 static splay_tree_t timeout_tree = {.compare = (splay_compare_t)timeout_compare};
60 static splay_tree_t signal_tree = {.compare = (splay_compare_t)signal_compare};
62 void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags) {
73 if(!splay_insert_node(&io_tree, &io->node))
77 void io_set(io_t *io, int flags) {
81 FD_SET(io->fd, &readfds);
83 FD_CLR(io->fd, &readfds);
86 FD_SET(io->fd, &writefds);
88 FD_CLR(io->fd, &writefds);
91 void io_del(io_t *io) {
97 splay_unlink_node(&io_tree, &io->node);
101 void timeout_add(timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
103 timeout->data = data;
104 timeout->node.data = timeout;
106 timeout_set(timeout, tv);
109 void timeout_set(timeout_t *timeout, struct timeval *tv) {
110 if(timerisset(&timeout->tv))
111 splay_unlink_node(&timeout_tree, &timeout->node);
114 gettimeofday(&now, NULL);
116 timeradd(&now, tv, &timeout->tv);
118 if(!splay_insert_node(&timeout_tree, &timeout->node))
122 void timeout_del(timeout_t *timeout) {
126 splay_unlink_node(&timeout_tree, &timeout->node);
128 timeout->tv = (struct timeval){0, 0};
132 static io_t signalio;
133 static int pipefd[2] = {-1, -1};
135 static void signal_handler(int signum) {
136 unsigned char num = signum;
137 write(pipefd[1], &num, 1);
140 static void signalio_handler(void *data, int flags) {
141 unsigned char signum;
142 if(read(pipefd[0], &signum, 1) != 1)
145 signal_t *sig = splay_search(&signal_tree, &((signal_t){.signum = signum}));
150 static void pipe_init(void) {
152 io_add(&signalio, signalio_handler, NULL, pipefd[0], IO_READ);
155 void signal_add(signal_t *sig, signal_cb_t cb, void *data, int signum) {
161 sig->signum = signum;
162 sig->node.data = sig;
167 signal(sig->signum, signal_handler);
169 if(!splay_insert_node(&signal_tree, &sig->node))
173 void signal_del(signal_t *sig) {
177 signal(sig->signum, SIG_DFL);
179 splay_unlink_node(&signal_tree, &sig->node);
184 bool event_loop(void) {
191 gettimeofday(&now, NULL);
192 struct timeval diff, *tv = NULL;
194 while(timeout_tree.head) {
195 timeout_t *timeout = timeout_tree.head->data;
196 timersub(&timeout->tv, &now, &diff);
198 if(diff.tv_sec < 0) {
199 timeout->cb(timeout->data);
200 if(timercmp(&timeout->tv, &now, <))
201 timeout_del(timeout);
208 memcpy(&readable, &readfds, sizeof readable);
209 memcpy(&writable, &writefds, sizeof writable);
214 io_t *last = io_tree.tail->data;
219 LeaveCriticalSection(&mutex);
221 int n = select(fds, &readable, &writable, NULL, tv);
223 EnterCriticalSection(&mutex);
227 if(sockwouldblock(errno))
236 for splay_each(io_t, io, &io_tree) {
237 if(FD_ISSET(io->fd, &writable))
238 io->cb(io->data, IO_WRITE);
239 else if(FD_ISSET(io->fd, &readable))
240 io->cb(io->data, IO_READ);
247 void event_exit(void) {