Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/fake_clock.h" |
Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 14 | #include "rtc_base/message_queue.h" |
Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 15 | |
| 16 | namespace rtc { |
| 17 | |
nisse | deb95f3 | 2016-11-28 09:54:54 | [diff] [blame] | 18 | int64_t FakeClock::TimeNanos() const { |
Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 19 | CritScope cs(&lock_); |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 20 | return time_ns_; |
Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 21 | } |
| 22 | |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 23 | void FakeClock::SetTime(webrtc::Timestamp new_time) { |
| 24 | CritScope cs(&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 | CritScope cs(&lock_); |
| 31 | time_ns_ += delta.ns(); |
| 32 | } |
| 33 | |
| 34 | void ThreadProcessingFakeClock::SetTime(webrtc::Timestamp time) { |
| 35 | clock_.SetTime(time); |
Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 36 | // If message queues are waiting in a socket select() with a timeout provided |
deadbeef | f5f03e8 | 2016-06-06 18:16:06 | [diff] [blame] | 37 | // by the OS, they should wake up and dispatch all messages that are ready. |
Niels Möller | 8909a63 | 2018-09-06 06:42:44 | [diff] [blame] | 38 | MessageQueueManager::ProcessAllMessageQueuesForTesting(); |
Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 39 | } |
| 40 | |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 41 | void ThreadProcessingFakeClock::AdvanceTime(webrtc::TimeDelta delta) { |
| 42 | clock_.AdvanceTime(delta); |
Niels Möller | 8909a63 | 2018-09-06 06:42:44 | [diff] [blame] | 43 | MessageQueueManager::ProcessAllMessageQueuesForTesting(); |
deadbeef | f5f03e8 | 2016-06-06 18:16:06 | [diff] [blame] | 44 | } |
| 45 | |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 46 | ScopedBaseFakeClock::ScopedBaseFakeClock() { |
| 47 | prev_clock_ = SetClockForTesting(this); |
| 48 | } |
| 49 | |
| 50 | ScopedBaseFakeClock::~ScopedBaseFakeClock() { |
| 51 | SetClockForTesting(prev_clock_); |
| 52 | } |
| 53 | |
deadbeef | f5f03e8 | 2016-06-06 18:16:06 | [diff] [blame] | 54 | ScopedFakeClock::ScopedFakeClock() { |
| 55 | prev_clock_ = SetClockForTesting(this); |
| 56 | } |
| 57 | |
| 58 | ScopedFakeClock::~ScopedFakeClock() { |
| 59 | SetClockForTesting(prev_clock_); |
Taylor Brandstetter | b3c6810 | 2016-05-27 21:15:43 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | } // namespace rtc |