Delete use of RWLockWrapper from SimulatedClock

Instead, use lock-less reads and increments of current time,
with relaxed memory order.

Bug: webrtc:12102
Change-Id: I0e804d97d7adb5d3d115544487573ea03d132590
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/191225
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Sebastian Jansson <srte@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32543}
This commit is contained in:
Niels Möller 2020-11-03 13:55:44 +01:00 committed by Commit Bot
parent c940870b72
commit 9308b7af1e
3 changed files with 16 additions and 11 deletions

View File

@ -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",
]

View File

@ -13,10 +13,10 @@
#include <stdint.h>
#include <atomic>
#include <memory>
#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<RWLockWrapper> 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<int64_t> time_us_;
};
} // namespace webrtc

View File

@ -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