]>
Commit | Line | Data |
---|---|---|
d76ed9a9 AS |
1 | /* timeq.c - time-based event queue |
2 | * Copyright 2000-2004 srvx Development Team | |
3 | * | |
1136f709 | 4 | * This file is part of srvx. |
d76ed9a9 | 5 | * |
1136f709 | 6 | * srvx 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 |
be2c97a5 | 8 | * the Free Software Foundation; either version 3 of the License, or |
d76ed9a9 AS |
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 | |
30874d66 | 33 | timeq_cleanup(UNUSED_ARG(void *extra)) |
d76ed9a9 AS |
34 | { |
35 | timeq_del(0, 0, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_FUNC|TIMEQ_IGNORE_DATA); | |
36 | heap_delete(timeq); | |
1136f709 | 37 | timeq = NULL; |
d76ed9a9 AS |
38 | } |
39 | ||
1136f709 | 40 | static void |
d76ed9a9 AS |
41 | timeq_init(void) |
42 | { | |
1136f709 | 43 | timeq = heap_new(ulong_comparator); |
30874d66 | 44 | reg_exit_func(timeq_cleanup, NULL); |
d76ed9a9 AS |
45 | } |
46 | ||
1136f709 | 47 | unsigned long |
d76ed9a9 AS |
48 | timeq_next(void) |
49 | { | |
1136f709 | 50 | void *timep; |
51 | if (!timeq) | |
52 | return ~0; | |
53 | heap_peek(timeq, &timep, 0); | |
54 | return (unsigned long)timep; | |
d76ed9a9 AS |
55 | } |
56 | ||
57 | void | |
1136f709 | 58 | timeq_add(unsigned long when, timeq_func func, void *data) |
d76ed9a9 AS |
59 | { |
60 | struct timeq_entry *ent; | |
61 | void *w; | |
62 | ent = malloc(sizeof(struct timeq_entry)); | |
63 | ent->func = func; | |
64 | ent->data = data; | |
65 | w = (void*)when; | |
1136f709 | 66 | if (!timeq) |
67 | timeq_init(); | |
d76ed9a9 AS |
68 | heap_insert(timeq, w, ent); |
69 | } | |
70 | ||
71 | struct timeq_extra { | |
1136f709 | 72 | unsigned long when; |
d76ed9a9 AS |
73 | timeq_func func; |
74 | void *data; | |
75 | int mask; | |
76 | }; | |
77 | ||
78 | static int | |
79 | timeq_del_matching(void *key, void *data, void *extra) | |
80 | { | |
81 | struct timeq_entry *a = data; | |
82 | struct timeq_extra *b = extra; | |
1136f709 | 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))) { | |
d76ed9a9 AS |
86 | free(data); |
87 | return 1; | |
88 | } else { | |
89 | return 0; | |
90 | } | |
91 | } | |
92 | ||
93 | void | |
1136f709 | 94 | timeq_del(unsigned long when, timeq_func func, void *data, int mask) |
d76ed9a9 AS |
95 | { |
96 | struct timeq_extra extra; | |
97 | extra.when = when; | |
98 | extra.func = func; | |
99 | extra.data = data; | |
100 | extra.mask = mask; | |
1136f709 | 101 | if (timeq) |
102 | heap_remove_pred(timeq, timeq_del_matching, &extra); | |
d76ed9a9 AS |
103 | } |
104 | ||
105 | unsigned int | |
106 | timeq_size(void) | |
107 | { | |
108 | return heap_size(timeq); | |
109 | } | |
110 | ||
111 | void | |
112 | timeq_run(void) | |
113 | { | |
114 | void *k, *d; | |
115 | struct timeq_entry *ent; | |
116 | while (heap_size(timeq) > 0) { | |
1136f709 | 117 | heap_peek(timeq, &k, &d); |
118 | if ((time_t)k > now) | |
d76ed9a9 | 119 | break; |
1136f709 | 120 | ent = d; |
121 | heap_pop(timeq); | |
122 | ent->func(ent->data); | |
d76ed9a9 AS |
123 | free(ent); |
124 | } | |
125 | } |