]> git.meshlink.io Git - meshlink/blobdiff - src/event.c
Avoid allocating packet buffers unnecessarily.
[meshlink] / src / event.c
index f88112ff6dfea4f8a03e8a72d4090014e8b59f56..739ff35de5a595b76fee4f56ed43ca42b31e9e4c 100644 (file)
 #include "utils.h"
 #include "xalloc.h"
 
-static int io_compare(const io_t *a, const io_t *b) {
-       return a->fd - b->fd;
+#ifndef EVENT_CLOCK
+#if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
+#define EVENT_CLOCK CLOCK_MONOTONIC_RAW
+#else
+#define EVENT_CLOCK CLOCK_MONOTONIC
+#endif
+#endif
+
+static void timespec_add(const struct timespec *a, const struct timespec *b, struct timespec *r) {
+       r->tv_sec = a->tv_sec + b->tv_sec;
+       r->tv_nsec = a->tv_nsec + b->tv_nsec;
+
+       if(r->tv_nsec > 1000000000) {
+               r->tv_sec++, r->tv_nsec -= 1000000000;
+       }
 }
 
-static int timeout_compare(const timeout_t *a, const timeout_t *b) {
-       struct timeval diff;
-       timersub(&a->tv, &b->tv, &diff);
+static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) {
+       r->tv_sec = a->tv_sec - b->tv_sec;
+       r->tv_nsec = a->tv_nsec - b->tv_nsec;
 
-       if(diff.tv_sec < 0) {
-               return -1;
+       if(r->tv_nsec < 0) {
+               r->tv_sec--, r->tv_nsec += 1000000000;
        }
+}
 
-       if(diff.tv_sec > 0) {
-               return 1;
+static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
+       if(a->tv_sec == b->tv_sec) {
+               return a->tv_nsec < b->tv_nsec;
+       } else {
+               return a->tv_sec < b->tv_sec;
        }
+}
 
-       if(diff.tv_usec < 0) {
-               return -1;
-       }
+static void timespec_clear(struct timespec *a) {
+       a->tv_sec = 0;
+}
 
-       if(diff.tv_usec > 0) {
-               return 1;
-       }
+static bool timespec_isset(const struct timespec *a) {
+       return a->tv_sec;
+}
 
-       if(a < b) {
-               return -1;
-       }
+static int io_compare(const io_t *a, const io_t *b) {
+       return a->fd - b->fd;
+}
 
-       if(a > b) {
+static int timeout_compare(const timeout_t *a, const timeout_t *b) {
+       if(a->tv.tv_sec < b->tv.tv_sec) {
+               return -1;
+       } else if(a->tv.tv_sec > b->tv.tv_sec) {
+               return 1;
+       } else if(a->tv.tv_nsec < b->tv.tv_nsec) {
+               return -1;
+       } else if(a->tv.tv_nsec > b->tv.tv_nsec) {
                return 1;
+       } else if(a < b) {
+               return -1;
+       } else if(a > b) {
+               return 1;
+       } else {
+               return 0;
        }
-
-       return 0;
 }
 
 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
-       if(io->cb) {
-               return;
-       }
+       assert(!io->cb);
 
        io->fd = fd;
        io->cb = cb;
@@ -73,12 +100,14 @@ void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int fl
 
        io_set(loop, io, flags);
 
-       if(!splay_insert_node(&loop->ios, &io->node)) {
-               abort();
-       }
+       splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
+       assert(node);
+       (void)node;
 }
 
 void io_set(event_loop_t *loop, io_t *io, int flags) {
+       assert(io->cb);
+
        io->flags = flags;
 
        if(flags & IO_READ) {
@@ -95,9 +124,7 @@ void io_set(event_loop_t *loop, io_t *io, int flags) {
 }
 
 void io_del(event_loop_t *loop, io_t *io) {
-       if(!io->cb) {
-               return;
-       }
+       assert(io->cb);
 
        loop->deletion = true;
 
@@ -107,7 +134,7 @@ void io_del(event_loop_t *loop, io_t *io) {
        io->cb = NULL;
 }
 
-void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
+void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timespec *tv) {
        timeout->cb = cb;
        timeout->data = data;
        timeout->node.data = timeout;
@@ -115,16 +142,18 @@ void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *
        timeout_set(loop, timeout, tv);
 }
 
-void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
-       if(timerisset(&timeout->tv)) {
+void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timespec *tv) {
+       assert(timeout->cb);
+
+       if(timespec_isset(&timeout->tv)) {
                splay_unlink_node(&loop->timeouts, &timeout->node);
        }
 
        if(!loop->now.tv_sec) {
-               gettimeofday(&loop->now, NULL);
+               clock_gettime(EVENT_CLOCK, &loop->now);
        }
 
-       timeradd(&loop->now, tv, &timeout->tv);
+       timespec_add(&loop->now, tv, &timeout->tv);
 
        if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
                abort();
@@ -135,9 +164,7 @@ void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
 
 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
        splay_unlink_node(&loop->timeouts, &timeout->node);
-       timeout->tv = (struct timeval) {
-               0, 0
-       };
+       timespec_clear(&timeout->tv);
 }
 
 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
@@ -145,7 +172,7 @@ void timeout_del(event_loop_t *loop, timeout_t *timeout) {
                return;
        }
 
-       if(timerisset(&timeout->tv)) {
+       if(timespec_isset(&timeout->tv)) {
                timeout_disable(loop, timeout);
        }
 
@@ -166,17 +193,21 @@ static void signalio_handler(event_loop_t *loop, void *data, int flags) {
                return;
        }
 
-       signal_t *sig = splay_search(&loop->signals, &((signal_t) {
+       signal_t *sig = splay_search(&loop->signals, &(signal_t) {
                .signum = signum
-       }));
+       });
 
        if(sig) {
+               sig->set = false;
                sig->cb(loop, sig->data);
        }
 }
 
 static void pipe_init(event_loop_t *loop) {
-       if(!pipe(loop->pipefd)) {
+       int result = pipe(loop->pipefd);
+       assert(result == 0);
+
+       if(result == 0) {
                io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
        }
 }
@@ -192,19 +223,23 @@ static void pipe_exit(event_loop_t *loop) {
 }
 
 void signal_trigger(event_loop_t *loop, signal_t *sig) {
+       if(sig->set) {
+               return;
+       }
+
        uint8_t signum = sig->signum;
+       sig->set = true;
        write(loop->pipefd[1], &signum, 1);
        return;
 }
 
 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
-       if(sig->cb) {
-               return;
-       }
+       assert(!sig->cb);
 
        sig->cb = cb;
        sig->data = data;
        sig->signum = signum;
+       sig->set = false;
        sig->node.data = sig;
 
        if(loop->pipefd[0] == -1) {
@@ -217,9 +252,7 @@ void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, u
 }
 
 void signal_del(event_loop_t *loop, signal_t *sig) {
-       if(!sig->cb) {
-               return;
-       }
+       assert(sig->cb);
 
        loop->deletion = true;
 
@@ -237,22 +270,23 @@ void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
 }
 
 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
+       assert(mutex);
+
        fd_set readable;
        fd_set writable;
 
        while(loop->running) {
-               gettimeofday(&loop->now, NULL);
-               struct timeval diff, it, *tv = NULL;
+               clock_gettime(EVENT_CLOCK, &loop->now);
+               struct timespec it, ts = {3600, 0};
 
                while(loop->timeouts.head) {
                        timeout_t *timeout = loop->timeouts.head->data;
-                       timersub(&timeout->tv, &loop->now, &diff);
 
-                       if(diff.tv_sec < 0) {
+                       if(timespec_lt(&timeout->tv, &loop->now)) {
                                timeout_disable(loop, timeout);
                                timeout->cb(loop, timeout->data);
                        } else {
-                               tv = &diff;
+                               timespec_sub(&timeout->tv, &loop->now, &ts);
                                break;
                        }
                }
@@ -260,8 +294,8 @@ bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
                if(loop->idle_cb) {
                        it = loop->idle_cb(loop, loop->idle_data);
 
-                       if(it.tv_sec >= 0 && (!tv || timercmp(&it, tv, <))) {
-                               tv = &it;
+                       if(it.tv_sec >= 0 && timespec_lt(&it, &ts)) {
+                               ts = it;
                        }
                }
 
@@ -276,17 +310,18 @@ bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
                }
 
                // release mesh mutex during select
-               if(mutex) {
-                       pthread_mutex_unlock(mutex);
-               }
+               pthread_mutex_unlock(mutex);
 
-               int n = select(fds, &readable, &writable, NULL, tv);
+#ifdef HAVE_PSELECT
+               int n = pselect(fds, &readable, &writable, NULL, &ts, NULL);
+#else
+               struct timeval tv = {ts.tv_sec, ts.tv_nsec / 1000};
+               int n = select(fds, &readable, &writable, NULL, (struct timeval *)&tv);
+#endif
 
-               if(mutex) {
-                       pthread_mutex_lock(mutex);
-               }
+               pthread_mutex_lock(mutex);
 
-               gettimeofday(&loop->now, NULL);
+               clock_gettime(EVENT_CLOCK, &loop->now);
 
                if(n < 0) {
                        if(sockwouldblock(errno)) {
@@ -349,10 +384,14 @@ void event_loop_init(event_loop_t *loop) {
        loop->signals.compare = (splay_compare_t)signal_compare;
        loop->pipefd[0] = -1;
        loop->pipefd[1] = -1;
-       gettimeofday(&loop->now, NULL);
+       clock_gettime(EVENT_CLOCK, &loop->now);
 }
 
 void event_loop_exit(event_loop_t *loop) {
+       assert(!loop->ios.count);
+       assert(!loop->timeouts.count);
+       assert(!loop->signals.count);
+
        for splay_each(io_t, io, &loop->ios) {
                splay_unlink_node(&loop->ios, node);
        }