timing.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. * 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. *
  28. */
  29. #include <assert.h>
  30. #include <stdio.h>
  31. #include "putty.h"
  32. #include "tree234.h"
  33. struct timer {
  34. timer_fn_t fn;
  35. void *ctx;
  36. unsigned long now;
  37. unsigned long when_set;
  38. };
  39. static tree234 *timers = NULL;
  40. static tree234 *timer_contexts = NULL;
  41. static unsigned long now = 0L;
  42. static int compare_timers(void *av, void *bv)
  43. {
  44. struct timer *a = (struct timer *)av;
  45. struct timer *b = (struct timer *)bv;
  46. long at = a->now - now;
  47. long bt = b->now - now;
  48. if (at < bt)
  49. return -1;
  50. else if (at > bt)
  51. return +1;
  52. /*
  53. * Failing that, compare on the other two fields, just so that
  54. * we don't get unwanted equality.
  55. */
  56. #if defined(__LCC__) || defined(__clang__)
  57. /* lcc won't let us compare function pointers. Legal, but annoying. */
  58. {
  59. int c = memcmp(&a->fn, &b->fn, sizeof(a->fn));
  60. if (c)
  61. return c;
  62. }
  63. #else
  64. if (a->fn < b->fn)
  65. return -1;
  66. else if (a->fn > b->fn)
  67. return +1;
  68. #endif
  69. if (a->ctx < b->ctx)
  70. return -1;
  71. else if (a->ctx > b->ctx)
  72. return +1;
  73. /*
  74. * Failing _that_, the two entries genuinely are equal, and we
  75. * never have a need to store them separately in the tree.
  76. */
  77. return 0;
  78. }
  79. static int compare_timer_contexts(void *av, void *bv)
  80. {
  81. char *a = (char *)av;
  82. char *b = (char *)bv;
  83. if (a < b)
  84. return -1;
  85. else if (a > b)
  86. return +1;
  87. return 0;
  88. }
  89. static void init_timers(void)
  90. {
  91. if (!timers) {
  92. timers = newtree234(compare_timers);
  93. timer_contexts = newtree234(compare_timer_contexts);
  94. now = GETTICKCOUNT();
  95. }
  96. }
  97. unsigned long schedule_timer(int ticks, timer_fn_t fn, void *ctx)
  98. {
  99. unsigned long when;
  100. struct timer *t, *first;
  101. init_timers();
  102. now = GETTICKCOUNT();
  103. when = ticks + now;
  104. /*
  105. * Just in case our various defences against timing skew fail
  106. * us: if we try to schedule a timer that's already in the
  107. * past, we instead schedule it for the immediate future.
  108. */
  109. if (when - now <= 0)
  110. when = now + 1;
  111. t = snew(struct timer);
  112. t->fn = fn;
  113. t->ctx = ctx;
  114. t->now = when;
  115. t->when_set = now;
  116. if (t != add234(timers, t)) {
  117. sfree(t); /* identical timer already exists */
  118. } else {
  119. add234(timer_contexts, t->ctx);/* don't care if this fails */
  120. }
  121. first = (struct timer *)index234(timers, 0);
  122. if (first == t) {
  123. /*
  124. * This timer is the very first on the list, so we must
  125. * notify the front end.
  126. */
  127. timer_change_notify(first->now);
  128. }
  129. return when;
  130. }
  131. unsigned long timing_last_clock(void)
  132. {
  133. /*
  134. * Return the last value we stored in 'now'. In particular,
  135. * calling this just after schedule_timer returns the value of
  136. * 'now' that was used to decide when the timer you just set would
  137. * go off.
  138. */
  139. return now;
  140. }
  141. /*
  142. * Call to run any timers whose time has reached the present.
  143. * Returns the time (in ticks) expected until the next timer after
  144. * that triggers.
  145. */
  146. bool run_timers(unsigned long anow, unsigned long *next)
  147. {
  148. struct timer *first;
  149. init_timers();
  150. now = GETTICKCOUNT();
  151. while (1) {
  152. first = (struct timer *)index234(timers, 0);
  153. if (!first)
  154. return false; /* no timers remaining */
  155. if (find234(timer_contexts, first->ctx, NULL) == NULL) {
  156. /*
  157. * This timer belongs to a context that has been
  158. * expired. Delete it without running.
  159. */
  160. delpos234(timers, 0);
  161. sfree(first);
  162. } else if (now - (first->when_set - 10) >
  163. first->now - (first->when_set - 10)) {
  164. /*
  165. * This timer is active and has reached its running
  166. * time. Run it.
  167. */
  168. delpos234(timers, 0);
  169. first->fn(first->ctx, first->now);
  170. sfree(first);
  171. } else {
  172. /*
  173. * This is the first still-active timer that is in the
  174. * future. Return how long it has yet to go.
  175. */
  176. *next = first->now;
  177. return true;
  178. }
  179. }
  180. }
  181. /*
  182. * Call to expire all timers associated with a given context.
  183. */
  184. void expire_timer_context(void *ctx)
  185. {
  186. init_timers();
  187. /*
  188. * We don't bother to check the return value; if the context
  189. * already wasn't in the tree (presumably because no timers
  190. * ever actually got scheduled for it) then that's fine and we
  191. * simply don't need to do anything.
  192. */
  193. del234(timer_contexts, ctx);
  194. }