2 event.c -- I/O, timeout and signal event handling
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
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.
25 #include "splay_tree.h"
29 static int io_compare(const io_t *a, const io_t *b) {
33 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
35 timersub(&a->tv, &b->tv, &diff);
51 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
60 io_set(loop, io, flags);
62 if(!splay_insert_node(&loop->ios, &io->node))
66 void io_set(event_loop_t *loop, io_t *io, int flags) {
70 FD_SET(io->fd, &loop->readfds);
72 FD_CLR(io->fd, &loop->readfds);
75 FD_SET(io->fd, &loop->writefds);
77 FD_CLR(io->fd, &loop->writefds);
80 void io_del(event_loop_t *loop, io_t *io) {
86 splay_unlink_node(&loop->ios, &io->node);
90 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
92 timeout->tv = (struct timeval){0, 0};
96 timeout->node.data = timeout;
98 timeout_set(loop, timeout, tv);
101 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
102 if(timerisset(&timeout->tv))
103 splay_unlink_node(&loop->timeouts, &timeout->node);
105 if(!loop->now.tv_sec)
106 gettimeofday(&loop->now, NULL);
108 timeradd(&loop->now, tv, &timeout->tv);
110 if(!splay_insert_node(&loop->timeouts, &timeout->node))
114 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
118 splay_unlink_node(&loop->timeouts, &timeout->node);
120 timeout->tv = (struct timeval){0, 0};
123 static int signal_compare(const signal_t *a, const signal_t *b) {
124 return (int)a->signum - (int)b->signum;
127 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
128 unsigned char signum;
129 if(read(loop->pipefd[0], &signum, 1) != 1)
132 signal_t *sig = splay_search(&loop->signals, &((signal_t){.signum = signum}));
134 sig->cb(loop, sig->data);
137 static void pipe_init(event_loop_t *loop) {
138 if(!pipe(loop->pipefd))
139 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
142 void signal_trigger(event_loop_t *loop, signal_t *sig) {
144 uint8_t signum = sig->signum;
145 write(loop->pipefd[1], &signum, 1);
150 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
156 sig->signum = signum;
157 sig->node.data = sig;
159 if(loop->pipefd[0] == -1)
162 if(!splay_insert_node(&loop->signals, &sig->node))
166 void signal_del(event_loop_t *loop, signal_t *sig) {
170 splay_unlink_node(&loop->signals, &sig->node);
174 bool event_loop_run(event_loop_t *loop) {
175 loop->running = true;
180 while(loop->running) {
181 gettimeofday(&loop->now, NULL);
182 struct timeval diff, *tv = NULL;
184 while(loop->timeouts.head) {
185 timeout_t *timeout = loop->timeouts.head->data;
186 timersub(&timeout->tv, &loop->now, &diff);
188 if(diff.tv_sec < 0) {
189 timeout->cb(loop, timeout->data);
190 if(timercmp(&timeout->tv, &loop->now, <))
191 timeout_del(loop, timeout);
198 memcpy(&readable, &loop->readfds, sizeof readable);
199 memcpy(&writable, &loop->writefds, sizeof writable);
204 io_t *last = loop->ios.tail->data;
208 int n = select(fds, &readable, &writable, NULL, tv);
211 if(sockwouldblock(errno))
220 for splay_each(io_t, io, &loop->ios) {
221 if(FD_ISSET(io->fd, &writable))
222 io->cb(loop, io->data, IO_WRITE);
223 else if(FD_ISSET(io->fd, &readable))
224 io->cb(loop, io->data, IO_READ);
231 void event_flush_output(event_loop_t *loop) {
232 for splay_each(io_t, io, &loop->ios)
233 if(FD_ISSET(io->fd, &loop->writefds))
234 io->cb(loop, io->data, IO_WRITE);
237 void event_loop_stop(event_loop_t *loop) {
238 loop->running = false;
241 void event_loop_init(event_loop_t *loop) {
242 loop->ios.compare = (splay_compare_t)io_compare;
243 loop->timeouts.compare = (splay_compare_t)timeout_compare;
244 loop->signals.compare = (splay_compare_t)signal_compare;
245 loop->pipefd[0] = -1;
246 loop->pipefd[1] = -1;
247 gettimeofday(&loop->now, NULL);
250 void event_loop_exit(event_loop_t *loop) {
251 for splay_each(io_t, io, &loop->ios)
252 splay_unlink_node(&loop->ios, node);
253 for splay_each(timeout_t, timeout, &loop->timeouts)
254 splay_unlink_node(&loop->timeouts, node);
255 for splay_each(signal_t, signal, &loop->signals)
256 splay_unlink_node(&loop->signals, node);