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"
30 #if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
31 #define EVENT_CLOCK CLOCK_MONOTONIC_RAW
33 #define EVENT_CLOCK CLOCK_MONOTONIC
37 static void timespec_add(const struct timespec *a, const struct timespec *b, struct timespec *r) {
38 r->tv_sec = a->tv_sec + b->tv_sec;
39 r->tv_nsec = a->tv_nsec + b->tv_nsec;
41 if(r->tv_nsec > 1000000000) {
42 r->tv_sec++, r->tv_nsec -= 1000000000;
46 static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) {
47 r->tv_sec = a->tv_sec - b->tv_sec;
48 r->tv_nsec = a->tv_nsec - b->tv_nsec;
51 r->tv_sec--, r->tv_nsec += 1000000000;
55 static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
56 if(a->tv_sec == b->tv_sec) {
57 return a->tv_nsec < b->tv_nsec;
59 return a->tv_sec < b->tv_sec;
63 static void timespec_clear(struct timespec *a) {
67 static int io_compare(const io_t *a, const io_t *b) {
71 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
72 if(a->tv.tv_sec < b->tv.tv_sec) {
74 } else if(a->tv.tv_sec > b->tv.tv_sec) {
76 } else if(a->tv.tv_nsec < b->tv.tv_nsec) {
78 } else if(a->tv.tv_nsec > b->tv.tv_nsec) {
89 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
97 io_set(loop, io, flags);
99 splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
104 void io_set(event_loop_t *loop, io_t *io, int flags) {
109 if(flags & IO_READ) {
110 FD_SET(io->fd, &loop->readfds);
112 FD_CLR(io->fd, &loop->readfds);
115 if(flags & IO_WRITE) {
116 FD_SET(io->fd, &loop->writefds);
118 FD_CLR(io->fd, &loop->writefds);
122 void io_del(event_loop_t *loop, io_t *io) {
125 loop->deletion = true;
129 splay_unlink_node(&loop->ios, &io->node);
133 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timespec *tv) {
135 timeout->data = data;
137 timeout_set(loop, timeout, tv);
140 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timespec *tv) {
143 if(timeout->node.data) {
144 splay_unlink_node(&loop->timeouts, &timeout->node);
146 timeout->node.data = timeout;
149 if(!loop->now.tv_sec) {
150 clock_gettime(EVENT_CLOCK, &loop->now);
153 timespec_add(&loop->now, tv, &timeout->tv);
155 if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
159 loop->deletion = true;
162 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
163 if(timeout->node.data) {
164 splay_unlink_node(&loop->timeouts, &timeout->node);
165 timeout->node.data = NULL;
168 timespec_clear(&timeout->tv);
171 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
176 if(timeout->node.data) {
177 timeout_disable(loop, timeout);
181 loop->deletion = true;
184 static int signal_compare(const signal_t *a, const signal_t *b) {
185 return (int)a->signum - (int)b->signum;
188 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
191 unsigned char signum;
193 if(read(loop->pipefd[0], &signum, 1) != 1) {
197 signal_t *sig = splay_search(&loop->signals, &(signal_t) {
202 #ifdef HAVE_STDATOMIC_H
203 atomic_flag_clear(&sig->set);
205 sig->cb(loop, sig->data);
209 static void pipe_init(event_loop_t *loop) {
210 int result = pipe(loop->pipefd);
214 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
218 static void pipe_exit(event_loop_t *loop) {
219 io_del(loop, &loop->signalio);
221 close(loop->pipefd[0]);
222 close(loop->pipefd[1]);
224 loop->pipefd[0] = -1;
225 loop->pipefd[1] = -1;
228 void signal_trigger(event_loop_t *loop, signal_t *sig) {
229 #ifdef HAVE_STDATOMIC_H
231 if(atomic_flag_test_and_set(&sig->set)) {
237 uint8_t signum = sig->signum;
238 write(loop->pipefd[1], &signum, 1);
242 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
247 sig->signum = signum;
248 sig->node.data = sig;
250 #ifdef HAVE_STDATOMIC_H
251 atomic_flag_clear(&sig->set);
254 if(loop->pipefd[0] == -1) {
258 if(!splay_insert_node(&loop->signals, &sig->node)) {
263 void signal_del(event_loop_t *loop, signal_t *sig) {
266 loop->deletion = true;
268 splay_unlink_node(&loop->signals, &sig->node);
271 if(!loop->signals.count && loop->pipefd[0] != -1) {
276 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
278 loop->idle_data = data;
281 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
287 while(loop->running) {
288 clock_gettime(EVENT_CLOCK, &loop->now);
289 struct timespec it, ts = {3600, 0};
291 while(loop->timeouts.head) {
292 timeout_t *timeout = loop->timeouts.head->data;
294 if(timespec_lt(&timeout->tv, &loop->now)) {
295 timeout_disable(loop, timeout);
296 timeout->cb(loop, timeout->data);
298 timespec_sub(&timeout->tv, &loop->now, &ts);
304 it = loop->idle_cb(loop, loop->idle_data);
306 if(it.tv_sec >= 0 && timespec_lt(&it, &ts)) {
311 memcpy(&readable, &loop->readfds, sizeof(readable));
312 memcpy(&writable, &loop->writefds, sizeof(writable));
317 io_t *last = loop->ios.tail->data;
321 // release mesh mutex during select
322 pthread_mutex_unlock(mutex);
325 int n = pselect(fds, &readable, &writable, NULL, &ts, NULL);
327 struct timeval tv = {ts.tv_sec, ts.tv_nsec / 1000};
328 int n = select(fds, &readable, &writable, NULL, (struct timeval *)&tv);
331 if(pthread_mutex_lock(mutex) != 0) {
335 clock_gettime(EVENT_CLOCK, &loop->now);
338 if(sockwouldblock(errno)) {
349 // Normally, splay_each allows the current node to be deleted. However,
350 // it can be that one io callback triggers the deletion of another io,
351 // so we have to detect this and break the loop.
353 loop->deletion = false;
355 for splay_each(io_t, io, &loop->ios) {
356 if(FD_ISSET(io->fd, &writable) && io->cb) {
357 io->cb(loop, io->data, IO_WRITE);
364 if(FD_ISSET(io->fd, &readable) && io->cb) {
365 io->cb(loop, io->data, IO_READ);
377 void event_loop_start(event_loop_t *loop) {
378 loop->running = true;
381 void event_loop_stop(event_loop_t *loop) {
382 loop->running = false;
385 void event_loop_init(event_loop_t *loop) {
386 loop->ios.compare = (splay_compare_t)io_compare;
387 loop->timeouts.compare = (splay_compare_t)timeout_compare;
388 loop->signals.compare = (splay_compare_t)signal_compare;
389 loop->pipefd[0] = -1;
390 loop->pipefd[1] = -1;
391 clock_gettime(EVENT_CLOCK, &loop->now);
394 void event_loop_exit(event_loop_t *loop) {
395 assert(!loop->ios.count);
396 assert(!loop->timeouts.count);
397 assert(!loop->signals.count);
399 for splay_each(io_t, io, &loop->ios) {
400 splay_unlink_node(&loop->ios, splay_node);
403 for splay_each(timeout_t, timeout, &loop->timeouts) {
404 splay_unlink_node(&loop->timeouts, splay_node);
407 for splay_each(signal_t, signal, &loop->signals) {
408 splay_unlink_node(&loop->signals, splay_node);