39934deb |
1 | /* |
2 | * timing.c |
3 | * |
4 | * This module tracks any timers set up by schedule_timer(). It |
5 | * keeps all the currently active timers in a list; it informs the |
6 | * front end of when the next timer is due to go off if that |
7 | * changes; and, very importantly, it tracks the context pointers |
8 | * passed to schedule_timer(), so that if a context is freed all |
9 | * the timers associated with it can be immediately annulled. |
10 | */ |
11 | |
12 | #include <assert.h> |
13 | #include <stdio.h> |
14 | |
15 | #include "putty.h" |
16 | #include "tree234.h" |
17 | |
18 | struct timer { |
19 | timer_fn_t fn; |
20 | void *ctx; |
21 | long now; |
22 | }; |
23 | |
24 | static tree234 *timers = NULL; |
25 | static tree234 *timer_contexts = NULL; |
26 | static long now = 0L; |
27 | |
28 | static int compare_timers(void *av, void *bv) |
29 | { |
30 | struct timer *a = (struct timer *)av; |
31 | struct timer *b = (struct timer *)bv; |
32 | long at = a->now - now; |
33 | long bt = b->now - now; |
34 | |
35 | if (at < bt) |
36 | return -1; |
37 | else if (at > bt) |
38 | return +1; |
39 | |
40 | /* |
41 | * Failing that, compare on the other two fields, just so that |
42 | * we don't get unwanted equality. |
43 | */ |
8136216b |
44 | #ifdef __LCC__ |
45 | /* lcc won't let us compare function pointers. Legal, but annoying. */ |
46 | { |
47 | int c = memcmp(&a->fn, &b->fn, sizeof(a->fn)); |
48 | if (c < 0) |
49 | return -1; |
50 | else if (c > 0) |
51 | return +1; |
52 | } |
53 | #else |
39934deb |
54 | if (a->fn < b->fn) |
55 | return -1; |
56 | else if (a->fn > b->fn) |
57 | return +1; |
8136216b |
58 | #endif |
39934deb |
59 | |
60 | if (a->ctx < b->ctx) |
61 | return -1; |
62 | else if (a->ctx > b->ctx) |
63 | return +1; |
64 | |
65 | /* |
66 | * Failing _that_, the two entries genuinely are equal, and we |
67 | * never have a need to store them separately in the tree. |
68 | */ |
69 | return 0; |
70 | } |
71 | |
72 | static int compare_timer_contexts(void *av, void *bv) |
73 | { |
74 | char *a = (char *)av; |
75 | char *b = (char *)bv; |
76 | if (a < b) |
77 | return -1; |
78 | else if (a > b) |
79 | return +1; |
80 | return 0; |
81 | } |
82 | |
83 | static void init_timers(void) |
84 | { |
85 | if (!timers) { |
86 | timers = newtree234(compare_timers); |
87 | timer_contexts = newtree234(compare_timer_contexts); |
88 | now = GETTICKCOUNT(); |
89 | } |
90 | } |
91 | |
92 | long schedule_timer(int ticks, timer_fn_t fn, void *ctx) |
93 | { |
94 | long when; |
95 | struct timer *t, *first; |
96 | |
97 | init_timers(); |
98 | |
99 | when = ticks + GETTICKCOUNT(); |
100 | assert(when - now > 0); |
101 | |
102 | t = snew(struct timer); |
103 | t->fn = fn; |
104 | t->ctx = ctx; |
105 | t->now = when; |
106 | |
107 | if (t != add234(timers, t)) { |
108 | sfree(t); /* identical timer already exists */ |
109 | } else { |
110 | add234(timer_contexts, t->ctx);/* don't care if this fails */ |
111 | } |
112 | |
113 | first = (struct timer *)index234(timers, 0); |
114 | if (first == t) { |
115 | /* |
116 | * This timer is the very first on the list, so we must |
117 | * notify the front end. |
118 | */ |
119 | timer_change_notify(first->now); |
120 | } |
121 | |
122 | return when; |
123 | } |
124 | |
125 | /* |
126 | * Call to run any timers whose time has reached the present. |
127 | * Returns the time (in ticks) expected until the next timer after |
128 | * that triggers. |
129 | */ |
130 | int run_timers(long anow, long *next) |
131 | { |
132 | struct timer *first; |
133 | |
134 | init_timers(); |
135 | |
136 | now = anow; |
137 | |
138 | while (1) { |
139 | first = (struct timer *)index234(timers, 0); |
140 | |
141 | if (!first) |
142 | return FALSE; /* no timers remaining */ |
143 | |
144 | if (find234(timer_contexts, first->ctx, NULL) == NULL) { |
145 | /* |
146 | * This timer belongs to a context that has been |
147 | * expired. Delete it without running. |
148 | */ |
149 | delpos234(timers, 0); |
150 | sfree(first); |
151 | } else if (first->now - now <= 0) { |
152 | /* |
153 | * This timer is active and has reached its running |
154 | * time. Run it. |
155 | */ |
156 | delpos234(timers, 0); |
157 | first->fn(first->ctx, first->now); |
158 | sfree(first); |
159 | } else { |
160 | /* |
161 | * This is the first still-active timer that is in the |
162 | * future. Return how long it has yet to go. |
163 | */ |
164 | *next = first->now; |
165 | return TRUE; |
166 | } |
167 | } |
168 | } |
169 | |
170 | /* |
171 | * Call to expire all timers associated with a given context. |
172 | */ |
173 | void expire_timer_context(void *ctx) |
174 | { |
175 | init_timers(); |
176 | |
177 | /* |
178 | * We don't bother to check the return value; if the context |
179 | * already wasn't in the tree (presumably because no timers |
180 | * ever actually got scheduled for it) then that's fine and we |
181 | * simply don't need to do anything. |
182 | */ |
183 | del234(timer_contexts, ctx); |
184 | } |