From 514351b127fdbef94943fae45e84c74dd111ce49 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 1 Sep 2006 06:08:23 +0000 Subject: [PATCH] Improve clock_gettime check --- ChangeLog | 7 +++++++ configure.in | 19 +++++++++++++++++++ glib/gtimer.c | 20 +++++++++++--------- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4814833..da9bacb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2006-09-01 Matthias Clasen + + * configure.in: Check for CLOCK_MONOTONIC. + + * glib/gtimer.c: Only use clock_gettime if we + have a monotonic clock. + 2006-08-31 Matthias Clasen * configure.in: Add missing includes to a few test diff --git a/configure.in b/configure.in index 2d93544..66a4420 100644 --- a/configure.in +++ b/configure.in @@ -863,6 +863,25 @@ AC_CHECK_FUNCS(clock_gettime, [], [ ]) ]) +AC_CACHE_CHECK(for monotonic clocks, + glib_cv_monotonic_clock,AC_TRY_RUN([ +#include +#ifdef HAVE_UNISTD_H +#include +#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) diff --git a/glib/gtimer.c b/glib/gtimer.c index 8568881..b0bfab1 100644 --- a/glib/gtimer.c +++ b/glib/gtimer.c @@ -52,12 +52,16 @@ #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; -- 2.7.4