--- /dev/null
+/*\r
+ * timing.c\r
+ * \r
+ * This module tracks any timers set up by schedule_timer(). It\r
+ * keeps all the currently active timers in a list; it informs the\r
+ * front end of when the next timer is due to go off if that\r
+ * changes; and, very importantly, it tracks the context pointers\r
+ * passed to schedule_timer(), so that if a context is freed all\r
+ * the timers associated with it can be immediately annulled.\r
+ */\r
+\r
+#include <assert.h>\r
+#include <stdio.h>\r
+\r
+#include "putty.h"\r
+#include "tree234.h"\r
+\r
+struct timer {\r
+ timer_fn_t fn;\r
+ void *ctx;\r
+ long now;\r
+};\r
+\r
+static tree234 *timers = NULL;\r
+static tree234 *timer_contexts = NULL;\r
+static long now = 0L;\r
+\r
+static int compare_timers(void *av, void *bv)\r
+{\r
+ struct timer *a = (struct timer *)av;\r
+ struct timer *b = (struct timer *)bv;\r
+ long at = a->now - now;\r
+ long bt = b->now - now;\r
+\r
+ if (at < bt)\r
+ return -1;\r
+ else if (at > bt)\r
+ return +1;\r
+\r
+ /*\r
+ * Failing that, compare on the other two fields, just so that\r
+ * we don't get unwanted equality.\r
+ */\r
+#ifdef __LCC__\r
+ /* lcc won't let us compare function pointers. Legal, but annoying. */\r
+ {\r
+ int c = memcmp(&a->fn, &b->fn, sizeof(a->fn));\r
+ if (c < 0)\r
+ return -1;\r
+ else if (c > 0)\r
+ return +1;\r
+ }\r
+#else \r
+ if (a->fn < b->fn)\r
+ return -1;\r
+ else if (a->fn > b->fn)\r
+ return +1;\r
+#endif\r
+\r
+ if (a->ctx < b->ctx)\r
+ return -1;\r
+ else if (a->ctx > b->ctx)\r
+ return +1;\r
+\r
+ /*\r
+ * Failing _that_, the two entries genuinely are equal, and we\r
+ * never have a need to store them separately in the tree.\r
+ */\r
+ return 0;\r
+}\r
+\r
+static int compare_timer_contexts(void *av, void *bv)\r
+{\r
+ char *a = (char *)av;\r
+ char *b = (char *)bv;\r
+ if (a < b)\r
+ return -1;\r
+ else if (a > b)\r
+ return +1;\r
+ return 0;\r
+}\r
+\r
+static void init_timers(void)\r
+{\r
+ if (!timers) {\r
+ timers = newtree234(compare_timers);\r
+ timer_contexts = newtree234(compare_timer_contexts);\r
+ now = GETTICKCOUNT();\r
+ }\r
+}\r
+\r
+long schedule_timer(int ticks, timer_fn_t fn, void *ctx)\r
+{\r
+ long when;\r
+ struct timer *t, *first;\r
+\r
+ init_timers();\r
+\r
+ when = ticks + GETTICKCOUNT();\r
+\r
+ /*\r
+ * Just in case our various defences against timing skew fail\r
+ * us: if we try to schedule a timer that's already in the\r
+ * past, we instead schedule it for the immediate future.\r
+ */\r
+ if (when - now <= 0)\r
+ when = now + 1;\r
+\r
+ t = snew(struct timer);\r
+ t->fn = fn;\r
+ t->ctx = ctx;\r
+ t->now = when;\r
+\r
+ if (t != add234(timers, t)) {\r
+ sfree(t); /* identical timer already exists */\r
+ } else {\r
+ add234(timer_contexts, t->ctx);/* don't care if this fails */\r
+ }\r
+\r
+ first = (struct timer *)index234(timers, 0);\r
+ if (first == t) {\r
+ /*\r
+ * This timer is the very first on the list, so we must\r
+ * notify the front end.\r
+ */\r
+ timer_change_notify(first->now);\r
+ }\r
+\r
+ return when;\r
+}\r
+\r
+/*\r
+ * Call to run any timers whose time has reached the present.\r
+ * Returns the time (in ticks) expected until the next timer after\r
+ * that triggers.\r
+ */\r
+int run_timers(long anow, long *next)\r
+{\r
+ struct timer *first;\r
+\r
+ init_timers();\r
+\r
+#ifdef TIMING_SYNC\r
+ /*\r
+ * In this ifdef I put some code which deals with the\r
+ * possibility that `anow' disagrees with GETTICKCOUNT by a\r
+ * significant margin. Our strategy for dealing with it differs\r
+ * depending on platform, because on some platforms\r
+ * GETTICKCOUNT is more likely to be right whereas on others\r
+ * `anow' is a better gold standard.\r
+ */\r
+ {\r
+ long tnow = GETTICKCOUNT();\r
+\r
+ if (tnow + TICKSPERSEC/50 - anow < 0 ||\r
+ anow + TICKSPERSEC/50 - tnow < 0\r
+ ) {\r
+#if defined TIMING_SYNC_ANOW\r
+ /*\r
+ * If anow is accurate and the tick count is wrong,\r
+ * this is likely to be because the tick count is\r
+ * derived from the system clock which has changed (as\r
+ * can occur on Unix). Therefore, we resolve this by\r
+ * inventing an offset which is used to adjust all\r
+ * future output from GETTICKCOUNT.\r
+ * \r
+ * A platform which defines TIMING_SYNC_ANOW is\r
+ * expected to have also defined this offset variable\r
+ * in (its platform-specific adjunct to) putty.h.\r
+ * Therefore we can simply reference it here and assume\r
+ * that it will exist.\r
+ */\r
+ tickcount_offset += anow - tnow;\r
+#elif defined TIMING_SYNC_TICKCOUNT\r
+ /*\r
+ * If the tick count is more likely to be accurate, we\r
+ * simply use that as our time value, which may mean we\r
+ * run no timers in this call (because we got called\r
+ * early), or alternatively it may mean we run lots of\r
+ * timers in a hurry because we were called late.\r
+ */\r
+ anow = tnow;\r
+#else\r
+/*\r
+ * Any platform which defines TIMING_SYNC must also define one of the two\r
+ * auxiliary symbols TIMING_SYNC_ANOW and TIMING_SYNC_TICKCOUNT, to\r
+ * indicate which measurement to trust when the two disagree.\r
+ */\r
+#error TIMING_SYNC definition incomplete\r
+#endif\r
+ }\r
+ }\r
+#endif\r
+\r
+ now = anow;\r
+\r
+ while (1) {\r
+ first = (struct timer *)index234(timers, 0);\r
+\r
+ if (!first)\r
+ return FALSE; /* no timers remaining */\r
+\r
+ if (find234(timer_contexts, first->ctx, NULL) == NULL) {\r
+ /*\r
+ * This timer belongs to a context that has been\r
+ * expired. Delete it without running.\r
+ */\r
+ delpos234(timers, 0);\r
+ sfree(first);\r
+ } else if (first->now - now <= 0) {\r
+ /*\r
+ * This timer is active and has reached its running\r
+ * time. Run it.\r
+ */\r
+ delpos234(timers, 0);\r
+ first->fn(first->ctx, first->now);\r
+ sfree(first);\r
+ } else {\r
+ /*\r
+ * This is the first still-active timer that is in the\r
+ * future. Return how long it has yet to go.\r
+ */\r
+ *next = first->now;\r
+ return TRUE;\r
+ }\r
+ }\r
+}\r
+\r
+/*\r
+ * Call to expire all timers associated with a given context.\r
+ */\r
+void expire_timer_context(void *ctx)\r
+{\r
+ init_timers();\r
+\r
+ /*\r
+ * We don't bother to check the return value; if the context\r
+ * already wasn't in the tree (presumably because no timers\r
+ * ever actually got scheduled for it) then that's fine and we\r
+ * simply don't need to do anything.\r
+ */\r
+ del234(timer_contexts, ctx);\r
+}\r