[lib] Bug #134: threads should be more robust against backward time jumps

2006-08-25 Paul Jakma <paul.jakma@sun.com>

	* thread.c: (general) Add support for monotonic clock, it may still
	  jump forward by huge amounts, but should be immune to going
	  backwards. Fixes bug #134.
	  (quagga_gettimeofday_relative_adjust) helper, does what name
	  says - adjusts gettimeofday based relative timer.
	  (quagga_gettimeofday) helper to keep recent_time up to date.
	  (quagga_get_relative) helper, update and getch the relative
	  timer using gettimeofday(). POSIX CLOCK_MONOTONIC is also
	  supported, but the code is not enabled yet nor tested.
	  (quagga_real_stabilised) helper, retrieve absolute time but
	  stabilised so as to never decrease.
	  (quagga_gettime) Exported interface, analogous to POSIX
	  clock_gettime() in interface, supporting several clocks.
	  (quagga_time) Exported interface, analogous to traditional
	  time(), will never decrease.
	  (recent_relative_time) Convenience function to retrieve
	  relative_time timeval, similar to existing recent_time absolute
	  timeval, for when an approximately recent value will do.
	  (remainder) Update to use above helpers.
	  (thread_getrusage) Previously was a macro, but needs to be
	  a function to twiddle with thread.c private stuff.
	* thread.c: Point the GETRUSAGE macro at previous function.
	  Export quagga_gettime, quagga_time and recent_relative_time for
	  general use.
diff --git a/lib/thread.h b/lib/thread.h
index 0670a89..1c324d8 100644
--- a/lib/thread.h
+++ b/lib/thread.h
@@ -31,14 +31,7 @@
 };
 #define RUSAGE_T        struct rusage_t
 
-#ifdef HAVE_RUSAGE
-#define GETRUSAGE(X)	\
-  getrusage(RUSAGE_SELF, &((X)->cpu));	\
-  gettimeofday(&recent_time, NULL); (X)->real = recent_time
-#else
-#define GETRUSAGE(X)	\
-  gettimeofday(&recent_time, NULL); (X)->real = recent_time
-#endif /* HAVE_RUSAGE */
+#define GETRUSAGE(X) thread_getrusage(X)
 
 /* Linked list of thread. */
 struct thread_list
@@ -99,6 +92,13 @@
   unsigned char types;
 };
 
+/* Clocks supported by Quagga */
+enum quagga_clkid {
+  QUAGGA_CLK_REALTIME = 0,	/* ala gettimeofday() */
+  QUAGGA_CLK_MONOTONIC,		/* monotonic, against an indeterminate base */
+  QUAGGA_CLK_REALTIME_STABILISED, /* like realtime, but non-decrementing */
+};
+
 /* Thread types. */
 #define THREAD_READ           0
 #define THREAD_WRITE          1
@@ -192,8 +192,17 @@
 extern unsigned long thread_timer_remain_second (struct thread *);
 extern int thread_should_yield (struct thread *);
 
+/* Internal libzebra exports */
+extern void thread_getrusage (RUSAGE_T *);
 extern struct cmd_element show_thread_cpu_cmd;
 
+/* replacements for the system gettimeofday(), clock_gettime() and
+ * time() functions, providing support for non-decrementing clock on
+ * all systems, and fully monotonic on /some/ systems.
+ */
+extern int quagga_gettime (enum quagga_clkid, struct timeval *);
+extern time_t quagga_time (time_t *);
+
 /* Returns elapsed real (wall clock) time. */
 extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before,
 					  unsigned long *cpu_time_elapsed);
@@ -202,5 +211,6 @@
    be used instead of calling gettimeofday if a recent value is sufficient.
    This is guaranteed to be refreshed before a thread is called. */
 extern struct timeval recent_time;
-
+/* Similar to recent_time, but a monotonically increasing time value */
+extern struct timeval recent_relative_time (void);
 #endif /* _ZEBRA_THREAD_H */