Improve clock_gettime check
authorMatthias Clasen <matthiasc@src.gnome.org>
Fri, 1 Sep 2006 06:08:23 +0000 (06:08 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Fri, 1 Sep 2006 06:08:23 +0000 (06:08 +0000)
ChangeLog
configure.in
glib/gtimer.c

index 4814833..da9bacb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2006-09-01  Matthias Clasen  <mclasen@redhat.com>
+
+       * configure.in: Check for CLOCK_MONOTONIC.
+
+       * glib/gtimer.c: Only use clock_gettime if we
+       have a monotonic clock.
+
 2006-08-31  Matthias Clasen  <mclasen@redhat.com>
 
        * configure.in: Add missing includes to a few test
index 2d93544..66a4420 100644 (file)
@@ -863,6 +863,25 @@ AC_CHECK_FUNCS(clock_gettime, [], [
   ])
 ])
 
+AC_CACHE_CHECK(for monotonic clocks,
+    glib_cv_monotonic_clock,AC_TRY_RUN([
+#include <time.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+      int main() {
+#if defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 && defined(CLOCK_MONOTONIC)
+        return 0;
+#else
+        return 1;
+#endif
+      }],glib_cv_monotonic_clock=yes,glib_cv_monotonic_clock=no))
+
+GLIB_ASSERT_SET(glib_cv_monotonic_clock)
+if test "$glib_cv_monotonic_clock" = "yes"; then
+  AC_DEFINE(HAVE_MONOTONIC_CLOCK,1,[Have a monotonic clock])
+fi
+
 AC_CHECK_HEADERS(crt_externs.h)
 AC_CHECK_FUNCS(_NSGetEnviron)
 
index 8568881..b0bfab1 100644 (file)
 
 #define G_NSEC_PER_SEC 1000000000
 
+#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK) 
+#define USE_CLOCK_GETTIME 1
+#endif
+
 struct _GTimer
 {
 #ifdef G_OS_WIN32
   guint64 start;
   guint64 end;
-#elif HAVE_CLOCK_GETTIME 
+#elif USE_CLOCK_GETTIME 
   struct timespec start;
   struct timespec end;
   gint clock;
@@ -72,7 +76,7 @@ struct _GTimer
 #ifdef G_OS_WIN32
 #  define GETTIME(v)                           \
      GetSystemTimeAsFileTime ((FILETIME *)&v)
-#elif HAVE_CLOCK_GETTIME
+#elif USE_CLOCK_GETTIME
 #  define GETTIME(v)                           \
      clock_gettime (posix_clock, &v)
 #else
@@ -80,7 +84,7 @@ struct _GTimer
      gettimeofday (&v, NULL)
 #endif
 
-#ifdef HAVE_CLOCK_GETTIME
+#ifdef USE_CLOCK_GETTIME
 static gint posix_clock = 0;
 
 static void
@@ -91,11 +95,9 @@ init_posix_clock (void)
   if (!initialized)
     {
       initialized = TRUE;
-#if !defined(_POSIX_MONOTONIC_CLOCK) || _POSIX_MONOTONIC_CLOCK >= 0
       if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
        posix_clock = CLOCK_MONOTONIC;
       else
-#endif
        posix_clock = CLOCK_REALTIME;
     }
 }
@@ -109,7 +111,7 @@ g_timer_new (void)
   timer = g_new (GTimer, 1);
   timer->active = TRUE;
 
-#ifdef HAVE_CLOCK_GETTIME
+#ifdef USE_CLOCK_GETTIME
   init_posix_clock ();
 #endif
 
@@ -159,7 +161,7 @@ g_timer_continue (GTimer *timer)
 {
 #ifdef G_OS_WIN32
   guint64 elapsed;
-#elif HAVE_CLOCK_GETTIME
+#elif USE_CLOCK_GETTIME
   struct timespec elapsed;
 #else
   struct timeval elapsed;
@@ -181,7 +183,7 @@ g_timer_continue (GTimer *timer)
 
   timer->start -= elapsed;
 
-#elif HAVE_CLOCK_GETTIME
+#elif USE_CLOCK_GETTIME
 
   if (timer->start.tv_nsec > timer->end.tv_nsec)
     {
@@ -237,7 +239,7 @@ g_timer_elapsed (GTimer *timer,
   gdouble total;
 #ifdef G_OS_WIN32
   gint64 elapsed;
-#elif HAVE_CLOCK_GETTIME
+#elif USE_CLOCK_GETTIME
   struct timespec elapsed;
 #else
   struct timeval elapsed;