File: fake_clock.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (84 lines) | stat: -rw-r--r-- 2,768 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "platform/test/fake_clock.h"

#include <algorithm>

#include "platform/test/fake_task_runner.h"
#include "util/osp_logging.h"

namespace openscreen {

namespace {
constexpr Clock::time_point kInvalid = Clock::time_point::min();
}

FakeClock::FakeClock(Clock::time_point start_time)
    : control_thread_id_(std::this_thread::get_id()) {
  OSP_CHECK_EQ(now_.load(std::memory_order_acquire), kInvalid)
      << "Only one FakeClock instance allowed!";
  now_.store(start_time, std::memory_order_release);
}

FakeClock::~FakeClock() {
  OSP_CHECK_EQ(std::this_thread::get_id(), control_thread_id_);
  OSP_CHECK(task_runners_.empty());
  // Set |now_| to kInvalid to flag that this FakeClock has been destroyed.
  now_.store(kInvalid, std::memory_order_release);
}

Clock::time_point FakeClock::now() noexcept {
  const Clock::time_point value = now_.load(std::memory_order_acquire);
  OSP_CHECK_NE(value, kInvalid) << "No FakeClock instance!";
  return value;
}

void FakeClock::Advance(Clock::duration delta) {
  OSP_CHECK_EQ(std::this_thread::get_id(), control_thread_id_);

  const Clock::time_point stop_time = now() + delta;

  for (;;) {
    // Run tasks at the current time, since this might cause additional delayed
    // tasks to be posted.
    for (FakeTaskRunner* task_runner : task_runners_) {
      task_runner->RunTasksUntilIdle();
    }

    // Find the next "step-to" time, and advance the clock to that point.
    Clock::time_point step_to = Clock::time_point::max();
    for (FakeTaskRunner* task_runner : task_runners_) {
      step_to = std::min(step_to, task_runner->GetResumeTime());
    }
    if (step_to > stop_time) {
      break;  // No tasks are scheduled for the remaining time range.
    }

    OSP_DCHECK_GT(step_to, now());
    now_.store(step_to, std::memory_order_release);
  }

  // Skip over any remaining "dead time."
  now_.store(stop_time, std::memory_order_release);
}

void FakeClock::SubscribeToTimeChanges(FakeTaskRunner* task_runner) {
  OSP_CHECK_EQ(std::this_thread::get_id(), control_thread_id_);
  OSP_CHECK(std::find(task_runners_.begin(), task_runners_.end(),
                      task_runner) == task_runners_.end());
  task_runners_.push_back(task_runner);
}

void FakeClock::UnsubscribeFromTimeChanges(FakeTaskRunner* task_runner) {
  OSP_CHECK_EQ(std::this_thread::get_id(), control_thread_id_);
  auto it = std::find(task_runners_.begin(), task_runners_.end(), task_runner);
  OSP_CHECK(it != task_runners_.end());
  task_runners_.erase(it);
}

// static
std::atomic<Clock::time_point> FakeClock::now_{kInvalid};

}  // namespace openscreen