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);
39 return diff.tv_sec ?: diff.tv_usec;
42 static int signal_compare(const signal_t *a, const signal_t *b) {
43 return a->signum - b->signum;
46 static splay_tree_t io_tree = {.compare = (splay_compare_t)io_compare};
47 static splay_tree_t timeout_tree = {.compare = (splay_compare_t)timeout_compare};
48 static splay_tree_t signal_tree = {.compare = (splay_compare_t)signal_compare};
50 void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags) {
61 splay_insert_node(&io_tree, &io->node);
64 void io_set(io_t *io, int flags) {
68 FD_SET(io->fd, &readfds);
70 FD_CLR(io->fd, &readfds);
73 FD_SET(io->fd, &writefds);
75 FD_CLR(io->fd, &writefds);
78 void io_del(io_t *io) {
84 splay_unlink_node(&io_tree, &io->node);
88 void timeout_add(timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
94 timeout->node.data = timeout;
96 timeout_set(timeout, tv);
99 void timeout_set(timeout_t *timeout, struct timeval *tv) {
100 if(timeout->tv.tv_sec)
101 splay_unlink_node(&timeout_tree, &timeout->node);
104 gettimeofday(&now, NULL);
106 timeradd(&now, tv, &timeout->tv);
108 splay_insert_node(&timeout_tree, &timeout->node);
111 void timeout_del(timeout_t *timeout) {
115 splay_unlink_node(&timeout_tree, &timeout->node);
120 static io_t signalio;
121 static int pipefd[2] = {-1, -1};
123 static void signal_handler(int signum) {
124 unsigned char num = signum;
125 write(pipefd[1], &num, 1);
128 static void signalio_handler(void *data, int flags) {
129 unsigned char signum;
130 if(read(pipefd[0], &signum, 1) != 1)
133 signal_t *sig = splay_search(&signal_tree, &((signal_t){.signum = signum}));
138 static void pipe_init(void) {
140 io_add(&signalio, signalio_handler, NULL, pipefd[0], IO_READ);
143 void signal_add(signal_t *sig, signal_cb_t cb, void *data, int signum) {
149 sig->signum = signum;
150 sig->node.data = sig;
155 signal(sig->signum, signal_handler);
157 splay_insert_node(&signal_tree, &sig->node);
160 void signal_del(signal_t *sig) {
164 signal(sig->signum, SIG_DFL);
166 splay_unlink_node(&signal_tree, &sig->node);
171 bool event_loop(void) {
178 gettimeofday(&now, NULL);
179 struct timeval diff, *tv = NULL;
181 while(timeout_tree.head) {
182 timeout_t *timeout = timeout_tree.head->data;
183 timersub(&timeout->tv, &now, &diff);
185 if(diff.tv_sec <= 0) {
186 timeout->cb(timeout->data);
187 if(timercmp(&timeout->tv, &now, <))
188 timeout_del(timeout);
195 memcpy(&readable, &readfds, sizeof readable);
196 memcpy(&writable, &writefds, sizeof writable);
201 io_t *last = io_tree.tail->data;
206 LeaveCriticalSection(&mutex);
208 int n = select(fds, &readable, &writable, NULL, tv);
210 EnterCriticalSection(&mutex);
214 if(sockwouldblock(errno))
223 for splay_each(io_t, io, &io_tree) {
224 if(FD_ISSET(io->fd, &writable))
225 io->cb(io->data, IO_WRITE);
226 else if(FD_ISSET(io->fd, &readable))
227 io->cb(io->data, IO_READ);
234 void event_exit(void) {