Fix checks for Cygwin-related macros.
[tinc] / src / event.c
1 /*
2     event.c -- event queue
3     Copyright (C) 2002-2009 Guus Sliepen <guus@tinc-vpn.org>,
4                   2002-2005 Ivo Timmermans
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "avl_tree.h"
24 #include "event.h"
25 #include "utils.h"
26 #include "xalloc.h"
27
28 avl_tree_t *event_tree;
29 extern time_t now;
30
31 static int id;
32
33 static int event_compare(const event_t *a, const event_t *b) {
34         if(a->time > b->time) {
35                 return 1;
36         }
37
38         if(a->time < b->time) {
39                 return -1;
40         }
41
42         return a->id - b->id;
43 }
44
45 void init_events(void) {
46         event_tree = avl_alloc_tree((avl_compare_t) event_compare, (avl_action_t) free_event);
47 }
48
49 void exit_events(void) {
50         avl_delete_tree(event_tree);
51 }
52
53 void expire_events(void) {
54         avl_node_t *node;
55         event_t *event;
56         time_t diff;
57
58         /*
59          * Make all events appear expired by subtracting the difference between
60          * the expiration time of the last event and the current time.
61          */
62
63         if(!event_tree->tail) {
64                 return;
65         }
66
67         event = event_tree->tail->data;
68
69         if(event->time <= now) {
70                 return;
71         }
72
73         diff = event->time - now;
74
75         for(node = event_tree->head; node; node = node->next) {
76                 event = node->data;
77                 event->time -= diff;
78         }
79 }
80
81 event_t *new_event(void) {
82         return xmalloc_and_zero(sizeof(event_t));
83 }
84
85 void free_event(event_t *event) {
86         free(event);
87 }
88
89 void event_add(event_t *event) {
90         event->id = ++id;
91         avl_insert(event_tree, event);
92 }
93
94 void event_del(event_t *event) {
95         avl_delete(event_tree, event);
96 }
97
98 event_t *get_expired_event(void) {
99         event_t *event;
100
101         if(event_tree->head) {
102                 event = event_tree->head->data;
103
104                 if(event->time <= now) {
105                         avl_node_t *node = event_tree->head;
106                         avl_unlink_node(event_tree, node);
107                         free(node);
108                         return event;
109                 }
110         }
111
112         return NULL;
113 }
114
115 event_t *peek_next_event(void) {
116         if(event_tree->head) {
117                 return event_tree->head->data;
118         }
119
120         return NULL;
121 }