]>
Commit | Line | Data |
---|---|---|
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 3 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 | timeq = NULL; | |
38 | } | |
39 | ||
40 | static void | |
41 | timeq_init(void) | |
42 | { | |
43 | timeq = heap_new(ulong_comparator); | |
44 | reg_exit_func(timeq_cleanup); | |
45 | } | |
46 | ||
47 | unsigned long | |
48 | timeq_next(void) | |
49 | { | |
50 | void *timep; | |
51 | if (!timeq) | |
52 | return ~0; | |
53 | heap_peek(timeq, &timep, 0); | |
54 | return (unsigned long)timep; | |
55 | } | |
56 | ||
57 | void | |
58 | timeq_add(unsigned long when, timeq_func func, void *data) | |
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; | |
66 | if (!timeq) | |
67 | timeq_init(); | |
68 | heap_insert(timeq, w, ent); | |
69 | } | |
70 | ||
71 | struct timeq_extra { | |
72 | unsigned long when; | |
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; | |
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))) { | |
86 | free(data); | |
87 | return 1; | |
88 | } else { | |
89 | return 0; | |
90 | } | |
91 | } | |
92 | ||
93 | void | |
94 | timeq_del(unsigned long when, timeq_func func, void *data, int mask) | |
95 | { | |
96 | struct timeq_extra extra; | |
97 | extra.when = when; | |
98 | extra.func = func; | |
99 | extra.data = data; | |
100 | extra.mask = mask; | |
101 | if (timeq) | |
102 | heap_remove_pred(timeq, timeq_del_matching, &extra); | |
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) { | |
117 | heap_peek(timeq, &k, &d); | |
118 | if ((time_t)k > now) | |
119 | break; | |
120 | ent = d; | |
121 | heap_pop(timeq); | |
122 | ent->func(ent->data); | |
123 | free(ent); | |
124 | } | |
125 | } |