X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fevent.c;h=1283edd332b8334fa8fc78a6faedb03d2a397516;hb=refs%2Fheads%2F1.1;hp=295e28f19e9394976054452230e932a169c47ce9;hpb=627f7c22b447bd464b536cd016278545674df93d;p=tinc diff --git a/src/event.c b/src/event.c index 295e28f1..05ddb47f 100644 --- a/src/event.c +++ b/src/event.c @@ -1,7 +1,6 @@ /* - event.c -- event queue - Copyright (C) 2002 Guus Sliepen , - 2002 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,98 +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: event.c,v 1.1.4.3 2002/06/21 10:11:12 guus Exp $ + 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 "config.h" - -#include -#include -#include -#include -#include -#include +#include "system.h" #include "event.h" -#include "system.h" +struct timeval now; -avl_tree_t *event_tree; -extern time_t 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; + } -int event_compare(event_t *a, event_t *b) -{ - if(a->time > b->time) - return 1; - if(a->time < b->time) - return -1; - return a->id - b->id; -} + if(a->event > b->event) { + return 1; + } -void init_events(void) -{ -cp - event_tree = avl_alloc_tree((avl_compare_t)event_compare, NULL); -cp + return 0; +#endif } -void exit_events(void) -{ -cp - avl_delete_tree(event_tree); -cp -} +static int timeout_compare(const timeout_t *a, const timeout_t *b) { + struct timeval diff; + timersub(&a->tv, &b->tv, &diff); + + if(diff.tv_sec < 0) { + return -1; + } + + if(diff.tv_sec > 0) { + return 1; + } + + if(diff.tv_usec < 0) { + return -1; + } + + if(diff.tv_usec > 0) { + return 1; + } + + uintptr_t ap = (uintptr_t)a; + uintptr_t bp = (uintptr_t)b; + + if(ap < bp) { + return -1; + } -event_t *new_event(void) -{ - event_t *event; -cp - event = (event_t *)xmalloc_and_zero(sizeof(*event)); -cp - return event; + if(ap > bp) { + return 1; + } + + return 0; } -void free_event(event_t *event) -{ -cp - free(event); -cp +splay_tree_t io_tree = {.compare = (splay_compare_t)io_compare}; +splay_tree_t timeout_tree = {.compare = (splay_compare_t)timeout_compare}; + +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 - event->id = ++id; - avl_insert(event_tree, 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); + } + + timeradd(&now, tv, &timeout->tv); + + if(!splay_insert_node(&timeout_tree, &timeout->node)) { + abort(); + } } -void event_del(event_t *event) -{ -cp - avl_delete(event_tree, event); -cp +void timeout_del(timeout_t *timeout) { + if(!timeout->cb) { + return; + } + + 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; -cp - if(event_tree->head) - { - event = (event_t *)event_tree->head->data; - if(event->time < now) - { - avl_delete(event_tree, event); - return event; - } - } -cp - return NULL; +struct timeval *timeout_execute(struct timeval *diff) { + gettimeofday(&now, NULL); + struct timeval *tv = NULL; + + while(timeout_tree.head) { + timeout_t *timeout = timeout_tree.head->data; + timersub(&timeout->tv, &now, diff); + + if(diff->tv_sec < 0) { + timeout->cb(timeout->data); + + if(timercmp(&timeout->tv, &now, <)) { + timeout_del(timeout); + } + } else { + tv = diff; + break; + } + } + + return tv; }