Clang, like LCC, objects to using '<' and '>' on function pointers.
[u/mdw/putty] / timing.c
CommitLineData
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.
c2eb6cb7 10 *
11 *
12 * The problem is that computer clocks aren't perfectly accurate.
13 * The GETTICKCOUNT function returns a 32bit number that normally
14 * increases by about 1000 every second. On windows this uses the PC's
15 * interrupt timer and so is only accurate to around 20ppm. On unix it's
16 * a value that's calculated from the current UTC time and so is in theory
17 * accurate in the long term but may jitter and jump in the short term.
18 *
19 * What PuTTY needs from these timers is simply a way of delaying the
20 * calling of a function for a little while, if it's occasionally called a
21 * little early or late that's not a problem. So to protect against clock
22 * jumps schedule_timer records the time that it was called in the timer
23 * structure. With this information the run_timers function can see when
24 * the current GETTICKCOUNT value is after the time the event should be
25 * fired OR before the time it was set. In the latter case the clock must
26 * have jumped, the former is (probably) just the normal passage of time.
27 *
39934deb 28 */
29
30#include <assert.h>
31#include <stdio.h>
32
33#include "putty.h"
34#include "tree234.h"
35
36struct timer {
37 timer_fn_t fn;
38 void *ctx;
39 long now;
c2eb6cb7 40 long when_set;
39934deb 41};
42
43static tree234 *timers = NULL;
1605c493 44static tree234 *timer_contexts = NULL;
39934deb 45static long now = 0L;
46
47static int compare_timers(void *av, void *bv)
48{
49 struct timer *a = (struct timer *)av;
50 struct timer *b = (struct timer *)bv;
51 long at = a->now - now;
52 long bt = b->now - now;
53
54 if (at < bt)
55 return -1;
56 else if (at > bt)
57 return +1;
58
59 /*
60 * Failing that, compare on the other two fields, just so that
61 * we don't get unwanted equality.
62 */
7cd7de02 63#if defined(__LCC__) || defined(__clang__)
8136216b 64 /* lcc won't let us compare function pointers. Legal, but annoying. */
7cd7de02 65 return memcmp(&a->fn, &b->fn, sizeof(a->fn));
8136216b 66#else
39934deb 67 if (a->fn < b->fn)
68 return -1;
69 else if (a->fn > b->fn)
70 return +1;
8136216b 71#endif
39934deb 72
73 if (a->ctx < b->ctx)
74 return -1;
75 else if (a->ctx > b->ctx)
76 return +1;
77
78 /*
79 * Failing _that_, the two entries genuinely are equal, and we
80 * never have a need to store them separately in the tree.
81 */
82 return 0;
83}
84
85static int compare_timer_contexts(void *av, void *bv)
86{
1605c493 87 char *a = (char *)av;
88 char *b = (char *)bv;
39934deb 89 if (a < b)
90 return -1;
91 else if (a > b)
92 return +1;
93 return 0;
94}
95
96static void init_timers(void)
97{
98 if (!timers) {
99 timers = newtree234(compare_timers);
1605c493 100 timer_contexts = newtree234(compare_timer_contexts);
39934deb 101 now = GETTICKCOUNT();
102 }
103}
104
105long schedule_timer(int ticks, timer_fn_t fn, void *ctx)
106{
107 long when;
108 struct timer *t, *first;
109
110 init_timers();
111
c2eb6cb7 112 now = GETTICKCOUNT();
113 when = ticks + now;
2ac3322e 114
115 /*
116 * Just in case our various defences against timing skew fail
117 * us: if we try to schedule a timer that's already in the
118 * past, we instead schedule it for the immediate future.
119 */
120 if (when - now <= 0)
121 when = now + 1;
39934deb 122
123 t = snew(struct timer);
124 t->fn = fn;
125 t->ctx = ctx;
126 t->now = when;
c2eb6cb7 127 t->when_set = now;
39934deb 128
129 if (t != add234(timers, t)) {
130 sfree(t); /* identical timer already exists */
1605c493 131 } else {
132 add234(timer_contexts, t->ctx);/* don't care if this fails */
39934deb 133 }
134
135 first = (struct timer *)index234(timers, 0);
136 if (first == t) {
137 /*
138 * This timer is the very first on the list, so we must
139 * notify the front end.
140 */
141 timer_change_notify(first->now);
142 }
143
144 return when;
145}
146
147/*
148 * Call to run any timers whose time has reached the present.
149 * Returns the time (in ticks) expected until the next timer after
150 * that triggers.
151 */
152int run_timers(long anow, long *next)
153{
154 struct timer *first;
155
156 init_timers();
157
c2eb6cb7 158 now = GETTICKCOUNT();
39934deb 159
160 while (1) {
161 first = (struct timer *)index234(timers, 0);
162
163 if (!first)
164 return FALSE; /* no timers remaining */
165
1605c493 166 if (find234(timer_contexts, first->ctx, NULL) == NULL) {
167 /*
168 * This timer belongs to a context that has been
169 * expired. Delete it without running.
170 */
171 delpos234(timers, 0);
172 sfree(first);
c2eb6cb7 173 } else if (first->now - now <= 0 ||
174 now - (first->when_set - 10) < 0) {
39934deb 175 /*
176 * This timer is active and has reached its running
177 * time. Run it.
178 */
179 delpos234(timers, 0);
180 first->fn(first->ctx, first->now);
181 sfree(first);
182 } else {
183 /*
184 * This is the first still-active timer that is in the
185 * future. Return how long it has yet to go.
186 */
187 *next = first->now;
188 return TRUE;
189 }
190 }
191}
192
193/*
194 * Call to expire all timers associated with a given context.
195 */
196void expire_timer_context(void *ctx)
197{
1605c493 198 init_timers();
5e376138 199
1605c493 200 /*
201 * We don't bother to check the return value; if the context
202 * already wasn't in the tree (presumably because no timers
203 * ever actually got scheduled for it) then that's fine and we
204 * simply don't need to do anything.
205 */
206 del234(timer_contexts, ctx);
39934deb 207}