• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2016 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
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 
TimeNanos() const18 int64_t FakeClock::TimeNanos() const {
19   webrtc::MutexLock lock(&lock_);
20   return time_ns_;
21 }
22 
SetTime(webrtc::Timestamp new_time)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 
AdvanceTime(webrtc::TimeDelta delta)29 void FakeClock::AdvanceTime(webrtc::TimeDelta delta) {
30   webrtc::MutexLock lock(&lock_);
31   time_ns_ += delta.ns();
32 }
33 
SetTime(webrtc::Timestamp time)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 
AdvanceTime(webrtc::TimeDelta delta)41 void ThreadProcessingFakeClock::AdvanceTime(webrtc::TimeDelta delta) {
42   clock_.AdvanceTime(delta);
43   ThreadManager::ProcessAllMessageQueuesForTesting();
44 }
45 
ScopedBaseFakeClock()46 ScopedBaseFakeClock::ScopedBaseFakeClock() {
47   prev_clock_ = SetClockForTesting(this);
48 }
49 
~ScopedBaseFakeClock()50 ScopedBaseFakeClock::~ScopedBaseFakeClock() {
51   SetClockForTesting(prev_clock_);
52 }
53 
ScopedFakeClock()54 ScopedFakeClock::ScopedFakeClock() {
55   prev_clock_ = SetClockForTesting(this);
56 }
57 
~ScopedFakeClock()58 ScopedFakeClock::~ScopedFakeClock() {
59   SetClockForTesting(prev_clock_);
60 }
61 
62 }  // namespace rtc
63