tinc/src/event.c

112 lines
2 KiB
C
Raw Normal View History

2002-02-11 10:05:58 +00:00
/*
event.c -- event queue
Copyright (C) 2002 Guus Sliepen <guus@sliepen.eu.org>,
2002 Ivo Timmermans <ivo@o2w.nl>
2002-02-11 10:05:58 +00:00
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
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
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.
2002-09-09 21:25:28 +00:00
$Id: event.c,v 1.1.4.5 2002/09/09 21:24:31 guus Exp $
2002-02-11 10:05:58 +00:00
*/
#include "config.h"
#include <stdlib.h>
#include <xalloc.h>
#include <string.h>
#include <utils.h>
#include <avl_tree.h>
#include <time.h>
#include "event.h"
#include "system.h"
avl_tree_t *event_tree;
extern time_t now;
2002-02-11 10:05:58 +00:00
int id;
2002-09-09 21:25:28 +00:00
int event_compare(event_t * a, event_t * b)
2002-02-11 10:05:58 +00:00
{
2002-09-09 21:25:28 +00:00
if(a->time > b->time)
return 1;
if(a->time < b->time)
return -1;
return a->id - b->id;
2002-02-11 10:05:58 +00:00
}
void init_events(void)
{
2002-09-09 21:25:28 +00:00
cp();
event_tree = avl_alloc_tree((avl_compare_t) event_compare, NULL);
2002-02-11 10:05:58 +00:00
}
void exit_events(void)
{
2002-09-09 21:25:28 +00:00
cp();
avl_delete_tree(event_tree);
2002-02-11 10:05:58 +00:00
}
event_t *new_event(void)
{
2002-09-09 21:25:28 +00:00
cp();
return (event_t *) xmalloc_and_zero(sizeof(event_t));
2002-02-11 10:05:58 +00:00
}
2002-09-09 21:25:28 +00:00
void free_event(event_t * event)
2002-02-11 10:05:58 +00:00
{
2002-09-09 21:25:28 +00:00
cp();
free(event);
2002-02-11 10:05:58 +00:00
}
2002-09-09 21:25:28 +00:00
void event_add(event_t * event)
2002-02-11 10:05:58 +00:00
{
2002-09-09 21:25:28 +00:00
cp();
event->id = ++id;
avl_insert(event_tree, event);
2002-02-11 10:05:58 +00:00
}
2002-09-09 21:25:28 +00:00
void event_del(event_t * event)
2002-02-11 10:05:58 +00:00
{
2002-09-09 21:25:28 +00:00
cp();
avl_delete(event_tree, event);
2002-02-11 10:05:58 +00:00
}
event_t *get_expired_event(void)
{
2002-09-09 21:25:28 +00:00
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;
}
}
return NULL;
2002-02-11 10:05:58 +00:00
}