1 /* timeq.c - time-based event queue
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of srvx.
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.
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.
35 timeq_del(0, 0, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_FUNC|TIMEQ_IGNORE_DATA);
43 timeq = heap_new(ulong_comparator);
44 reg_exit_func(timeq_cleanup);
53 heap_peek(timeq, &time, 0);
54 return (unsigned long)time;
58 timeq_add(unsigned long when, timeq_func func, void *data)
60 struct timeq_entry *ent;
62 ent = malloc(sizeof(struct timeq_entry));
68 heap_insert(timeq, w, ent);
79 timeq_del_matching(void *key, void *data, void *extra)
81 struct timeq_entry *a = data;
82 struct timeq_extra *b = extra;
83 if (((b->mask & TIMEQ_IGNORE_WHEN) || ((unsigned long)key == b->when))
84 && ((b->mask & TIMEQ_IGNORE_FUNC) || (a->func == b->func))
85 && ((b->mask & TIMEQ_IGNORE_DATA) || (a->data == b->data))) {
94 timeq_del(unsigned long when, timeq_func func, void *data, int mask)
96 struct timeq_extra extra;
102 heap_remove_pred(timeq, timeq_del_matching, &extra);
108 return heap_size(timeq);
115 struct timeq_entry *ent;
116 while (heap_size(timeq) > 0) {
117 heap_peek(timeq, &k, &d);
118 if ((unsigned long)k > now)
122 ent->func(ent->data);