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) {
203 sig->cb(loop, sig->data);
207 static void pipe_init(event_loop_t *loop) {
208 int result = pipe(loop->pipefd);
212 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
216 static void pipe_exit(event_loop_t *loop) {
217 io_del(loop, &loop->signalio);
219 close(loop->pipefd[0]);
220 close(loop->pipefd[1]);
222 loop->pipefd[0] = -1;
223 loop->pipefd[1] = -1;
226 void signal_trigger(event_loop_t *loop, signal_t *sig) {
231 uint8_t signum = sig->signum;
233 write(loop->pipefd[1], &signum, 1);
237 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
242 sig->signum = signum;
244 sig->node.data = sig;
246 if(loop->pipefd[0] == -1) {
250 if(!splay_insert_node(&loop->signals, &sig->node)) {
255 void signal_del(event_loop_t *loop, signal_t *sig) {
258 loop->deletion = true;
260 splay_unlink_node(&loop->signals, &sig->node);
263 if(!loop->signals.count && loop->pipefd[0] != -1) {
268 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
270 loop->idle_data = data;
273 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
279 while(loop->running) {
280 clock_gettime(EVENT_CLOCK, &loop->now);
281 struct timespec it, ts = {3600, 0};
283 while(loop->timeouts.head) {
284 timeout_t *timeout = loop->timeouts.head->data;
286 if(timespec_lt(&timeout->tv, &loop->now)) {
287 timeout_disable(loop, timeout);
288 timeout->cb(loop, timeout->data);
290 timespec_sub(&timeout->tv, &loop->now, &ts);
296 it = loop->idle_cb(loop, loop->idle_data);
298 if(it.tv_sec >= 0 && timespec_lt(&it, &ts)) {
303 memcpy(&readable, &loop->readfds, sizeof(readable));
304 memcpy(&writable, &loop->writefds, sizeof(writable));
309 io_t *last = loop->ios.tail->data;
313 // release mesh mutex during select
314 pthread_mutex_unlock(mutex);
317 int n = pselect(fds, &readable, &writable, NULL, &ts, NULL);
319 struct timeval tv = {ts.tv_sec, ts.tv_nsec / 1000};
320 int n = select(fds, &readable, &writable, NULL, (struct timeval *)&tv);
323 pthread_mutex_lock(mutex);
325 clock_gettime(EVENT_CLOCK, &loop->now);
328 if(sockwouldblock(errno)) {
339 // Normally, splay_each allows the current node to be deleted. However,
340 // it can be that one io callback triggers the deletion of another io,
341 // so we have to detect this and break the loop.
343 loop->deletion = false;
345 for splay_each(io_t, io, &loop->ios) {
346 if(FD_ISSET(io->fd, &writable) && io->cb) {
347 io->cb(loop, io->data, IO_WRITE);
354 if(FD_ISSET(io->fd, &readable) && io->cb) {
355 io->cb(loop, io->data, IO_READ);
367 void event_loop_start(event_loop_t *loop) {
368 loop->running = true;
371 void event_loop_stop(event_loop_t *loop) {
372 loop->running = false;
375 void event_loop_init(event_loop_t *loop) {
376 loop->ios.compare = (splay_compare_t)io_compare;
377 loop->timeouts.compare = (splay_compare_t)timeout_compare;
378 loop->signals.compare = (splay_compare_t)signal_compare;
379 loop->pipefd[0] = -1;
380 loop->pipefd[1] = -1;
381 clock_gettime(EVENT_CLOCK, &loop->now);
384 void event_loop_exit(event_loop_t *loop) {
385 assert(!loop->ios.count);
386 assert(!loop->timeouts.count);
387 assert(!loop->signals.count);
389 for splay_each(io_t, io, &loop->ios) {
390 splay_unlink_node(&loop->ios, splay_node);
393 for splay_each(timeout_t, timeout, &loop->timeouts) {
394 splay_unlink_node(&loop->timeouts, splay_node);
397 for splay_each(signal_t, signal, &loop->signals) {
398 splay_unlink_node(&loop->signals, splay_node);