OSDN Git Service

Modify documents.
[ffftp/ffftp.git] / putty / TIMING.C
1 /*\r
2  * timing.c\r
3  * \r
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
10  */\r
11 \r
12 #include <assert.h>\r
13 #include <stdio.h>\r
14 \r
15 #include "putty.h"\r
16 #include "tree234.h"\r
17 \r
18 struct timer {\r
19     timer_fn_t fn;\r
20     void *ctx;\r
21     long now;\r
22 };\r
23 \r
24 static tree234 *timers = NULL;\r
25 static tree234 *timer_contexts = NULL;\r
26 static long now = 0L;\r
27 \r
28 static int compare_timers(void *av, void *bv)\r
29 {\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
34 \r
35     if (at < bt)\r
36         return -1;\r
37     else if (at > bt)\r
38         return +1;\r
39 \r
40     /*\r
41      * Failing that, compare on the other two fields, just so that\r
42      * we don't get unwanted equality.\r
43      */\r
44 #ifdef __LCC__\r
45     /* lcc won't let us compare function pointers. Legal, but annoying. */\r
46     {\r
47         int c = memcmp(&a->fn, &b->fn, sizeof(a->fn));\r
48         if (c < 0)\r
49             return -1;\r
50         else if (c > 0)\r
51             return +1;\r
52     }\r
53 #else    \r
54     if (a->fn < b->fn)\r
55         return -1;\r
56     else if (a->fn > b->fn)\r
57         return +1;\r
58 #endif\r
59 \r
60     if (a->ctx < b->ctx)\r
61         return -1;\r
62     else if (a->ctx > b->ctx)\r
63         return +1;\r
64 \r
65     /*\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
68      */\r
69     return 0;\r
70 }\r
71 \r
72 static int compare_timer_contexts(void *av, void *bv)\r
73 {\r
74     char *a = (char *)av;\r
75     char *b = (char *)bv;\r
76     if (a < b)\r
77         return -1;\r
78     else if (a > b)\r
79         return +1;\r
80     return 0;\r
81 }\r
82 \r
83 static void init_timers(void)\r
84 {\r
85     if (!timers) {\r
86         timers = newtree234(compare_timers);\r
87         timer_contexts = newtree234(compare_timer_contexts);\r
88         now = GETTICKCOUNT();\r
89     }\r
90 }\r
91 \r
92 long schedule_timer(int ticks, timer_fn_t fn, void *ctx)\r
93 {\r
94     long when;\r
95     struct timer *t, *first;\r
96 \r
97     init_timers();\r
98 \r
99     when = ticks + GETTICKCOUNT();\r
100 \r
101     /*\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
105      */\r
106     if (when - now <= 0)\r
107         when = now + 1;\r
108 \r
109     t = snew(struct timer);\r
110     t->fn = fn;\r
111     t->ctx = ctx;\r
112     t->now = when;\r
113 \r
114     if (t != add234(timers, t)) {\r
115         sfree(t);                      /* identical timer already exists */\r
116     } else {\r
117         add234(timer_contexts, t->ctx);/* don't care if this fails */\r
118     }\r
119 \r
120     first = (struct timer *)index234(timers, 0);\r
121     if (first == t) {\r
122         /*\r
123          * This timer is the very first on the list, so we must\r
124          * notify the front end.\r
125          */\r
126         timer_change_notify(first->now);\r
127     }\r
128 \r
129     return when;\r
130 }\r
131 \r
132 /*\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
135  * that triggers.\r
136  */\r
137 int run_timers(long anow, long *next)\r
138 {\r
139     struct timer *first;\r
140 \r
141     init_timers();\r
142 \r
143 #ifdef TIMING_SYNC\r
144     /*\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
151      */\r
152     {\r
153         long tnow = GETTICKCOUNT();\r
154 \r
155         if (tnow + TICKSPERSEC/50 - anow < 0 ||\r
156             anow + TICKSPERSEC/50 - tnow < 0\r
157             ) {\r
158 #if defined TIMING_SYNC_ANOW\r
159             /*\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
166              * \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
172              */\r
173             tickcount_offset += anow - tnow;\r
174 #elif defined TIMING_SYNC_TICKCOUNT\r
175             /*\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
181              */\r
182             anow = tnow;\r
183 #else\r
184 /*\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
188  */\r
189 #error TIMING_SYNC definition incomplete\r
190 #endif\r
191         }\r
192     }\r
193 #endif\r
194 \r
195     now = anow;\r
196 \r
197     while (1) {\r
198         first = (struct timer *)index234(timers, 0);\r
199 \r
200         if (!first)\r
201             return FALSE;              /* no timers remaining */\r
202 \r
203         if (find234(timer_contexts, first->ctx, NULL) == NULL) {\r
204             /*\r
205              * This timer belongs to a context that has been\r
206              * expired. Delete it without running.\r
207              */\r
208             delpos234(timers, 0);\r
209             sfree(first);\r
210         } else if (first->now - now <= 0) {\r
211             /*\r
212              * This timer is active and has reached its running\r
213              * time. Run it.\r
214              */\r
215             delpos234(timers, 0);\r
216             first->fn(first->ctx, first->now);\r
217             sfree(first);\r
218         } else {\r
219             /*\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
222              */\r
223             *next = first->now;\r
224             return TRUE;\r
225         }\r
226     }\r
227 }\r
228 \r
229 /*\r
230  * Call to expire all timers associated with a given context.\r
231  */\r
232 void expire_timer_context(void *ctx)\r
233 {\r
234     init_timers();\r
235 \r
236     /*\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
241      */\r
242     del234(timer_contexts, ctx);\r
243 }\r