License update
[srvx.git] / src / timeq.c
1 /* timeq.c - time-based event queue
2  * Copyright 2000-2004 srvx Development Team
3  *
4  * This file is part of srvx.
5  *
6  * srvx 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
17  * along with srvx; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
19  */
20
21 #include "common.h"
22 #include "heap.h"
23 #include "timeq.h"
24
25 heap_t timeq;
26
27 struct timeq_entry {
28     timeq_func func;
29     void *data;
30 };
31
32 static void
33 timeq_cleanup(void)
34 {
35     timeq_del(0, 0, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_FUNC|TIMEQ_IGNORE_DATA);
36     heap_delete(timeq);
37 }
38
39 void
40 timeq_init(void)
41 {
42     timeq = heap_new(int_comparator);
43     reg_exit_func(timeq_cleanup);
44 }
45
46 time_t
47 timeq_next(void)
48 {
49     void *time;
50     heap_peek(timeq, &time, 0);
51     return (time_t)time;
52 }
53
54 void
55 timeq_add(time_t when, timeq_func func, void *data)
56 {
57     struct timeq_entry *ent;
58     void *w;
59     ent = malloc(sizeof(struct timeq_entry));
60     ent->func = func;
61     ent->data = data;
62     w = (void*)when;
63     heap_insert(timeq, w, ent);
64 }
65
66 struct timeq_extra {
67     time_t when;
68     timeq_func func;
69     void *data;
70     int mask;
71 };
72
73 static int
74 timeq_del_matching(void *key, void *data, void *extra)
75 {
76     struct timeq_entry *a = data;
77     struct timeq_extra *b = extra;
78     if (((b->mask & TIMEQ_IGNORE_WHEN) || ((time_t)key == b->when))
79         && ((b->mask & TIMEQ_IGNORE_FUNC) || (a->func == b->func))
80         && ((b->mask & TIMEQ_IGNORE_DATA) || (a->data == b->data))) {
81         free(data);
82         return 1;
83     } else {
84         return 0;
85     }
86 }
87
88 void
89 timeq_del(time_t when, timeq_func func, void *data, int mask)
90 {
91     struct timeq_extra extra;
92     extra.when = when;
93     extra.func = func;
94     extra.data = data;
95     extra.mask = mask;
96     heap_remove_pred(timeq, timeq_del_matching, &extra);
97 }
98
99 unsigned int
100 timeq_size(void)
101 {
102     return heap_size(timeq);
103 }
104
105 void
106 timeq_run(void)
107 {
108     void *k, *d;
109     struct timeq_entry *ent;
110     while (heap_size(timeq) > 0) {
111         heap_peek(timeq, &k, &d);
112         if ((time_t)k > now)
113             break;
114         ent = d;
115         heap_pop(timeq);
116         ent->func(ent->data);
117         free(ent);
118     }
119 }