]>
jfr.im git - irc/evilnet/x3.git/blob - src/timeq.c
1 /* timeq.c - time-based event queue
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
6 * x3 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.
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.
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.
36 timeq_del(0, 0, 0, TIMEQ_IGNORE_WHEN
|TIMEQ_IGNORE_FUNC
|TIMEQ_IGNORE_DATA
);
43 timeq
= heap_new(int_comparator
);
44 reg_exit_func(timeq_cleanup
);
51 heap_peek(timeq
, &time
, 0);
56 timeq_add_real(time_t when
, timeq_func func
, void *data
, UNUSED_ARG(const char *calling_func
))
58 struct timeq_entry
*ent
;
60 /* log_module(MAIN_LOG, LOG_DEBUG, "TIMEQ: %s adding timer: %ul (address: %x)", calling_func, (unsigned int)when, (unsigned int)func); */
61 ent
= malloc(sizeof(struct timeq_entry
));
65 heap_insert(timeq
, w
, ent
);
76 timeq_del_matching(void *key
, void *data
, void *extra
)
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
))) {
83 /* log_module(MAIN_LOG, LOG_DEBUG, "TIMEQ: - deleting matching timer %x", a->func); */
92 timeq_del_real(time_t when
, timeq_func func
, void *data
, int mask
, UNUSED_ARG(const char *calling_func
))
94 struct timeq_extra extra
;
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); */
100 heap_remove_pred(timeq
, timeq_del_matching
, &extra
);
106 return heap_size(timeq
);
113 struct timeq_entry
*ent
;
114 while (heap_size(timeq
) > 0) {
115 heap_peek(timeq
, &k
, &d
);
120 ent
->func(ent
->data
);