blob: b9f0ee95e5358c6e9be3f12eb68c6972782cc390 [file] [log] [blame]
Taylor Brandstetterb3c68102016-05-27 21:15:431/*
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 Anton10542f22019-01-11 17:11:0011#include "rtc_base/fake_clock.h"
Taylor Brandstetterb3c68102016-05-27 21:15:4312
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0014#include "rtc_base/message_queue.h"
Taylor Brandstetterb3c68102016-05-27 21:15:4315
16namespace rtc {
17
nissedeb95f32016-11-28 09:54:5418int64_t FakeClock::TimeNanos() const {
Taylor Brandstetterb3c68102016-05-27 21:15:4319 CritScope cs(&lock_);
Sebastian Janssond624c392019-04-17 08:36:0320 return time_ns_;
Taylor Brandstetterb3c68102016-05-27 21:15:4321}
22
Sebastian Janssond624c392019-04-17 08:36:0323void 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
29void FakeClock::AdvanceTime(webrtc::TimeDelta delta) {
30 CritScope cs(&lock_);
31 time_ns_ += delta.ns();
32}
33
34void ThreadProcessingFakeClock::SetTime(webrtc::Timestamp time) {
35 clock_.SetTime(time);
Taylor Brandstetterb3c68102016-05-27 21:15:4336 // If message queues are waiting in a socket select() with a timeout provided
deadbeeff5f03e82016-06-06 18:16:0637 // by the OS, they should wake up and dispatch all messages that are ready.
Niels Möller8909a632018-09-06 06:42:4438 MessageQueueManager::ProcessAllMessageQueuesForTesting();
Taylor Brandstetterb3c68102016-05-27 21:15:4339}
40
Sebastian Janssond624c392019-04-17 08:36:0341void ThreadProcessingFakeClock::AdvanceTime(webrtc::TimeDelta delta) {
42 clock_.AdvanceTime(delta);
Niels Möller8909a632018-09-06 06:42:4443 MessageQueueManager::ProcessAllMessageQueuesForTesting();
deadbeeff5f03e82016-06-06 18:16:0644}
45
Sebastian Janssond624c392019-04-17 08:36:0346ScopedBaseFakeClock::ScopedBaseFakeClock() {
47 prev_clock_ = SetClockForTesting(this);
48}
49
50ScopedBaseFakeClock::~ScopedBaseFakeClock() {
51 SetClockForTesting(prev_clock_);
52}
53
deadbeeff5f03e82016-06-06 18:16:0654ScopedFakeClock::ScopedFakeClock() {
55 prev_clock_ = SetClockForTesting(this);
56}
57
58ScopedFakeClock::~ScopedFakeClock() {
59 SetClockForTesting(prev_clock_);
Taylor Brandstetterb3c68102016-05-27 21:15:4360}
61
62} // namespace rtc