Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/fakeclock.h" |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/messagequeue.h" |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 15 | |
| 16 | namespace rtc { |
| 17 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 18 | int64_t FakeClock::TimeNanos() const { |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 19 | CritScope cs(&lock_); |
| 20 | return time_; |
| 21 | } |
| 22 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 23 | void FakeClock::SetTimeNanos(int64_t nanos) { |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 24 | { |
| 25 | CritScope cs(&lock_); |
| 26 | RTC_DCHECK(nanos >= time_); |
| 27 | time_ = nanos; |
| 28 | } |
| 29 | // If message queues are waiting in a socket select() with a timeout provided |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 30 | // by the OS, they should wake up and dispatch all messages that are ready. |
Niels Möller | 8909a63 | 2018-09-06 08:42:44 +0200 | [diff] [blame] | 31 | MessageQueueManager::ProcessAllMessageQueuesForTesting(); |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 34 | void FakeClock::AdvanceTime(webrtc::TimeDelta delta) { |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 35 | { |
| 36 | CritScope cs(&lock_); |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 37 | time_ += delta.ns(); |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 38 | } |
Niels Möller | 8909a63 | 2018-09-06 08:42:44 +0200 | [diff] [blame] | 39 | MessageQueueManager::ProcessAllMessageQueuesForTesting(); |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ScopedFakeClock::ScopedFakeClock() { |
| 43 | prev_clock_ = SetClockForTesting(this); |
| 44 | } |
| 45 | |
| 46 | ScopedFakeClock::~ScopedFakeClock() { |
| 47 | SetClockForTesting(prev_clock_); |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | } // namespace rtc |