]> jfr.im git - irc/evilnet/x3.git/blame - src/timeq.c
ignore stats headers
[irc/evilnet/x3.git] / src / timeq.c
CommitLineData
d76ed9a9 1/* timeq.c - time-based event queue
2 * Copyright 2000-2004 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
d76ed9a9 5 *
d0f04f71 6 * x3 is free software; you can redistribute it and/or modify
d76ed9a9 7 * it under the terms of the GNU General Public License as published by
348683aa 8 * the Free Software Foundation; either version 3 of the License, or
d76ed9a9 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"
9079d26c 24#include "log.h"
d76ed9a9 25
26heap_t timeq;
27
28struct timeq_entry {
29 timeq_func func;
30 void *data;
31};
32
33static void
34timeq_cleanup(void)
35{
36 timeq_del(0, 0, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_FUNC|TIMEQ_IGNORE_DATA);
37 heap_delete(timeq);
38}
39
0f6fe38c 40void
d76ed9a9 41timeq_init(void)
42{
43 timeq = heap_new(int_comparator);
44 reg_exit_func(timeq_cleanup);
45}
46
47time_t
48timeq_next(void)
49{
50 void *time;
51 heap_peek(timeq, &time, 0);
52 return (time_t)time;
53}
54
55void
9079d26c 56timeq_add_real(time_t when, timeq_func func, void *data, UNUSED_ARG(const char *calling_func))
d76ed9a9 57{
58 struct timeq_entry *ent;
59 void *w;
9079d26c 60/* log_module(MAIN_LOG, LOG_DEBUG, "TIMEQ: %s adding timer: %ul (address: %x)", calling_func, (unsigned int)when, (unsigned int)func); */
d76ed9a9 61 ent = malloc(sizeof(struct timeq_entry));
62 ent->func = func;
63 ent->data = data;
64 w = (void*)when;
65 heap_insert(timeq, w, ent);
66}
67
68struct timeq_extra {
69 time_t when;
70 timeq_func func;
71 void *data;
72 int mask;
73};
74
75static int
76timeq_del_matching(void *key, void *data, void *extra)
77{
78 struct timeq_entry *a = data;
79 struct timeq_extra *b = extra;
80 if (((b->mask & TIMEQ_IGNORE_WHEN) || ((time_t)key == b->when))
81 && ((b->mask & TIMEQ_IGNORE_FUNC) || (a->func == b->func))
82 && ((b->mask & TIMEQ_IGNORE_DATA) || (a->data == b->data))) {
9079d26c 83 /* log_module(MAIN_LOG, LOG_DEBUG, "TIMEQ: - deleting matching timer %x", a->func); */
d76ed9a9 84 free(data);
85 return 1;
86 } else {
87 return 0;
88 }
89}
90
91void
9079d26c 92timeq_del_real(time_t when, timeq_func func, void *data, int mask, UNUSED_ARG(const char *calling_func))
d76ed9a9 93{
94 struct timeq_extra extra;
9079d26c 95 /* log_module(MAIN_LOG, LOG_DEBUG, "TIMEQ: %s deleting timer: %d (address: %x mask: %x)", calling_func, (unsigned int)when, (unsigned int) func, mask); */
d76ed9a9 96 extra.when = when;
97 extra.func = func;
98 extra.data = data;
99 extra.mask = mask;
100 heap_remove_pred(timeq, timeq_del_matching, &extra);
101}
102
103unsigned int
104timeq_size(void)
105{
106 return heap_size(timeq);
107}
108
109void
110timeq_run(void)
111{
112 void *k, *d;
113 struct timeq_entry *ent;
114 while (heap_size(timeq) > 0) {
115 heap_peek(timeq, &k, &d);
116 if ((time_t)k > now)
117 break;
118 ent = d;
119 heap_pop(timeq);
120 ent->func(ent->data);
121 free(ent);
122 }
123}