Render timing.c robust in the face of strangeness. The strangenesses
[u/mdw/putty] / timing.c
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 */
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
54 if (a->fn < b->fn)
55 return -1;
56 else if (a->fn > b->fn)
57 return +1;
58 #endif
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
101 /*
102 * Just in case our various defences against timing skew fail
103 * us: if we try to schedule a timer that's already in the
104 * past, we instead schedule it for the immediate future.
105 */
106 if (when - now <= 0)
107 when = now + 1;
108
109 t = snew(struct timer);
110 t->fn = fn;
111 t->ctx = ctx;
112 t->now = when;
113
114 if (t != add234(timers, t)) {
115 sfree(t); /* identical timer already exists */
116 } else {
117 add234(timer_contexts, t->ctx);/* don't care if this fails */
118 }
119
120 first = (struct timer *)index234(timers, 0);
121 if (first == t) {
122 /*
123 * This timer is the very first on the list, so we must
124 * notify the front end.
125 */
126 timer_change_notify(first->now);
127 }
128
129 return when;
130 }
131
132 /*
133 * Call to run any timers whose time has reached the present.
134 * Returns the time (in ticks) expected until the next timer after
135 * that triggers.
136 */
137 int run_timers(long anow, long *next)
138 {
139 struct timer *first;
140
141 init_timers();
142
143 #ifdef TIMING_SYNC
144 /*
145 * In this ifdef I put some code which deals with the
146 * possibility that `anow' disagrees with GETTICKCOUNT by a
147 * significant margin. Our strategy for dealing with it differs
148 * depending on platform, because on some platforms
149 * GETTICKCOUNT is more likely to be right whereas on others
150 * `anow' is a better gold standard.
151 */
152 {
153 long tnow = GETTICKCOUNT();
154
155 if (tnow + TICKSPERSEC/50 - anow < 0 ||
156 anow + TICKSPERSEC/50 - tnow < 0
157 ) {
158 #if defined TIMING_SYNC_ANOW
159 /*
160 * If anow is accurate and the tick count is wrong,
161 * this is likely to be because the tick count is
162 * derived from the system clock which has changed (as
163 * can occur on Unix). Therefore, we resolve this by
164 * inventing an offset which is used to adjust all
165 * future output from GETTICKCOUNT.
166 *
167 * A platform which defines TIMING_SYNC_ANOW is
168 * expected to have also defined this offset variable
169 * in (its platform-specific adjunct to) putty.h.
170 * Therefore we can simply reference it here and assume
171 * that it will exist.
172 */
173 tickcount_offset += anow - tnow;
174 #elif defined TIMING_SYNC_TICKCOUNT
175 /*
176 * If the tick count is more likely to be accurate, we
177 * simply use that as our time value, which may mean we
178 * run no timers in this call (because we got called
179 * early), or alternatively it may mean we run lots of
180 * timers in a hurry because we were called late.
181 */
182 anow = tnow;
183 #else
184 /*
185 * Any platform which defines TIMING_SYNC must also define one of the two
186 * auxiliary symbols TIMING_SYNC_ANOW and TIMING_SYNC_TICKCOUNT, to
187 * indicate which measurement to trust when the two disagree.
188 */
189 #error TIMING_SYNC definition incomplete
190 #endif
191 }
192 }
193 #endif
194
195 now = anow;
196
197 while (1) {
198 first = (struct timer *)index234(timers, 0);
199
200 if (!first)
201 return FALSE; /* no timers remaining */
202
203 if (find234(timer_contexts, first->ctx, NULL) == NULL) {
204 /*
205 * This timer belongs to a context that has been
206 * expired. Delete it without running.
207 */
208 delpos234(timers, 0);
209 sfree(first);
210 } else if (first->now - now <= 0) {
211 /*
212 * This timer is active and has reached its running
213 * time. Run it.
214 */
215 delpos234(timers, 0);
216 first->fn(first->ctx, first->now);
217 sfree(first);
218 } else {
219 /*
220 * This is the first still-active timer that is in the
221 * future. Return how long it has yet to go.
222 */
223 *next = first->now;
224 return TRUE;
225 }
226 }
227 }
228
229 /*
230 * Call to expire all timers associated with a given context.
231 */
232 void expire_timer_context(void *ctx)
233 {
234 init_timers();
235
236 /*
237 * We don't bother to check the return value; if the context
238 * already wasn't in the tree (presumably because no timers
239 * ever actually got scheduled for it) then that's fine and we
240 * simply don't need to do anything.
241 */
242 del234(timer_contexts, ctx);
243 }