X-Git-Url: https://www.tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fevent.c;h=89ee02292f76b4d75c1acfa7ade864ea34c77459;hp=028483bdce515648bfcf33d0906ba0e883e2ceed;hb=63f8303a5dc1758876451a580a8317dbc3d295d6;hpb=14979f835df4214a7c2510852f7ffedc9e08c2c0 diff --git a/src/event.c b/src/event.c index 028483bd..89ee0229 100644 --- a/src/event.c +++ b/src/event.c @@ -1,7 +1,7 @@ /* event.c -- event queue - Copyright (C) 2002 Guus Sliepen , - 2002 Ivo Timmermans + Copyright (C) 2002-2009 Guus Sliepen , + 2002-2005 Ivo Timmermans 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 +13,102 @@ 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.2 2002/03/01 14:09:30 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 "avl_tree.h" #include "event.h" - -#include "system.h" +#include "utils.h" +#include "xalloc.h" avl_tree_t *event_tree; extern time_t now; -int id; +static int id; + +static int event_compare(const event_t *a, const event_t *b) { + if(a->time > b->time) + return 1; + + if(a->time < b->time) + 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; + return a->id - b->id; } -void init_events(void) -{ -cp - event_tree = avl_alloc_tree((avl_compare_t)event_compare, NULL); -cp +void init_events(void) { + event_tree = avl_alloc_tree((avl_compare_t) event_compare, (avl_action_t) free_event); } -void exit_events(void) -{ -cp - avl_delete_tree(event_tree); -cp +void exit_events(void) { + avl_delete_tree(event_tree); } -event_t *new_event(void) -{ - event_t *event; -cp - event = (event_t *)xmalloc_and_zero(sizeof(*event)); -cp - return event; +void expire_events(void) { + avl_node_t *node; + event_t *event; + time_t diff; + + /* + * Make all events appear expired by substracting the difference between + * the expiration time of the last event and the current time. + */ + + if(!event_tree->tail) + return; + + event = event_tree->tail->data; + if(event->time <= now) + return; + + diff = event->time - now; + + for(node = event_tree->head; node; node = node->next) { + event = node->data; + event->time -= diff; + } } -void free_event(event_t *event) -{ -cp - free(event); -cp +event_t *new_event(void) { + return xmalloc_and_zero(sizeof(event_t)); } -void event_add(event_t *event) -{ -cp - event->id = ++id; - avl_insert(event_tree, event); -cp +void free_event(event_t *event) { + free(event); } -void event_del(event_t *event) -{ -cp - avl_delete(event_tree, event); -cp +void event_add(event_t *event) { + event->id = ++id; + avl_insert(event_tree, event); +} + +void event_del(event_t *event) { + avl_delete(event_tree, event); +} + +event_t *get_expired_event(void) { + event_t *event; + + if(event_tree->head) { + event = event_tree->head->data; + + if(event->time <= now) { + avl_node_t *node = event_tree->head; + avl_unlink_node(event_tree, node); + free(node); + return event; + } + } + + return NULL; } -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; +event_t *peek_next_event(void) { + if (event_tree->head) + return event_tree->head->data; + return NULL; }