[PATCH] Use clock_gettime()'s CLOCK_REALTIME instead of gettimeofday() where possible

Ed Schouten ed at nuxi.nl
Wed Mar 11 07:30:23 PDT 2015


Hi jroelofs,

The system_clock::now() function currently uses gettimeofday(). The problem with gettimeofday() is that it is an obsolete XSI function, hence unavailable on CloudABI. See:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/gettimeofday.html

Change this code to use clock_gettime() with CLOCK_REALTIME instead, which is more consistent, as clock_gettime() is already used for steady_clock.

A previous version of this change actually attempted to change system_clock::duration, but I reverted this part as it breaks the existing ABI.

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D8253

Files:
  src/chrono.cpp

Index: src/chrono.cpp
===================================================================
--- src/chrono.cpp
+++ src/chrono.cpp
@@ -8,13 +8,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "chrono"
-#include <sys/time.h>        //for gettimeofday and timeval
 #ifdef __APPLE__
+#include <sys/time.h>        // for gettimeofday and timeval
 #include <mach/mach_time.h>  // mach_absolute_time, mach_timebase_info_data_t
 #else  /* !__APPLE__ */
 #include <cerrno>  // errno
 #include <system_error>  // __throw_system_error
-#include <time.h>  // clock_gettime, CLOCK_MONOTONIC
+#include <time.h>  // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
 #endif  // __APPLE__
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -29,9 +29,16 @@
 system_clock::time_point
 system_clock::now() _NOEXCEPT
 {
+#ifdef __APPLE__
     timeval tv;
     gettimeofday(&tv, 0);
     return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
+#else  // __APPLE__
+    struct timespec tp;
+    if (0 != clock_gettime(CLOCK_REALTIME, &tp))
+        __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
+    return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
+#endif  // __APPLE__
 }
 
 time_t

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8253.21717.patch
Type: text/x-patch
Size: 1252 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150311/2358e316/attachment.bin>


More information about the cfe-commits mailing list