fallback to gettimeofday when monotonic clock is unavailable at run-time
authorYang Tse <yangsita@gmail.com>
Thu, 10 Jul 2008 07:16:45 +0000 (07:16 +0000)
committerYang Tse <yangsita@gmail.com>
Thu, 10 Jul 2008 07:16:45 +0000 (07:16 +0000)
src/curlutil.c
tests/libtest/testutil.c

index 56cfa53c14ec38b52123106bc73a7d18a9776e94..1712ce3c62d348064f9394f3cf234c7d81bf4721 100644 (file)
@@ -54,9 +54,24 @@ struct timeval cutil_tvnow(void)
   */
   struct timeval now;
   struct timespec tsnow;
-  (void)clock_gettime(CLOCK_MONOTONIC, &tsnow);
-  now.tv_sec = tsnow.tv_sec;
-  now.tv_usec = tsnow.tv_nsec / 1000;
+  if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
+    now.tv_sec = tsnow.tv_sec;
+    now.tv_usec = tsnow.tv_nsec / 1000;
+  }
+  /*
+  ** Even when the configure process has truly detected monotonic clock
+  ** availability, it might happen that it is not actually available at
+  ** run-time. When this occurs simply fallback to other time source.
+  */
+#ifdef HAVE_GETTIMEOFDAY
+  else
+    (void)gettimeofday(&now, NULL);
+#else
+  else {
+    now.tv_sec = (long)time(NULL);
+    now.tv_usec = 0;
+  }
+#endif
   return now;
 }
 
index 435b30e09588bcd5459166f9fc4d085d52009f13..573e6faed2b9c87fe61adcb79f537918285c1e30 100644 (file)
@@ -54,9 +54,24 @@ struct timeval tutil_tvnow(void)
   */
   struct timeval now;
   struct timespec tsnow;
-  (void)clock_gettime(CLOCK_MONOTONIC, &tsnow);
-  now.tv_sec = tsnow.tv_sec;
-  now.tv_usec = tsnow.tv_nsec / 1000;
+  if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
+    now.tv_sec = tsnow.tv_sec;
+    now.tv_usec = tsnow.tv_nsec / 1000;
+  }
+  /*
+  ** Even when the configure process has truly detected monotonic clock
+  ** availability, it might happen that it is not actually available at
+  ** run-time. When this occurs simply fallback to other time source.
+  */
+#ifdef HAVE_GETTIMEOFDAY
+  else
+    (void)gettimeofday(&now, NULL);
+#else
+  else {
+    now.tv_sec = (long)time(NULL);
+    now.tv_usec = 0;
+  }
+#endif
   return now;
 }