CLOCK_REALTIME is 0 on Linux, use -1 for invalid clock id.
authorbmeurer@chromium.org <bmeurer@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 12 Nov 2013 12:30:59 +0000 (12:30 +0000)
committerbmeurer@chromium.org <bmeurer@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 12 Nov 2013 12:30:59 +0000 (12:30 +0000)
TBR=svenpanne@chromium.org

Review URL: https://codereview.chromium.org/70133003

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@17644 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/platform/time.cc

index d87dc24..eb5d72f 100644 (file)
@@ -295,8 +295,10 @@ Time Time::Now() {
   struct timespec ts;
   // Use CLOCK_REALTIME_COARSE if it's available and has a precision of 1ms
   // or higher.  It's serviced from the vDSO with no system call overhead.
-  static clock_t clock_id = static_cast<clock_t>(0);
-  if (!clock_id) {
+  static clock_t clock_id = static_cast<clock_t>(-1);
+  STATIC_ASSERT(CLOCK_REALTIME != static_cast<clock_t>(-1));
+  STATIC_ASSERT(CLOCK_REALTIME_COARSE != static_cast<clock_t>(-1));
+  if (clock_id == static_cast<clock_t>(-1)) {
     if (clock_getres(CLOCK_REALTIME_COARSE, &ts) == 0
         && ts.tv_nsec <= kNanosecondsPerMillisecond)
       clock_id = CLOCK_REALTIME_COARSE;
@@ -611,8 +613,10 @@ TimeTicks TimeTicks::HighResolutionNow() {
 #if defined(CLOCK_MONOTONIC_COARSE)
   // Use CLOCK_MONOTONIC_COARSE if it's available and has a precision of 1ms
   // or higher.  It's serviced from the vDSO with no system call overhead.
-  static clock_t clock_id = static_cast<clock_t>(0);
-  if (!clock_id) {
+  static clock_t clock_id = static_cast<clock_t>(-1);
+  STATIC_ASSERT(CLOCK_MONOTONIC != static_cast<clock_t>(-1));
+  STATIC_ASSERT(CLOCK_MONOTONIC_COARSE != static_cast<clock_t>(-1));
+  if (clock_id == static_cast<clock_t>(-1)) {
     if (clock_getres(CLOCK_MONOTONIC_COARSE, &ts) == 0
         && ts.tv_nsec <= Time::kNanosecondsPerMillisecond)
       clock_id = CLOCK_MONOTONIC_COARSE;