diff --git a/system_wrappers/BUILD.gn b/system_wrappers/BUILD.gn index b4466482c1..f44ff5b8bf 100644 --- a/system_wrappers/BUILD.gn +++ b/system_wrappers/BUILD.gn @@ -37,7 +37,6 @@ rtc_library("system_wrappers") { "../modules:module_api_public", "../rtc_base:checks", "../rtc_base/synchronization:mutex", - "../rtc_base/synchronization:rw_lock_wrapper", "../rtc_base/system:arch", "../rtc_base/system:rtc_export", ] diff --git a/system_wrappers/include/clock.h b/system_wrappers/include/clock.h index 8eac3d18b3..3c60f63da8 100644 --- a/system_wrappers/include/clock.h +++ b/system_wrappers/include/clock.h @@ -13,10 +13,10 @@ #include +#include #include #include "api/units/timestamp.h" -#include "rtc_base/synchronization/rw_lock_wrapper.h" #include "rtc_base/system/rtc_export.h" #include "system_wrappers/include/ntp_time.h" @@ -78,8 +78,12 @@ class SimulatedClock : public Clock { void AdvanceTime(TimeDelta delta); private: - Timestamp time_; - std::unique_ptr lock_; + // The time is read and incremented with relaxed order. Each thread will see + // monotonically increasing time, and when threads post tasks or messages to + // one another, the synchronization done as part of the message passing should + // ensure that any causual chain of events on multiple threads also + // corresponds to monotonically increasing time. + std::atomic time_us_; }; } // namespace webrtc diff --git a/system_wrappers/source/clock.cc b/system_wrappers/source/clock.cc index e0f4b401e8..0ae624d849 100644 --- a/system_wrappers/source/clock.cc +++ b/system_wrappers/source/clock.cc @@ -26,7 +26,6 @@ #endif // defined(WEBRTC_POSIX) #include "rtc_base/synchronization/mutex.h" -#include "rtc_base/synchronization/rw_lock_wrapper.h" #include "rtc_base/time_utils.h" namespace webrtc { @@ -239,16 +238,15 @@ Clock* Clock::GetRealTimeClock() { } SimulatedClock::SimulatedClock(int64_t initial_time_us) - : SimulatedClock(Timestamp::Micros(initial_time_us)) {} + : time_us_(initial_time_us) {} SimulatedClock::SimulatedClock(Timestamp initial_time) - : time_(initial_time), lock_(RWLockWrapper::CreateRWLock()) {} + : SimulatedClock(initial_time.us()) {} SimulatedClock::~SimulatedClock() {} Timestamp SimulatedClock::CurrentTime() { - ReadLockScoped synchronize(*lock_); - return time_; + return Timestamp::Micros(time_us_.load(std::memory_order_relaxed)); } NtpTime SimulatedClock::CurrentNtpTime() { @@ -271,9 +269,13 @@ void SimulatedClock::AdvanceTimeMicroseconds(int64_t microseconds) { AdvanceTime(TimeDelta::Micros(microseconds)); } +// TODO(bugs.webrtc.org(12102): It's desirable to let a single thread own +// advancement of the clock. We could then replace this read-modify-write +// operation with just a thread checker. But currently, that breaks a couple of +// tests, in particular, RepeatingTaskTest.ClockIntegration and +// CallStatsTest.LastProcessedRtt. void SimulatedClock::AdvanceTime(TimeDelta delta) { - WriteLockScoped synchronize(*lock_); - time_ += delta; + time_us_.fetch_add(delta.us(), std::memory_order_relaxed); } } // namespace webrtc