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 assert(splay_insert_node(&loop->ios, &io->node));
77 void io_set(event_loop_t *loop, io_t *io, int flags) {
83 FD_SET(io->fd, &loop->readfds);
85 FD_CLR(io->fd, &loop->readfds);
88 if(flags & IO_WRITE) {
89 FD_SET(io->fd, &loop->writefds);
91 FD_CLR(io->fd, &loop->writefds);
95 void io_del(event_loop_t *loop, io_t *io) {
98 loop->deletion = true;
102 splay_unlink_node(&loop->ios, &io->node);
106 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
108 timeout->data = data;
109 timeout->node.data = timeout;
111 timeout_set(loop, timeout, tv);
114 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
117 if(timerisset(&timeout->tv)) {
118 splay_unlink_node(&loop->timeouts, &timeout->node);
121 if(!loop->now.tv_sec) {
122 gettimeofday(&loop->now, NULL);
125 timeradd(&loop->now, tv, &timeout->tv);
127 if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
131 loop->deletion = true;
134 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
135 splay_unlink_node(&loop->timeouts, &timeout->node);
136 timeout->tv = (struct timeval) {
141 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
146 if(timerisset(&timeout->tv)) {
147 timeout_disable(loop, timeout);
151 loop->deletion = true;
154 static int signal_compare(const signal_t *a, const signal_t *b) {
155 return (int)a->signum - (int)b->signum;
158 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
161 unsigned char signum;
163 if(read(loop->pipefd[0], &signum, 1) != 1) {
167 signal_t *sig = splay_search(&loop->signals, &(signal_t) {
172 sig->cb(loop, sig->data);
176 static void pipe_init(event_loop_t *loop) {
177 assert(pipe(loop->pipefd) == 0);
178 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
181 static void pipe_exit(event_loop_t *loop) {
182 io_del(loop, &loop->signalio);
184 close(loop->pipefd[0]);
185 close(loop->pipefd[1]);
187 loop->pipefd[0] = -1;
188 loop->pipefd[1] = -1;
191 void signal_trigger(event_loop_t *loop, signal_t *sig) {
192 uint8_t signum = sig->signum;
193 write(loop->pipefd[1], &signum, 1);
197 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
202 sig->signum = signum;
203 sig->node.data = sig;
205 if(loop->pipefd[0] == -1) {
209 if(!splay_insert_node(&loop->signals, &sig->node)) {
214 void signal_del(event_loop_t *loop, signal_t *sig) {
217 loop->deletion = true;
219 splay_unlink_node(&loop->signals, &sig->node);
222 if(!loop->signals.count && loop->pipefd[0] != -1) {
227 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
229 loop->idle_data = data;
232 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
238 while(loop->running) {
239 gettimeofday(&loop->now, NULL);
240 struct timeval diff, it, *tv = NULL;
242 while(loop->timeouts.head) {
243 timeout_t *timeout = loop->timeouts.head->data;
244 timersub(&timeout->tv, &loop->now, &diff);
246 if(diff.tv_sec < 0) {
247 timeout_disable(loop, timeout);
248 timeout->cb(loop, timeout->data);
256 it = loop->idle_cb(loop, loop->idle_data);
258 if(it.tv_sec >= 0 && (!tv || timercmp(&it, tv, <))) {
263 memcpy(&readable, &loop->readfds, sizeof(readable));
264 memcpy(&writable, &loop->writefds, sizeof(writable));
269 io_t *last = loop->ios.tail->data;
273 // release mesh mutex during select
274 pthread_mutex_unlock(mutex);
276 int n = select(fds, &readable, &writable, NULL, tv);
278 pthread_mutex_lock(mutex);
280 gettimeofday(&loop->now, NULL);
283 if(sockwouldblock(errno)) {
294 // Normally, splay_each allows the current node to be deleted. However,
295 // it can be that one io callback triggers the deletion of another io,
296 // so we have to detect this and break the loop.
298 loop->deletion = false;
300 for splay_each(io_t, io, &loop->ios) {
301 if(FD_ISSET(io->fd, &writable) && io->cb) {
302 io->cb(loop, io->data, IO_WRITE);
309 if(FD_ISSET(io->fd, &readable) && io->cb) {
310 io->cb(loop, io->data, IO_READ);
322 void event_flush_output(event_loop_t *loop) {
323 for splay_each(io_t, io, &loop->ios)
324 if(FD_ISSET(io->fd, &loop->writefds)) {
325 io->cb(loop, io->data, IO_WRITE);
329 void event_loop_start(event_loop_t *loop) {
330 loop->running = true;
333 void event_loop_stop(event_loop_t *loop) {
334 loop->running = false;
337 void event_loop_init(event_loop_t *loop) {
338 loop->ios.compare = (splay_compare_t)io_compare;
339 loop->timeouts.compare = (splay_compare_t)timeout_compare;
340 loop->signals.compare = (splay_compare_t)signal_compare;
341 loop->pipefd[0] = -1;
342 loop->pipefd[1] = -1;
343 gettimeofday(&loop->now, NULL);
346 void event_loop_exit(event_loop_t *loop) {
347 assert(!loop->ios.count);
348 assert(!loop->timeouts.count);
349 assert(!loop->signals.count);
351 for splay_each(io_t, io, &loop->ios) {
352 splay_unlink_node(&loop->ios, node);
355 for splay_each(timeout_t, timeout, &loop->timeouts) {
356 splay_unlink_node(&loop->timeouts, node);
359 for splay_each(signal_t, signal, &loop->signals) {
360 splay_unlink_node(&loop->signals, node);