2016-05-27 14:15:43 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright 2016 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/fake_clock.h"
|
2016-05-27 14:15:43 -07:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2020-01-13 14:07:22 +01:00
|
|
|
#include "rtc_base/thread.h"
|
2016-05-27 14:15:43 -07:00
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
2016-11-28 01:54:54 -08:00
|
|
|
int64_t FakeClock::TimeNanos() const {
|
2020-07-08 17:55:58 +02:00
|
|
|
webrtc::MutexLock lock(&lock_);
|
2019-04-17 10:36:03 +02:00
|
|
|
return time_ns_;
|
2016-05-27 14:15:43 -07:00
|
|
|
}
|
|
|
|
|
|
2019-04-17 10:36:03 +02:00
|
|
|
void FakeClock::SetTime(webrtc::Timestamp new_time) {
|
2020-07-08 17:55:58 +02:00
|
|
|
webrtc::MutexLock lock(&lock_);
|
2019-04-17 10:36:03 +02:00
|
|
|
RTC_DCHECK(new_time.us() * 1000 >= time_ns_);
|
|
|
|
|
time_ns_ = new_time.us() * 1000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FakeClock::AdvanceTime(webrtc::TimeDelta delta) {
|
2020-07-08 17:55:58 +02:00
|
|
|
webrtc::MutexLock lock(&lock_);
|
2019-04-17 10:36:03 +02:00
|
|
|
time_ns_ += delta.ns();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ThreadProcessingFakeClock::SetTime(webrtc::Timestamp time) {
|
|
|
|
|
clock_.SetTime(time);
|
2016-05-27 14:15:43 -07:00
|
|
|
// If message queues are waiting in a socket select() with a timeout provided
|
2016-06-06 11:16:06 -07:00
|
|
|
// by the OS, they should wake up and dispatch all messages that are ready.
|
2020-01-13 14:07:22 +01:00
|
|
|
ThreadManager::ProcessAllMessageQueuesForTesting();
|
2016-05-27 14:15:43 -07:00
|
|
|
}
|
|
|
|
|
|
2019-04-17 10:36:03 +02:00
|
|
|
void ThreadProcessingFakeClock::AdvanceTime(webrtc::TimeDelta delta) {
|
|
|
|
|
clock_.AdvanceTime(delta);
|
2020-01-13 14:07:22 +01:00
|
|
|
ThreadManager::ProcessAllMessageQueuesForTesting();
|
2016-06-06 11:16:06 -07:00
|
|
|
}
|
|
|
|
|
|
2019-04-17 10:36:03 +02:00
|
|
|
ScopedBaseFakeClock::ScopedBaseFakeClock() {
|
|
|
|
|
prev_clock_ = SetClockForTesting(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScopedBaseFakeClock::~ScopedBaseFakeClock() {
|
|
|
|
|
SetClockForTesting(prev_clock_);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-06 11:16:06 -07:00
|
|
|
ScopedFakeClock::ScopedFakeClock() {
|
|
|
|
|
prev_clock_ = SetClockForTesting(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScopedFakeClock::~ScopedFakeClock() {
|
|
|
|
|
SetClockForTesting(prev_clock_);
|
2016-05-27 14:15:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|