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