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.
24 static tree234
*timers
= NULL
;
27 static int compare_timers(void *av
, void *bv
)
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
;
40 * Failing that, compare on the other two fields, just so that
41 * we don't get unwanted equality.
44 /* lcc won't let us compare function pointers. Legal, but annoying. */
46 int c
= memcmp(&a
->fn
, &b
->fn
, sizeof(a
->fn
));
55 else if (a
->fn
> b
->fn
)
61 else if (a
->ctx
> b
->ctx
)
65 * Failing _that_, the two entries genuinely are equal, and we
66 * never have a need to store them separately in the tree.
71 static int compare_timer_contexts(void *av
, void *bv
)
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
;
84 static void init_timers(void)
87 timers
= newtree234(compare_timers
);
92 long schedule_timer(int ticks
, timer_fn_t fn
, void *ctx
)
95 struct timer
*t
, *first
;
99 when
= ticks
+ GETTICKCOUNT();
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.
109 t
= snew(struct timer
);
114 if (t
!= add234(timers
, t
)) {
115 sfree(t
); /* identical timer already exists */
118 first
= (struct timer
*)index234(timers
, 0);
121 * This timer is the very first on the list, so we must
122 * notify the front end.
124 timer_change_notify(first
->now
);
131 * Call to run any timers whose time has reached the present.
132 * Returns the time (in ticks) expected until the next timer after
135 int run_timers(long anow
, long *next
)
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.
151 long tnow
= GETTICKCOUNT();
153 if (tnow
+ TICKSPERSEC
/50 - anow
< 0 ||
154 anow
+ TICKSPERSEC
/50 - tnow
< 0
156 #if defined TIMING_SYNC_ANOW
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.
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.
171 tickcount_offset
+= anow
- tnow
;
172 #elif defined TIMING_SYNC_TICKCOUNT
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.
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.
187 #error TIMING_SYNC definition incomplete
196 first
= (struct timer
*)index234(timers
, 0);
199 return FALSE
; /* no timers remaining */
201 if (first
->now
- now
<= 0) {
203 * This timer is active and has reached its running
206 delpos234(timers
, 0);
207 first
->fn(first
->ctx
, first
->now
);
211 * This is the first still-active timer that is in the
212 * future. Return how long it has yet to go.
221 * Call to expire all timers associated with a given context.
223 void expire_timer_context(void *ctx
)
226 struct timer exemplar
;
231 /* don't care about initialisation of other members */
233 /* Dispose of all timers with this context */
234 while ((ptr
= (struct timer
*)find234(timers
, &exemplar
,
235 compare_timer_contexts
))) {
240 /* Dispose of timer tree itself if none are left */
241 if (count234(timers
) == 0) {