From 5c4d218936559af2f6cb9857af7f5b9c8d83be6d Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 26 Nov 2010 22:46:26 +0100 Subject: [PATCH] =?utf8?q?src/utils=5Ftime.[ch]:=20Implement=20work-around?= =?utf8?q?=20for=20Mac=20OS=20X=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit … which, apparently, doesn't have clock_gettime(2). --- configure.in | 25 ++++++++++++++++++------- src/utils_time.c | 20 ++++++++++++++++++++ src/utils_time.h | 4 ++-- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/configure.in b/configure.in index fd6a2570..3b46188a 100644 --- a/configure.in +++ b/configure.in @@ -574,13 +574,24 @@ AM_CONDITIONAL(BUILD_WITH_LIBSOCKET, test "x$socket_needs_socket" = "xyes") clock_gettime_needs_rt="no" clock_gettime_needs_posix4="no" -AC_CHECK_FUNCS(clock_gettime, - [], - AC_CHECK_LIB(rt, clock_gettime, - [clock_gettime_needs_rt="yes"], - AC_CHECK_LIB(posix4, clock_gettime, - [clock_gettime_needs_posix4="yes"], - AC_MSG_ERROR(cannot find clock_gettime)))) +have_clock_gettime="no" +AC_CHECK_FUNCS(clock_gettime, [have_clock_gettime="yes"]) +if test "x$have_clock_gettime" = "xno" +then + AC_CHECK_LIB(rt, clock_gettime, [clock_gettime_needs_rt="yes" + have_clock_gettime="yes"]) +fi +if test "x$have_clock_gettime" = "xno" +then + AC_CHECK_LIB(posix4, clock_gettime, [clock_gettime_needs_posix4="yes" + have_clock_gettime="yes"]) +fi +if test "x$have_clock_gettime" = "xyes" +then + AC_DEFINE(HAVE_CLOCK_GETTIME, 1, [Define if the clock_gettime(2) function is available.]) +else + AC_MSG_WARN(cannot find clock_gettime) +fi nanosleep_needs_rt="no" nanosleep_needs_posix4="no" diff --git a/src/utils_time.c b/src/utils_time.c index 420b425c..aac6135e 100644 --- a/src/utils_time.c +++ b/src/utils_time.c @@ -24,6 +24,7 @@ #include "plugin.h" #include "common.h" +#if HAVE_CLOCK_GETTIME cdtime_t cdtime (void) /* {{{ */ { int status; @@ -40,5 +41,24 @@ cdtime_t cdtime (void) /* {{{ */ return (TIMESPEC_TO_CDTIME_T (&ts)); } /* }}} cdtime_t cdtime */ +#else +/* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */ +cdtime_t cdtime (void) /* {{{ */ +{ + int status; + struct timeval tv = { 0, 0 }; + + status = gettimeofday (&tv, /* struct timezone = */ NULL); + if (status != 0) + { + char errbuf[1024]; + ERROR ("cdtime: gettimeofday failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (0); + } + + return (TIMEVAL_TO_CDTIME_T (&tv)); +} /* }}} cdtime_t cdtime */ +#endif /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/utils_time.h b/src/utils_time.h index da73492c..0fd809ac 100644 --- a/src/utils_time.h +++ b/src/utils_time.h @@ -54,8 +54,8 @@ (tvp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ (tvp)->tv_usec = CDTIME_T_TO_US ((cdt) % 1073741824); \ } while (0) -#define TIMEVAL_TO_CDTIME_T(tv) (TIME_T_TO_CDTIME_T ((tv).tv_sec) \ - + US_TO_CDTIME_T ((tv).tv_usec)) +#define TIMEVAL_TO_CDTIME_T(tv) (TIME_T_TO_CDTIME_T ((tv)->tv_sec) \ + + US_TO_CDTIME_T ((tv)->tv_usec)) #define CDTIME_T_TO_TIMESPEC(cdt,tsp) do { \ (tsp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ -- 2.11.0