New timing infrastructure. There's a new function schedule_timer()
[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 if (a->fn < b->fn)
45 return -1;
46 else if (a->fn > b->fn)
47 return +1;
48
49 if (a->ctx < b->ctx)
50 return -1;
51 else if (a->ctx > b->ctx)
52 return +1;
53
54 /*
55 * Failing _that_, the two entries genuinely are equal, and we
56 * never have a need to store them separately in the tree.
57 */
58 return 0;
59 }
60
61 static int compare_timer_contexts(void *av, void *bv)
62 {
63 char *a = (char *)av;
64 char *b = (char *)bv;
65 if (a < b)
66 return -1;
67 else if (a > b)
68 return +1;
69 return 0;
70 }
71
72 static void init_timers(void)
73 {
74 if (!timers) {
75 timers = newtree234(compare_timers);
76 timer_contexts = newtree234(compare_timer_contexts);
77 now = GETTICKCOUNT();
78 }
79 }
80
81 long schedule_timer(int ticks, timer_fn_t fn, void *ctx)
82 {
83 long when;
84 struct timer *t, *first;
85
86 init_timers();
87
88 when = ticks + GETTICKCOUNT();
89 assert(when - now > 0);
90
91 t = snew(struct timer);
92 t->fn = fn;
93 t->ctx = ctx;
94 t->now = when;
95
96 if (t != add234(timers, t)) {
97 sfree(t); /* identical timer already exists */
98 } else {
99 add234(timer_contexts, t->ctx);/* don't care if this fails */
100 }
101
102 first = (struct timer *)index234(timers, 0);
103 if (first == t) {
104 /*
105 * This timer is the very first on the list, so we must
106 * notify the front end.
107 */
108 timer_change_notify(first->now);
109 }
110
111 return when;
112 }
113
114 /*
115 * Call to run any timers whose time has reached the present.
116 * Returns the time (in ticks) expected until the next timer after
117 * that triggers.
118 */
119 int run_timers(long anow, long *next)
120 {
121 struct timer *first;
122
123 init_timers();
124
125 now = anow;
126
127 while (1) {
128 first = (struct timer *)index234(timers, 0);
129
130 if (!first)
131 return FALSE; /* no timers remaining */
132
133 if (find234(timer_contexts, first->ctx, NULL) == NULL) {
134 /*
135 * This timer belongs to a context that has been
136 * expired. Delete it without running.
137 */
138 delpos234(timers, 0);
139 sfree(first);
140 } else if (first->now - now <= 0) {
141 /*
142 * This timer is active and has reached its running
143 * time. Run it.
144 */
145 delpos234(timers, 0);
146 first->fn(first->ctx, first->now);
147 sfree(first);
148 } else {
149 /*
150 * This is the first still-active timer that is in the
151 * future. Return how long it has yet to go.
152 */
153 *next = first->now;
154 return TRUE;
155 }
156 }
157 }
158
159 /*
160 * Call to expire all timers associated with a given context.
161 */
162 void expire_timer_context(void *ctx)
163 {
164 init_timers();
165
166 /*
167 * We don't bother to check the return value; if the context
168 * already wasn't in the tree (presumably because no timers
169 * ever actually got scheduled for it) then that's fine and we
170 * simply don't need to do anything.
171 */
172 del234(timer_contexts, ctx);
173 }