| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "rtc_base/fake_clock.h" |
| 12 |
| 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/thread.h" |
| 15 |
| 16 | namespace rtc { |
| 17 |
| 18 | int64_t FakeClock::TimeNanos() const { |
| 19 | webrtc::MutexLock lock(&lock_); |
| 20 | return time_ns_; |
| 21 | } |
| 22 |
| 23 | void FakeClock::SetTime(webrtc::Timestamp new_time) { |
| 24 | webrtc::MutexLock lock(&lock_); |
| 25 | RTC_DCHECK(new_time.us() * 1000 >= time_ns_); |
| 26 | time_ns_ = new_time.us() * 1000; |
| 27 | } |
| 28 |
| 29 | void FakeClock::AdvanceTime(webrtc::TimeDelta delta) { |
| 30 | webrtc::MutexLock lock(&lock_); |
| 31 | time_ns_ += delta.ns(); |
| 32 | } |
| 33 |
| 34 | void ThreadProcessingFakeClock::SetTime(webrtc::Timestamp time) { |
| 35 | clock_.SetTime(time); |
| 36 | // If message queues are waiting in a socket select() with a timeout provided |
| 37 | // by the OS, they should wake up and dispatch all messages that are ready. |
| 38 | ThreadManager::ProcessAllMessageQueuesForTesting(); |
| 39 | } |
| 40 |
| 41 | void ThreadProcessingFakeClock::AdvanceTime(webrtc::TimeDelta delta) { |
| 42 | clock_.AdvanceTime(delta); |
| 43 | ThreadManager::ProcessAllMessageQueuesForTesting(); |
| 44 | } |
| 45 |
| 46 | ScopedBaseFakeClock::ScopedBaseFakeClock() { |
| 47 | prev_clock_ = SetClockForTesting(this); |
| 48 | } |
| 49 |
| 50 | ScopedBaseFakeClock::~ScopedBaseFakeClock() { |
| 51 | SetClockForTesting(prev_clock_); |
| 52 | } |
| 53 |
| 54 | ScopedFakeClock::ScopedFakeClock() { |
| 55 | prev_clock_ = SetClockForTesting(this); |
| 56 | } |
| 57 |
| 58 | ScopedFakeClock::~ScopedFakeClock() { |
| 59 | SetClockForTesting(prev_clock_); |
| 60 | } |
| 61 |
| 62 | } // namespace rtc |
| 63 |