4 * This module tracks any timers set up by schedule_timer(). It
\r
5 * keeps all the currently active timers in a list; it informs the
\r
6 * front end of when the next timer is due to go off if that
\r
7 * changes; and, very importantly, it tracks the context pointers
\r
8 * passed to schedule_timer(), so that if a context is freed all
\r
9 * the timers associated with it can be immediately annulled.
\r
16 #include "tree234.h"
\r
24 static tree234 *timers = NULL;
\r
25 static tree234 *timer_contexts = NULL;
\r
26 static long now = 0L;
\r
28 static int compare_timers(void *av, void *bv)
\r
30 struct timer *a = (struct timer *)av;
\r
31 struct timer *b = (struct timer *)bv;
\r
32 long at = a->now - now;
\r
33 long bt = b->now - now;
\r
41 * Failing that, compare on the other two fields, just so that
\r
42 * we don't get unwanted equality.
\r
45 /* lcc won't let us compare function pointers. Legal, but annoying. */
\r
47 int c = memcmp(&a->fn, &b->fn, sizeof(a->fn));
\r
56 else if (a->fn > b->fn)
\r
60 if (a->ctx < b->ctx)
\r
62 else if (a->ctx > b->ctx)
\r
66 * Failing _that_, the two entries genuinely are equal, and we
\r
67 * never have a need to store them separately in the tree.
\r
72 static int compare_timer_contexts(void *av, void *bv)
\r
74 char *a = (char *)av;
\r
75 char *b = (char *)bv;
\r
83 static void init_timers(void)
\r
86 timers = newtree234(compare_timers);
\r
87 timer_contexts = newtree234(compare_timer_contexts);
\r
88 now = GETTICKCOUNT();
\r
92 long schedule_timer(int ticks, timer_fn_t fn, void *ctx)
\r
95 struct timer *t, *first;
\r
99 when = ticks + GETTICKCOUNT();
\r
102 * Just in case our various defences against timing skew fail
\r
103 * us: if we try to schedule a timer that's already in the
\r
104 * past, we instead schedule it for the immediate future.
\r
106 if (when - now <= 0)
\r
109 t = snew(struct timer);
\r
114 if (t != add234(timers, t)) {
\r
115 sfree(t); /* identical timer already exists */
\r
117 add234(timer_contexts, t->ctx);/* don't care if this fails */
\r
120 first = (struct timer *)index234(timers, 0);
\r
123 * This timer is the very first on the list, so we must
\r
124 * notify the front end.
\r
126 timer_change_notify(first->now);
\r
133 * Call to run any timers whose time has reached the present.
\r
134 * Returns the time (in ticks) expected until the next timer after
\r
137 int run_timers(long anow, long *next)
\r
139 struct timer *first;
\r
145 * In this ifdef I put some code which deals with the
\r
146 * possibility that `anow' disagrees with GETTICKCOUNT by a
\r
147 * significant margin. Our strategy for dealing with it differs
\r
148 * depending on platform, because on some platforms
\r
149 * GETTICKCOUNT is more likely to be right whereas on others
\r
150 * `anow' is a better gold standard.
\r
153 long tnow = GETTICKCOUNT();
\r
155 if (tnow + TICKSPERSEC/50 - anow < 0 ||
\r
156 anow + TICKSPERSEC/50 - tnow < 0
\r
158 #if defined TIMING_SYNC_ANOW
\r
160 * If anow is accurate and the tick count is wrong,
\r
161 * this is likely to be because the tick count is
\r
162 * derived from the system clock which has changed (as
\r
163 * can occur on Unix). Therefore, we resolve this by
\r
164 * inventing an offset which is used to adjust all
\r
165 * future output from GETTICKCOUNT.
\r
167 * A platform which defines TIMING_SYNC_ANOW is
\r
168 * expected to have also defined this offset variable
\r
169 * in (its platform-specific adjunct to) putty.h.
\r
170 * Therefore we can simply reference it here and assume
\r
171 * that it will exist.
\r
173 tickcount_offset += anow - tnow;
\r
174 #elif defined TIMING_SYNC_TICKCOUNT
\r
176 * If the tick count is more likely to be accurate, we
\r
177 * simply use that as our time value, which may mean we
\r
178 * run no timers in this call (because we got called
\r
179 * early), or alternatively it may mean we run lots of
\r
180 * timers in a hurry because we were called late.
\r
185 * Any platform which defines TIMING_SYNC must also define one of the two
\r
186 * auxiliary symbols TIMING_SYNC_ANOW and TIMING_SYNC_TICKCOUNT, to
\r
187 * indicate which measurement to trust when the two disagree.
\r
189 #error TIMING_SYNC definition incomplete
\r
198 first = (struct timer *)index234(timers, 0);
\r
201 return FALSE; /* no timers remaining */
\r
203 if (find234(timer_contexts, first->ctx, NULL) == NULL) {
\r
205 * This timer belongs to a context that has been
\r
206 * expired. Delete it without running.
\r
208 delpos234(timers, 0);
\r
210 } else if (first->now - now <= 0) {
\r
212 * This timer is active and has reached its running
\r
215 delpos234(timers, 0);
\r
216 first->fn(first->ctx, first->now);
\r
220 * This is the first still-active timer that is in the
\r
221 * future. Return how long it has yet to go.
\r
223 *next = first->now;
\r
230 * Call to expire all timers associated with a given context.
\r
232 void expire_timer_context(void *ctx)
\r
237 * We don't bother to check the return value; if the context
\r
238 * already wasn't in the tree (presumably because no timers
\r
239 * ever actually got scheduled for it) then that's fine and we
\r
240 * simply don't need to do anything.
\r
242 del234(timer_contexts, ctx);
\r