Change expiry of timers when their contexts go away from lazy to eager.
[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 long now = 0L;
26
27 static int compare_timers(void *av, void *bv)
28 {
29 struct timer *a = (struct timer *)av;
30 struct timer *b = (struct timer *)bv;
31 long at = a->now - now;
32 long bt = b->now - now;
33
34 if (at < bt)
35 return -1;
36 else if (at > bt)
37 return +1;
38
39 /*
40 * Failing that, compare on the other two fields, just so that
41 * we don't get unwanted equality.
42 */
43 #ifdef __LCC__
44 /* lcc won't let us compare function pointers. Legal, but annoying. */
45 {
46 int c = memcmp(&a->fn, &b->fn, sizeof(a->fn));
47 if (c < 0)
48 return -1;
49 else if (c > 0)
50 return +1;
51 }
52 #else
53 if (a->fn < b->fn)
54 return -1;
55 else if (a->fn > b->fn)
56 return +1;
57 #endif
58
59 if (a->ctx < b->ctx)
60 return -1;
61 else if (a->ctx > b->ctx)
62 return +1;
63
64 /*
65 * Failing _that_, the two entries genuinely are equal, and we
66 * never have a need to store them separately in the tree.
67 */
68 return 0;
69 }
70
71 static int compare_timer_contexts(void *av, void *bv)
72 {
73 struct timer *at = (struct timer *)av;
74 struct timer *bt = (struct timer *)bv;
75 char *a = (char *)at->ctx;
76 char *b = (char *)bt->ctx;
77 if (a < b)
78 return -1;
79 else if (a > b)
80 return +1;
81 return 0;
82 }
83
84 static void init_timers(void)
85 {
86 if (!timers) {
87 timers = newtree234(compare_timers);
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 }
117
118 first = (struct timer *)index234(timers, 0);
119 if (first == t) {
120 /*
121 * This timer is the very first on the list, so we must
122 * notify the front end.
123 */
124 timer_change_notify(first->now);
125 }
126
127 return when;
128 }
129
130 /*
131 * Call to run any timers whose time has reached the present.
132 * Returns the time (in ticks) expected until the next timer after
133 * that triggers.
134 */
135 int run_timers(long anow, long *next)
136 {
137 struct timer *first;
138
139 init_timers();
140
141 #ifdef TIMING_SYNC
142 /*
143 * In this ifdef I put some code which deals with the
144 * possibility that `anow' disagrees with GETTICKCOUNT by a
145 * significant margin. Our strategy for dealing with it differs
146 * depending on platform, because on some platforms
147 * GETTICKCOUNT is more likely to be right whereas on others
148 * `anow' is a better gold standard.
149 */
150 {
151 long tnow = GETTICKCOUNT();
152
153 if (tnow + TICKSPERSEC/50 - anow < 0 ||
154 anow + TICKSPERSEC/50 - tnow < 0
155 ) {
156 #if defined TIMING_SYNC_ANOW
157 /*
158 * If anow is accurate and the tick count is wrong,
159 * this is likely to be because the tick count is
160 * derived from the system clock which has changed (as
161 * can occur on Unix). Therefore, we resolve this by
162 * inventing an offset which is used to adjust all
163 * future output from GETTICKCOUNT.
164 *
165 * A platform which defines TIMING_SYNC_ANOW is
166 * expected to have also defined this offset variable
167 * in (its platform-specific adjunct to) putty.h.
168 * Therefore we can simply reference it here and assume
169 * that it will exist.
170 */
171 tickcount_offset += anow - tnow;
172 #elif defined TIMING_SYNC_TICKCOUNT
173 /*
174 * If the tick count is more likely to be accurate, we
175 * simply use that as our time value, which may mean we
176 * run no timers in this call (because we got called
177 * early), or alternatively it may mean we run lots of
178 * timers in a hurry because we were called late.
179 */
180 anow = tnow;
181 #else
182 /*
183 * Any platform which defines TIMING_SYNC must also define one of the two
184 * auxiliary symbols TIMING_SYNC_ANOW and TIMING_SYNC_TICKCOUNT, to
185 * indicate which measurement to trust when the two disagree.
186 */
187 #error TIMING_SYNC definition incomplete
188 #endif
189 }
190 }
191 #endif
192
193 now = anow;
194
195 while (1) {
196 first = (struct timer *)index234(timers, 0);
197
198 if (!first)
199 return FALSE; /* no timers remaining */
200
201 if (first->now - now <= 0) {
202 /*
203 * This timer is active and has reached its running
204 * time. Run it.
205 */
206 delpos234(timers, 0);
207 first->fn(first->ctx, first->now);
208 sfree(first);
209 } else {
210 /*
211 * This is the first still-active timer that is in the
212 * future. Return how long it has yet to go.
213 */
214 *next = first->now;
215 return TRUE;
216 }
217 }
218 }
219
220 /*
221 * Call to expire all timers associated with a given context.
222 */
223 void expire_timer_context(void *ctx)
224 {
225 struct timer *ptr;
226 struct timer exemplar;
227
228 if (!timers) return;
229
230 exemplar.ctx = ctx;
231 /* don't care about initialisation of other members */
232
233 /* Dispose of all timers with this context */
234 while ((ptr = (struct timer *)find234(timers, &exemplar,
235 compare_timer_contexts))) {
236 del234(timers, ptr);
237 sfree(ptr);
238 }
239
240 /* Dispose of timer tree itself if none are left */
241 if (count234(timers) == 0) {
242 freetree234(timers);
243 timers = NULL;
244 }
245 }