X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fevent.c;h=05ddb47fb4031aff06ffb1c83278583e9ecff588;hb=c45a3fd7319d03bd147448a017f5aaed3b46fdfe;hp=e7fbb882c1cb9a42d426edfff48a9603181f2207;hpb=0714ac6c59099a398e67770ad9c72fcec615812b;p=tinc diff --git a/src/event.c b/src/event.c index e7fbb882..05ddb47f 100644 --- a/src/event.c +++ b/src/event.c @@ -1,7 +1,6 @@ /* - event.c -- event queue - Copyright (C) 2002-2006 Guus Sliepen , - 2002-2005 Ivo Timmermans + event.c -- I/O, timeout, and event handling + Copyright (C) 2012-2022 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,93 +12,126 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id$ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "system.h" -#include "avl_tree.h" #include "event.h" -#include "utils.h" -#include "xalloc.h" -avl_tree_t *event_tree; -extern time_t now; +struct timeval now; + +static int io_compare(const io_t *a, const io_t *b) { +#ifndef HAVE_WINDOWS + return a->fd - b->fd; +#else -int id; + if(a->event < b->event) { + return -1; + } -static int event_compare(const event_t *a, const event_t *b) -{ - if(a->time > b->time) + if(a->event > b->event) { return 1; + } + + return 0; +#endif +} + +static int timeout_compare(const timeout_t *a, const timeout_t *b) { + struct timeval diff; + timersub(&a->tv, &b->tv, &diff); - if(a->time < b->time) + if(diff.tv_sec < 0) { return -1; + } - return a->id - b->id; -} + if(diff.tv_sec > 0) { + return 1; + } -void init_events(void) -{ - cp(); + if(diff.tv_usec < 0) { + return -1; + } - event_tree = avl_alloc_tree((avl_compare_t) event_compare, NULL); -} + if(diff.tv_usec > 0) { + return 1; + } -void exit_events(void) -{ - cp(); + uintptr_t ap = (uintptr_t)a; + uintptr_t bp = (uintptr_t)b; - avl_delete_tree(event_tree); -} + if(ap < bp) { + return -1; + } -event_t *new_event(void) -{ - cp(); + if(ap > bp) { + return 1; + } - return xmalloc_and_zero(sizeof(event_t)); + return 0; } -void free_event(event_t *event) -{ - cp(); +splay_tree_t io_tree = {.compare = (splay_compare_t)io_compare}; +splay_tree_t timeout_tree = {.compare = (splay_compare_t)timeout_compare}; - free(event); +void timeout_add(timeout_t *timeout, timeout_cb_t cb, void *data, const struct timeval *tv) { + timeout->cb = cb; + timeout->data = data; + timeout->node.data = timeout; + + timeout_set(timeout, tv); } -void event_add(event_t *event) -{ - cp(); +void timeout_set(timeout_t *timeout, const struct timeval *tv) { + if(timerisset(&timeout->tv)) { + splay_unlink_node(&timeout_tree, &timeout->node); + } + + if(!now.tv_sec) { + gettimeofday(&now, NULL); + } - event->id = ++id; - avl_insert(event_tree, event); + timeradd(&now, tv, &timeout->tv); + + if(!splay_insert_node(&timeout_tree, &timeout->node)) { + abort(); + } } -void event_del(event_t *event) -{ - cp(); +void timeout_del(timeout_t *timeout) { + if(!timeout->cb) { + return; + } - avl_delete(event_tree, event); + splay_unlink_node(&timeout_tree, &timeout->node); + timeout->cb = 0; + timeout->tv = (struct timeval) { + 0, 0 + }; } -event_t *get_expired_event(void) -{ - event_t *event; +struct timeval *timeout_execute(struct timeval *diff) { + gettimeofday(&now, NULL); + struct timeval *tv = NULL; - cp(); + while(timeout_tree.head) { + timeout_t *timeout = timeout_tree.head->data; + timersub(&timeout->tv, &now, diff); - if(event_tree->head) { - event = event_tree->head->data; + if(diff->tv_sec < 0) { + timeout->cb(timeout->data); - if(event->time < now) { - event_del(event); - return event; + if(timercmp(&timeout->tv, &now, <)) { + timeout_del(timeout); + } + } else { + tv = diff; + break; } } - return NULL; + return tv; }