]>
Commit | Line | Data |
---|---|---|
1 | /* timeq.c - time-based event queue | |
2 | * Copyright 2000-2004 srvx Development Team | |
3 | * | |
4 | * This file is part of x3. | |
5 | * | |
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. | |
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 | #include "log.h" | |
25 | ||
26 | heap_t timeq; | |
27 | ||
28 | struct timeq_entry { | |
29 | timeq_func func; | |
30 | void *data; | |
31 | }; | |
32 | ||
33 | static void | |
34 | timeq_cleanup(void) | |
35 | { | |
36 | timeq_del(0, 0, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_FUNC|TIMEQ_IGNORE_DATA); | |
37 | heap_delete(timeq); | |
38 | } | |
39 | ||
40 | void | |
41 | timeq_init(void) | |
42 | { | |
43 | timeq = heap_new(int_comparator); | |
44 | reg_exit_func(timeq_cleanup); | |
45 | } | |
46 | ||
47 | time_t | |
48 | timeq_next(void) | |
49 | { | |
50 | void *time; | |
51 | heap_peek(timeq, &time, 0); | |
52 | return (time_t)time; | |
53 | } | |
54 | ||
55 | void | |
56 | timeq_add_real(time_t when, timeq_func func, void *data, UNUSED_ARG(const char *calling_func)) | |
57 | { | |
58 | struct timeq_entry *ent; | |
59 | void *w; | |
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)); | |
62 | ent->func = func; | |
63 | ent->data = data; | |
64 | w = (void*)when; | |
65 | heap_insert(timeq, w, ent); | |
66 | } | |
67 | ||
68 | struct timeq_extra { | |
69 | time_t when; | |
70 | timeq_func func; | |
71 | void *data; | |
72 | int mask; | |
73 | }; | |
74 | ||
75 | static int | |
76 | timeq_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))) { | |
83 | /* log_module(MAIN_LOG, LOG_DEBUG, "TIMEQ: - deleting matching timer %x", a->func); */ | |
84 | free(data); | |
85 | return 1; | |
86 | } else { | |
87 | return 0; | |
88 | } | |
89 | } | |
90 | ||
91 | void | |
92 | timeq_del_real(time_t when, timeq_func func, void *data, int mask, UNUSED_ARG(const char *calling_func)) | |
93 | { | |
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); */ | |
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 | ||
103 | unsigned int | |
104 | timeq_size(void) | |
105 | { | |
106 | return heap_size(timeq); | |
107 | } | |
108 | ||
109 | void | |
110 | timeq_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 | } |