Bug 1039924 part 1 - Separate platform-dependent parts of TimeDuration into TimeDurationPlatformUtils; r=froydnj
authorBrian Birtles <birtles@gmail.com>
Thu, 25 Sep 2014 14:25:48 +0900
changeset 222589 dc77f42529f7b494895c19365939f400b7176b62
parent 222588 84a355b48a1af40453d27573425e80f382a7caaf
child 222590 cef7118a25cb3dfb378eb46eb7e01201e44948e0
push id7107
push userraliiev@mozilla.com
push dateMon, 13 Oct 2014 17:43:31 +0000
treeherdermozilla-aurora@b4b34e0acc75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1039924
milestone35.0a1
Bug 1039924 part 1 - Separate platform-dependent parts of TimeDuration into TimeDurationPlatformUtils; r=froydnj In order to have different templated versions of TimeDuration we first split out the platform-specific code so that this code doesn't need to concern itself with templates (and because putting template code in .cpp files is messy).
xpcom/ds/TimeStamp.h
xpcom/ds/TimeStamp_darwin.cpp
xpcom/ds/TimeStamp_posix.cpp
xpcom/ds/TimeStamp_windows.cpp
--- a/xpcom/ds/TimeStamp.h
+++ b/xpcom/ds/TimeStamp.h
@@ -27,16 +27,28 @@ namespace mozilla {
 
 #ifndef XP_WIN
 typedef uint64_t TimeStampValue;
 #endif
 
 class TimeStamp;
 
 /**
+ * Platform-specific implementation details of TimeDuration.
+ */
+class TimeDurationPlatformUtils
+{
+public:
+  static double ToSeconds(int64_t aTicks);
+  static double ToSecondsSigDigits(int64_t aTicks);
+  static int64_t TicksFromMilliseconds(double aMilliseconds);
+  static int64_t ResolutionInTicks();
+};
+
+/**
  * Instances of this class represent the length of an interval of time.
  * Negative durations are allowed, meaning the end is before the start.
  *
  * Internally the duration is stored as a int64_t in units of
  * PR_TicksPerSecond() when building with NSPR interval timers, or a
  * system-dependent unit when building with system clocks.  The
  * system-dependent unit must be constant, otherwise the semantics of
  * this class would be broken.
@@ -50,34 +62,44 @@ public:
   // but no other numbers (so we don't have any implicit unit conversions).
   struct _SomethingVeryRandomHere;
   MOZ_IMPLICIT TimeDuration(_SomethingVeryRandomHere* aZero) : mValue(0)
   {
     MOZ_ASSERT(!aZero, "Who's playing funny games here?");
   }
   // Default copy-constructor and assignment are OK
 
-  double ToSeconds() const;
+  double ToSeconds() const
+  {
+    return TimeDurationPlatformUtils::ToSeconds(mValue);
+  }
   // Return a duration value that includes digits of time we think to
   // be significant.  This method should be used when displaying a
   // time to humans.
-  double ToSecondsSigDigits() const;
+  double ToSecondsSigDigits() const
+  {
+    return TimeDurationPlatformUtils::ToSecondsSigDigits(mValue);
+  }
   double ToMilliseconds() const { return ToSeconds() * 1000.0; }
   double ToMicroseconds() const { return ToMilliseconds() * 1000.0; }
 
   // Using a double here is safe enough; with 53 bits we can represent
   // durations up to over 280,000 years exactly.  If the units of
   // mValue do not allow us to represent durations of that length,
   // long durations are clamped to the max/min representable value
   // instead of overflowing.
   static inline TimeDuration FromSeconds(double aSeconds)
   {
     return FromMilliseconds(aSeconds * 1000.0);
   }
-  static TimeDuration FromMilliseconds(double aMilliseconds);
+  static TimeDuration FromMilliseconds(double aMilliseconds)
+  {
+    return FromTicks(
+      TimeDurationPlatformUtils::TicksFromMilliseconds(aMilliseconds));
+  }
   static inline TimeDuration FromMicroseconds(double aMicroseconds)
   {
     return FromMilliseconds(aMicroseconds / 1000.0);
   }
 
   static TimeDuration Forever()
   {
     return FromTicks(INT64_MAX);
@@ -170,17 +192,19 @@ public:
   {
     return mValue != aOther.mValue;
   }
 
   // Return a best guess at the system's current timing resolution,
   // which might be variable.  TimeDurations below this order of
   // magnitude are meaningless, and those at the same order of
   // magnitude or just above are suspect.
-  static TimeDuration Resolution();
+  static TimeDuration Resolution() {
+    return FromTicks(TimeDurationPlatformUtils::ResolutionInTicks());
+  }
 
   // We could define additional operators here:
   // -- convert to/from other time units
   // -- scale duration by a float
   // but let's do that on demand.
   // Comparing durations for equality will only lead to bugs on
   // platforms with high-resolution timers.
 
--- a/xpcom/ds/TimeStamp_darwin.cpp
+++ b/xpcom/ds/TimeStamp_darwin.cpp
@@ -77,45 +77,45 @@ ClockResolutionNs()
   }
 
   return minres;
 }
 
 namespace mozilla {
 
 double
-TimeDuration::ToSeconds() const
+TimeDurationPlatformUtils::ToSeconds(int64_t aTicks)
 {
   NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
-  return (mValue * sNsPerTick) / kNsPerSecd;
+  return (aTicks * sNsPerTick) / kNsPerSecd;
 }
 
 double
-TimeDuration::ToSecondsSigDigits() const
+TimeDurationPlatformUtils::ToSecondsSigDigits(int64_t aTicks)
 {
   NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
   // don't report a value < mResolution ...
-  int64_t valueSigDigs = sResolution * (mValue / sResolution);
+  int64_t valueSigDigs = sResolution * (aTicks / sResolution);
   // and chop off insignificant digits
   valueSigDigs = sResolutionSigDigs * (valueSigDigs / sResolutionSigDigs);
   return (valueSigDigs * sNsPerTick) / kNsPerSecd;
 }
 
-TimeDuration
-TimeDuration::FromMilliseconds(double aMilliseconds)
+int64_t
+TimeDurationPlatformUtils::TicksFromMilliseconds(double aMilliseconds)
 {
   NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
-  return TimeDuration::FromTicks((aMilliseconds * kNsPerMsd) / sNsPerTick);
+  return (aMilliseconds * kNsPerMsd) / sNsPerTick;
 }
 
-TimeDuration
-TimeDuration::Resolution()
+int64_t
+TimeDurationPlatformUtils::ResolutionInTicks()
 {
   NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
-  return TimeDuration::FromTicks(int64_t(sResolution));
+  return static_cast<int64_t>(sResolution);
 }
 
 nsresult
 TimeStamp::Startup()
 {
   if (gInitialized) {
     return NS_OK;
   }
--- a/xpcom/ds/TimeStamp_posix.cpp
+++ b/xpcom/ds/TimeStamp_posix.cpp
@@ -129,41 +129,41 @@ ClockResolutionNs()
   }
 
   return minres;
 }
 
 namespace mozilla {
 
 double
-TimeDuration::ToSeconds() const
+TimeDurationPlatformUtils::ToSeconds(int64_t aTicks)
 {
-  return double(mValue) / kNsPerSecd;
+  return double(aTicks) / kNsPerSecd;
 }
 
 double
-TimeDuration::ToSecondsSigDigits() const
+TimeDurationPlatformUtils::ToSecondsSigDigits(int64_t aTicks)
 {
   // don't report a value < mResolution ...
-  int64_t valueSigDigs = sResolution * (mValue / sResolution);
+  int64_t valueSigDigs = sResolution * (aTicks / sResolution);
   // and chop off insignificant digits
   valueSigDigs = sResolutionSigDigs * (valueSigDigs / sResolutionSigDigs);
   return double(valueSigDigs) / kNsPerSecd;
 }
 
-TimeDuration
-TimeDuration::FromMilliseconds(double aMilliseconds)
+int64_t
+TimeDurationPlatformUtils::TicksFromMilliseconds(double aMilliseconds)
 {
-  return TimeDuration::FromTicks(aMilliseconds * kNsPerMsd);
+  return aMilliseconds * kNsPerMsd;
 }
 
-TimeDuration
-TimeDuration::Resolution()
+int64_t
+TimeDurationPlatformUtils::ResolutionInTicks()
 {
-  return TimeDuration::FromTicks(int64_t(sResolution));
+  return static_cast<int64_t>(sResolution);
 }
 
 static bool gInitialized = false;
 
 nsresult
 TimeStamp::Startup()
 {
   if (gInitialized) {
--- a/xpcom/ds/TimeStamp_windows.cpp
+++ b/xpcom/ds/TimeStamp_windows.cpp
@@ -408,44 +408,44 @@ TimeStampValue::operator-(const TimeStam
   return CheckQPC(aOther);
 }
 
 // ----------------------------------------------------------------------------
 // TimeDuration and TimeStamp implementation
 // ----------------------------------------------------------------------------
 
 double
-TimeDuration::ToSeconds() const
+TimeDurationPlatformUtils::ToSeconds(int64_t aTicks)
 {
   // Converting before arithmetic avoids blocked store forward
-  return double(mValue) / (double(sFrequencyPerSec) * 1000.0);
+  return double(aTicks) / (double(sFrequencyPerSec) * 1000.0);
 }
 
 double
-TimeDuration::ToSecondsSigDigits() const
+TimeDurationPlatformUtils::ToSecondsSigDigits(int64_t aTicks)
 {
   // don't report a value < mResolution ...
   LONGLONG resolution = sResolution;
   LONGLONG resolutionSigDigs = sResolutionSigDigs;
-  LONGLONG valueSigDigs = resolution * (mValue / resolution);
+  LONGLONG valueSigDigs = resolution * (aTicks / resolution);
   // and chop off insignificant digits
   valueSigDigs = resolutionSigDigs * (valueSigDigs / resolutionSigDigs);
   return double(valueSigDigs) / kNsPerSecd;
 }
 
-TimeDuration
-TimeDuration::FromMilliseconds(double aMilliseconds)
+int64_t
+TimeDurationPlatformUtils::TicksFromMilliseconds(double aMilliseconds)
 {
-  return TimeDuration::FromTicks(ms2mt(aMilliseconds));
+  return ms2mt(aMilliseconds);
 }
 
-TimeDuration
-TimeDuration::Resolution()
+int64_t
+TimeDurationPlatformUtils::ResolutionInTicks()
 {
-  return TimeDuration::FromTicks(int64_t(sResolution));
+  return static_cast<int64_t>(sResolution);
 }
 
 static bool
 HasStableTSC()
 {
   union
   {
     int regs[4];