Makes Clock interface fully mutable.
Calls to the time functions in Clock can have side effects in some
circumstances. It's also questionable if it's a good idea to allow
repeated calls to a const method return different values without
any changed to the class instance.
Bug: webrtc:10270
Change-Id: I316f9788adac954c52b0f9230881b872c54a7ac9
Reviewed-on: https://webrtc-review.googlesource.com/c/120348
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26482}
diff --git a/system_wrappers/include/clock.h b/system_wrappers/include/clock.h
index f1fc11f..3d292c8 100644
--- a/system_wrappers/include/clock.h
+++ b/system_wrappers/include/clock.h
@@ -32,17 +32,17 @@
// Return a timestamp in milliseconds relative to some arbitrary source; the
// source is fixed for this clock.
- virtual int64_t TimeInMilliseconds() const = 0;
+ virtual int64_t TimeInMilliseconds() = 0;
// Return a timestamp in microseconds relative to some arbitrary source; the
// source is fixed for this clock.
- virtual int64_t TimeInMicroseconds() const = 0;
+ virtual int64_t TimeInMicroseconds() = 0;
// Retrieve an NTP absolute timestamp.
- virtual NtpTime CurrentNtpTime() const = 0;
+ virtual NtpTime CurrentNtpTime() = 0;
// Retrieve an NTP absolute timestamp in milliseconds.
- virtual int64_t CurrentNtpInMilliseconds() const = 0;
+ virtual int64_t CurrentNtpInMilliseconds() = 0;
// Converts an NTP timestamp to a millisecond timestamp.
static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) {
@@ -61,17 +61,17 @@
// Return a timestamp in milliseconds relative to some arbitrary source; the
// source is fixed for this clock.
- int64_t TimeInMilliseconds() const override;
+ int64_t TimeInMilliseconds() override;
// Return a timestamp in microseconds relative to some arbitrary source; the
// source is fixed for this clock.
- int64_t TimeInMicroseconds() const override;
+ int64_t TimeInMicroseconds() override;
// Retrieve an NTP absolute timestamp.
- NtpTime CurrentNtpTime() const override;
+ NtpTime CurrentNtpTime() override;
// Converts an NTP timestamp to a millisecond timestamp.
- int64_t CurrentNtpInMilliseconds() const override;
+ int64_t CurrentNtpInMilliseconds() override;
// Advance the simulated clock with a given number of milliseconds or
// microseconds.
diff --git a/system_wrappers/source/clock.cc b/system_wrappers/source/clock.cc
index 27624be..a0acf40 100644
--- a/system_wrappers/source/clock.cc
+++ b/system_wrappers/source/clock.cc
@@ -34,14 +34,14 @@
class RealTimeClock : public Clock {
// Return a timestamp in milliseconds relative to some arbitrary source; the
// source is fixed for this clock.
- int64_t TimeInMilliseconds() const override { return rtc::TimeMillis(); }
+ int64_t TimeInMilliseconds() override { return rtc::TimeMillis(); }
// Return a timestamp in microseconds relative to some arbitrary source; the
// source is fixed for this clock.
- int64_t TimeInMicroseconds() const override { return rtc::TimeMicros(); }
+ int64_t TimeInMicroseconds() override { return rtc::TimeMicros(); }
// Retrieve an NTP absolute timestamp.
- NtpTime CurrentNtpTime() const override {
+ NtpTime CurrentNtpTime() override {
timeval tv = CurrentTimeVal();
double microseconds_in_seconds;
uint32_t seconds;
@@ -52,7 +52,7 @@
}
// Retrieve an NTP absolute timestamp in milliseconds.
- int64_t CurrentNtpInMilliseconds() const override {
+ int64_t CurrentNtpInMilliseconds() override {
timeval tv = CurrentTimeVal();
uint32_t seconds;
double microseconds_in_seconds;
@@ -62,7 +62,7 @@
}
protected:
- virtual timeval CurrentTimeVal() const = 0;
+ virtual timeval CurrentTimeVal() = 0;
static void Adjust(const timeval& tv,
uint32_t* adjusted_s,
@@ -87,7 +87,7 @@
~WinUwpRealTimeClock() override {}
protected:
- timeval CurrentTimeVal() const override {
+ timeval CurrentTimeVal() override {
// The rtc::SystemTimeNanos() method is already time offset from a base
// epoch value and might as be synchronized against an NTP time server as
// an added bonus.
@@ -104,7 +104,7 @@
#elif defined(WEBRTC_WIN)
// TODO(pbos): Consider modifying the implementation to synchronize itself
-// against system time (update ref_point_, make it non-const) periodically to
+// against system time (update ref_point_) periodically to
// prevent clock drift.
class WindowsRealTimeClock : public RealTimeClock {
public:
@@ -121,7 +121,7 @@
LARGE_INTEGER counter_ms;
};
- timeval CurrentTimeVal() const override {
+ timeval CurrentTimeVal() override {
const uint64_t FILETIME_1970 = 0x019db1ded53e8000;
FILETIME StartTime;
@@ -143,7 +143,7 @@
return tv;
}
- void GetTime(FILETIME* current_time) const {
+ void GetTime(FILETIME* current_time) {
DWORD t;
LARGE_INTEGER elapsed_ms;
{
@@ -197,10 +197,9 @@
return ref;
}
- // mutable as time-accessing functions are const.
rtc::CriticalSection crit_;
- mutable DWORD last_time_ms_;
- mutable LONG num_timer_wraps_;
+ DWORD last_time_ms_;
+ LONG num_timer_wraps_;
const ReferencePoint ref_point_;
};
@@ -212,7 +211,7 @@
~UnixRealTimeClock() override {}
protected:
- timeval CurrentTimeVal() const override {
+ timeval CurrentTimeVal() override {
struct timeval tv;
struct timezone tz;
tz.tz_minuteswest = 0;
@@ -241,17 +240,17 @@
SimulatedClock::~SimulatedClock() {}
-int64_t SimulatedClock::TimeInMilliseconds() const {
+int64_t SimulatedClock::TimeInMilliseconds() {
ReadLockScoped synchronize(*lock_);
return (time_us_ + 500) / 1000;
}
-int64_t SimulatedClock::TimeInMicroseconds() const {
+int64_t SimulatedClock::TimeInMicroseconds() {
ReadLockScoped synchronize(*lock_);
return time_us_;
}
-NtpTime SimulatedClock::CurrentNtpTime() const {
+NtpTime SimulatedClock::CurrentNtpTime() {
int64_t now_ms = TimeInMilliseconds();
uint32_t seconds = (now_ms / 1000) + kNtpJan1970;
uint32_t fractions =
@@ -259,7 +258,7 @@
return NtpTime(seconds, fractions);
}
-int64_t SimulatedClock::CurrentNtpInMilliseconds() const {
+int64_t SimulatedClock::CurrentNtpInMilliseconds() {
return TimeInMilliseconds() + 1000 * static_cast<int64_t>(kNtpJan1970);
}
diff --git a/test/drifting_clock.cc b/test/drifting_clock.cc
index 5333e1d..41ab2cf 100644
--- a/test/drifting_clock.cc
+++ b/test/drifting_clock.cc
@@ -31,15 +31,15 @@
return (now - start_time_) * drift_;
}
-int64_t DriftingClock::TimeInMilliseconds() const {
+int64_t DriftingClock::TimeInMilliseconds() {
return clock_->TimeInMilliseconds() + Drift() / 1000.;
}
-int64_t DriftingClock::TimeInMicroseconds() const {
+int64_t DriftingClock::TimeInMicroseconds() {
return clock_->TimeInMicroseconds() + Drift();
}
-NtpTime DriftingClock::CurrentNtpTime() const {
+NtpTime DriftingClock::CurrentNtpTime() {
// NTP precision is 1/2^32 seconds, i.e. 2^32 ntp fractions = 1 second.
const double kNtpFracPerMicroSecond = 4294.967296; // = 2^32 / 10^6
@@ -49,7 +49,7 @@
return NtpTime(total_fractions);
}
-int64_t DriftingClock::CurrentNtpInMilliseconds() const {
+int64_t DriftingClock::CurrentNtpInMilliseconds() {
return clock_->CurrentNtpInMilliseconds() + Drift() / 1000.;
}
} // namespace test
diff --git a/test/drifting_clock.h b/test/drifting_clock.h
index dba820b..46e3c1b 100644
--- a/test/drifting_clock.h
+++ b/test/drifting_clock.h
@@ -30,10 +30,10 @@
static float PercentsFaster(float percent) { return 1.0f + percent / 100.0f; }
static float PercentsSlower(float percent) { return 1.0f - percent / 100.0f; }
- int64_t TimeInMilliseconds() const override;
- int64_t TimeInMicroseconds() const override;
- NtpTime CurrentNtpTime() const override;
- int64_t CurrentNtpInMilliseconds() const override;
+ int64_t TimeInMilliseconds() override;
+ int64_t TimeInMicroseconds() override;
+ NtpTime CurrentNtpTime() override;
+ int64_t CurrentNtpInMilliseconds() override;
private:
float Drift() const;