Code

src/utils_time.[ch]: Implement work-around for Mac OS X …
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Fri, 26 Nov 2010 21:46:26 +0000 (22:46 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Fri, 26 Nov 2010 21:46:26 +0000 (22:46 +0100)
… which, apparently, doesn't have clock_gettime(2).

configure.in
src/utils_time.c
src/utils_time.h

index fd6a25701e5c5a41c416ac53dab941f5001caee0..3b46188a3e73ea9ac4cb7be49b3db4bd1dccc2d7 100644 (file)
@@ -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"
index 420b425c0bd1a2739fa398f292062867f57f3144..aac6135e28f8bf7d1be5966d5316a64c28200e93 100644 (file)
@@ -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 : */
index da73492cc922b785b0677df828180a1b9d5ac0c6..0fd809ac5de49327044102a9eabd9b28820abdc9 100644 (file)
@@ -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);                                  \