Improve clock_gettime check

This commit is contained in:
Matthias Clasen 2006-09-01 06:08:23 +00:00
parent 401b01f196
commit 514351b127
3 changed files with 37 additions and 9 deletions

View File

@ -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> 2006-08-31 Matthias Clasen <mclasen@redhat.com>
* configure.in: Add missing includes to a few test * configure.in: Add missing includes to a few test

View 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_HEADERS(crt_externs.h)
AC_CHECK_FUNCS(_NSGetEnviron) AC_CHECK_FUNCS(_NSGetEnviron)

View File

@ -52,12 +52,16 @@
#define G_NSEC_PER_SEC 1000000000 #define G_NSEC_PER_SEC 1000000000
#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
#define USE_CLOCK_GETTIME 1
#endif
struct _GTimer struct _GTimer
{ {
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
guint64 start; guint64 start;
guint64 end; guint64 end;
#elif HAVE_CLOCK_GETTIME #elif USE_CLOCK_GETTIME
struct timespec start; struct timespec start;
struct timespec end; struct timespec end;
gint clock; gint clock;
@ -72,7 +76,7 @@ struct _GTimer
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
# define GETTIME(v) \ # define GETTIME(v) \
GetSystemTimeAsFileTime ((FILETIME *)&v) GetSystemTimeAsFileTime ((FILETIME *)&v)
#elif HAVE_CLOCK_GETTIME #elif USE_CLOCK_GETTIME
# define GETTIME(v) \ # define GETTIME(v) \
clock_gettime (posix_clock, &v) clock_gettime (posix_clock, &v)
#else #else
@ -80,7 +84,7 @@ struct _GTimer
gettimeofday (&v, NULL) gettimeofday (&v, NULL)
#endif #endif
#ifdef HAVE_CLOCK_GETTIME #ifdef USE_CLOCK_GETTIME
static gint posix_clock = 0; static gint posix_clock = 0;
static void static void
@ -91,11 +95,9 @@ init_posix_clock (void)
if (!initialized) if (!initialized)
{ {
initialized = TRUE; initialized = TRUE;
#if !defined(_POSIX_MONOTONIC_CLOCK) || _POSIX_MONOTONIC_CLOCK >= 0
if (sysconf (_SC_MONOTONIC_CLOCK) >= 0) if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
posix_clock = CLOCK_MONOTONIC; posix_clock = CLOCK_MONOTONIC;
else else
#endif
posix_clock = CLOCK_REALTIME; posix_clock = CLOCK_REALTIME;
} }
} }
@ -109,7 +111,7 @@ g_timer_new (void)
timer = g_new (GTimer, 1); timer = g_new (GTimer, 1);
timer->active = TRUE; timer->active = TRUE;
#ifdef HAVE_CLOCK_GETTIME #ifdef USE_CLOCK_GETTIME
init_posix_clock (); init_posix_clock ();
#endif #endif
@ -159,7 +161,7 @@ g_timer_continue (GTimer *timer)
{ {
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
guint64 elapsed; guint64 elapsed;
#elif HAVE_CLOCK_GETTIME #elif USE_CLOCK_GETTIME
struct timespec elapsed; struct timespec elapsed;
#else #else
struct timeval elapsed; struct timeval elapsed;
@ -181,7 +183,7 @@ g_timer_continue (GTimer *timer)
timer->start -= elapsed; timer->start -= elapsed;
#elif HAVE_CLOCK_GETTIME #elif USE_CLOCK_GETTIME
if (timer->start.tv_nsec > timer->end.tv_nsec) if (timer->start.tv_nsec > timer->end.tv_nsec)
{ {
@ -237,7 +239,7 @@ g_timer_elapsed (GTimer *timer,
gdouble total; gdouble total;
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
gint64 elapsed; gint64 elapsed;
#elif HAVE_CLOCK_GETTIME #elif USE_CLOCK_GETTIME
struct timespec elapsed; struct timespec elapsed;
#else #else
struct timeval elapsed; struct timeval elapsed;