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.
27 #include "splay_tree.h"
32 #if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
33 #define EVENT_CLOCK CLOCK_MONOTONIC_RAW
35 #define EVENT_CLOCK CLOCK_MONOTONIC
39 static int io_compare(const io_t *a, const io_t *b) {
43 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
44 if(a->tv.tv_sec < b->tv.tv_sec) {
46 } else if(a->tv.tv_sec > b->tv.tv_sec) {
48 } else if(a->tv.tv_nsec < b->tv.tv_nsec) {
50 } else if(a->tv.tv_nsec > b->tv.tv_nsec) {
61 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
69 io_set(loop, io, flags);
71 splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
76 void io_set(event_loop_t *loop, io_t *io, int flags) {
82 FD_SET(io->fd, &loop->readfds);
84 FD_CLR(io->fd, &loop->readfds);
87 if(flags & IO_WRITE) {
88 FD_SET(io->fd, &loop->writefds);
90 FD_CLR(io->fd, &loop->writefds);
94 void io_del(event_loop_t *loop, io_t *io) {
97 loop->deletion = true;
101 splay_unlink_node(&loop->ios, &io->node);
105 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timespec *tv) {
107 timeout->data = data;
109 timeout_set(loop, timeout, tv);
112 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timespec *tv) {
115 if(timeout->node.data) {
116 splay_unlink_node(&loop->timeouts, &timeout->node);
118 timeout->node.data = timeout;
121 if(!loop->now.tv_sec) {
122 clock_gettime(EVENT_CLOCK, &loop->now);
125 timespec_add(&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 if(timeout->node.data) {
136 splay_unlink_node(&loop->timeouts, &timeout->node);
137 timeout->node.data = NULL;
140 timespec_clear(&timeout->tv);
143 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
148 if(timeout->node.data) {
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 #ifdef HAVE_STDATOMIC_H
175 atomic_flag_clear(&sig->set);
177 sig->cb(loop, sig->data);
181 static void pipe_init(event_loop_t *loop) {
182 int result = pipe(loop->pipefd);
187 fcntl(loop->pipefd[0], F_SETFL, O_NONBLOCK);
188 fcntl(loop->pipefd[1], F_SETFL, O_NONBLOCK);
190 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
194 static void pipe_exit(event_loop_t *loop) {
195 io_del(loop, &loop->signalio);
197 close(loop->pipefd[0]);
198 close(loop->pipefd[1]);
200 loop->pipefd[0] = -1;
201 loop->pipefd[1] = -1;
204 void signal_trigger(event_loop_t *loop, signal_t *sig) {
205 #ifdef HAVE_STDATOMIC_H
207 if(atomic_flag_test_and_set(&sig->set)) {
213 uint8_t signum = sig->signum;
214 write(loop->pipefd[1], &signum, 1);
218 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
223 sig->signum = signum;
224 sig->node.data = sig;
226 #ifdef HAVE_STDATOMIC_H
227 atomic_flag_clear(&sig->set);
230 if(loop->pipefd[0] == -1) {
234 if(!splay_insert_node(&loop->signals, &sig->node)) {
239 void signal_del(event_loop_t *loop, signal_t *sig) {
242 loop->deletion = true;
244 splay_unlink_node(&loop->signals, &sig->node);
247 if(!loop->signals.count && loop->pipefd[0] != -1) {
252 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
254 loop->idle_data = data;
257 static void check_bad_fds(event_loop_t *loop, meshlink_handle_t *mesh) {
258 // Just call all registered callbacks and have them check their fds
261 loop->deletion = false;
263 for splay_each(io_t, io, &loop->ios) {
264 if(io->flags & IO_WRITE) {
265 io->cb(loop, io->data, IO_WRITE);
272 if(io->flags & IO_READ) {
273 io->cb(loop, io->data, IO_READ);
280 } while(loop->deletion);
282 // Rebuild the fdsets
286 memcpy(&old_readfds, &loop->readfds, sizeof(old_readfds));
287 memcpy(&old_writefds, &loop->writefds, sizeof(old_writefds));
289 memset(&loop->readfds, 0, sizeof(loop->readfds));
290 memset(&loop->writefds, 0, sizeof(loop->writefds));
292 for splay_each(io_t, io, &loop->ios) {
293 if(io->flags & IO_READ) {
294 FD_SET(io->fd, &loop->readfds);
295 io->cb(loop, io->data, IO_READ);
298 if(io->flags & IO_WRITE) {
299 FD_SET(io->fd, &loop->writefds);
300 io->cb(loop, io->data, IO_WRITE);
304 if(memcmp(&old_readfds, &loop->readfds, sizeof(old_readfds))) {
305 logger(mesh, MESHLINK_WARNING, "Incorrect readfds fixed");
308 if(memcmp(&old_writefds, &loop->writefds, sizeof(old_writefds))) {
309 logger(mesh, MESHLINK_WARNING, "Incorrect writefds fixed");
313 bool event_loop_run(event_loop_t *loop, meshlink_handle_t *mesh) {
320 while(loop->running) {
321 clock_gettime(EVENT_CLOCK, &loop->now);
322 struct timespec it, ts = {3600, 0};
324 while(loop->timeouts.head) {
325 timeout_t *timeout = loop->timeouts.head->data;
327 if(timespec_lt(&timeout->tv, &loop->now)) {
328 timeout_disable(loop, timeout);
329 timeout->cb(loop, timeout->data);
331 timespec_sub(&timeout->tv, &loop->now, &ts);
337 it = loop->idle_cb(loop, loop->idle_data);
339 if(it.tv_sec >= 0 && timespec_lt(&it, &ts)) {
344 memcpy(&readable, &loop->readfds, sizeof(readable));
345 memcpy(&writable, &loop->writefds, sizeof(writable));
350 io_t *last = loop->ios.tail->data;
354 // release mesh mutex during select
355 pthread_mutex_unlock(&mesh->mutex);
358 int n = pselect(fds, &readable, &writable, NULL, &ts, NULL);
360 struct timeval tv = {ts.tv_sec, ts.tv_nsec / 1000};
361 int n = select(fds, &readable, &writable, NULL, (struct timeval *)&tv);
364 if(pthread_mutex_lock(&mesh->mutex) != 0) {
368 clock_gettime(EVENT_CLOCK, &loop->now);
371 if(sockwouldblock(errno)) {
377 logger(mesh, MESHLINK_ERROR, "Unrecoverable error from select(): %s", strerror(errno));
381 logger(mesh, MESHLINK_WARNING, "Error from select(), checking for bad fds: %s", strerror(errno));
382 check_bad_fds(loop, mesh);
393 // Normally, splay_each allows the current node to be deleted. However,
394 // it can be that one io callback triggers the deletion of another io,
395 // so we have to detect this and break the loop.
397 loop->deletion = false;
399 for splay_each(io_t, io, &loop->ios) {
400 if(FD_ISSET(io->fd, &writable) && io->cb) {
401 io->cb(loop, io->data, IO_WRITE);
408 if(FD_ISSET(io->fd, &readable) && io->cb) {
409 io->cb(loop, io->data, IO_READ);
421 void event_loop_start(event_loop_t *loop) {
422 loop->running = true;
425 void event_loop_stop(event_loop_t *loop) {
426 loop->running = false;
429 void event_loop_init(event_loop_t *loop) {
430 loop->ios.compare = (splay_compare_t)io_compare;
431 loop->timeouts.compare = (splay_compare_t)timeout_compare;
432 loop->signals.compare = (splay_compare_t)signal_compare;
433 loop->pipefd[0] = -1;
434 loop->pipefd[1] = -1;
435 clock_gettime(EVENT_CLOCK, &loop->now);
438 void event_loop_exit(event_loop_t *loop) {
439 assert(!loop->ios.count);
440 assert(!loop->timeouts.count);
441 assert(!loop->signals.count);
443 for splay_each(io_t, io, &loop->ios) {
444 splay_unlink_node(&loop->ios, splay_node);
447 for splay_each(timeout_t, timeout, &loop->timeouts) {
448 splay_unlink_node(&loop->timeouts, splay_node);
451 for splay_each(signal_t, signal, &loop->signals) {
452 splay_unlink_node(&loop->signals, splay_node);