2 event.c -- I/O, timeout and signal event handling
3 Copyright (C) 2014-2017 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);
45 if(diff.tv_usec < 0) {
49 if(diff.tv_usec > 0) {
64 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
72 io_set(loop, io, flags);
74 splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
79 void io_set(event_loop_t *loop, io_t *io, int flags) {
85 FD_SET(io->fd, &loop->readfds);
87 FD_CLR(io->fd, &loop->readfds);
90 if(flags & IO_WRITE) {
91 FD_SET(io->fd, &loop->writefds);
93 FD_CLR(io->fd, &loop->writefds);
97 void io_del(event_loop_t *loop, io_t *io) {
100 loop->deletion = true;
104 splay_unlink_node(&loop->ios, &io->node);
108 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
110 timeout->data = data;
111 timeout->node.data = timeout;
113 timeout_set(loop, timeout, tv);
116 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
119 if(timerisset(&timeout->tv)) {
120 splay_unlink_node(&loop->timeouts, &timeout->node);
123 if(!loop->now.tv_sec) {
124 gettimeofday(&loop->now, NULL);
127 timeradd(&loop->now, tv, &timeout->tv);
129 if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
133 loop->deletion = true;
136 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
137 splay_unlink_node(&loop->timeouts, &timeout->node);
138 timeout->tv = (struct timeval) {
143 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
148 if(timerisset(&timeout->tv)) {
149 timeout_disable(loop, timeout);
153 loop->deletion = true;
156 static int signal_compare(const signal_t *a, const signal_t *b) {
157 return (int)a->signum - (int)b->signum;
160 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
163 unsigned char signum;
165 if(read(loop->pipefd[0], &signum, 1) != 1) {
169 signal_t *sig = splay_search(&loop->signals, &(signal_t) {
174 sig->cb(loop, sig->data);
178 static void pipe_init(event_loop_t *loop) {
179 int result = pipe(loop->pipefd);
183 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
187 static void pipe_exit(event_loop_t *loop) {
188 io_del(loop, &loop->signalio);
190 close(loop->pipefd[0]);
191 close(loop->pipefd[1]);
193 loop->pipefd[0] = -1;
194 loop->pipefd[1] = -1;
197 void signal_trigger(event_loop_t *loop, signal_t *sig) {
198 uint8_t signum = sig->signum;
199 write(loop->pipefd[1], &signum, 1);
203 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
208 sig->signum = signum;
209 sig->node.data = sig;
211 if(loop->pipefd[0] == -1) {
215 if(!splay_insert_node(&loop->signals, &sig->node)) {
220 void signal_del(event_loop_t *loop, signal_t *sig) {
223 loop->deletion = true;
225 splay_unlink_node(&loop->signals, &sig->node);
228 if(!loop->signals.count && loop->pipefd[0] != -1) {
233 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
235 loop->idle_data = data;
238 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
244 while(loop->running) {
245 gettimeofday(&loop->now, NULL);
246 struct timeval diff, it, *tv = NULL;
248 while(loop->timeouts.head) {
249 timeout_t *timeout = loop->timeouts.head->data;
250 timersub(&timeout->tv, &loop->now, &diff);
252 if(diff.tv_sec < 0) {
253 timeout_disable(loop, timeout);
254 timeout->cb(loop, timeout->data);
262 it = loop->idle_cb(loop, loop->idle_data);
264 if(it.tv_sec >= 0 && (!tv || timercmp(&it, tv, <))) {
269 memcpy(&readable, &loop->readfds, sizeof(readable));
270 memcpy(&writable, &loop->writefds, sizeof(writable));
275 io_t *last = loop->ios.tail->data;
279 // release mesh mutex during select
280 pthread_mutex_unlock(mutex);
282 int n = select(fds, &readable, &writable, NULL, tv);
284 pthread_mutex_lock(mutex);
286 gettimeofday(&loop->now, NULL);
289 if(sockwouldblock(errno)) {
300 // Normally, splay_each allows the current node to be deleted. However,
301 // it can be that one io callback triggers the deletion of another io,
302 // so we have to detect this and break the loop.
304 loop->deletion = false;
306 for splay_each(io_t, io, &loop->ios) {
307 if(FD_ISSET(io->fd, &writable) && io->cb) {
308 io->cb(loop, io->data, IO_WRITE);
315 if(FD_ISSET(io->fd, &readable) && io->cb) {
316 io->cb(loop, io->data, IO_READ);
328 void event_flush_output(event_loop_t *loop) {
329 for splay_each(io_t, io, &loop->ios)
330 if(FD_ISSET(io->fd, &loop->writefds)) {
331 io->cb(loop, io->data, IO_WRITE);
335 void event_loop_start(event_loop_t *loop) {
336 loop->running = true;
339 void event_loop_stop(event_loop_t *loop) {
340 loop->running = false;
343 void event_loop_init(event_loop_t *loop) {
344 loop->ios.compare = (splay_compare_t)io_compare;
345 loop->timeouts.compare = (splay_compare_t)timeout_compare;
346 loop->signals.compare = (splay_compare_t)signal_compare;
347 loop->pipefd[0] = -1;
348 loop->pipefd[1] = -1;
349 gettimeofday(&loop->now, NULL);
352 void event_loop_exit(event_loop_t *loop) {
353 assert(!loop->ios.count);
354 assert(!loop->timeouts.count);
355 assert(!loop->signals.count);
357 for splay_each(io_t, io, &loop->ios) {
358 splay_unlink_node(&loop->ios, node);
361 for splay_each(timeout_t, timeout, &loop->timeouts) {
362 splay_unlink_node(&loop->timeouts, node);
365 for splay_each(signal_t, signal, &loop->signals) {
366 splay_unlink_node(&loop->signals, node);