Commit 1f358aca authored by Greg Kraynov's avatar Greg Kraynov Committed by Commit Bot

Simplify LazyNow and prepare it to move to //base.

Inspired by https://chromium-review.googlesource.com/1015743
but not moving it yet, because move will happen in one go.

Bug: 783309
Change-Id: I4ef38820accab561b73b28d9bfa9fcd2ee2f47a8
Reviewed-on: https://chromium-review.googlesource.com/1032773Reviewed-by: default avatarAlexander Timin <altimin@chromium.org>
Commit-Queue: Greg Kraynov <kraynov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#554433}
parent 195b614b
......@@ -8,10 +8,30 @@
namespace blink {
namespace scheduler {
LazyNow::LazyNow(base::TimeTicks now) : tick_clock_(nullptr), now_(now) {
DCHECK(!now.is_null());
}
LazyNow::LazyNow(const base::TickClock* tick_clock)
: tick_clock_(tick_clock), now_() {
DCHECK(tick_clock);
}
LazyNow::LazyNow(LazyNow&& move_from)
: tick_clock_(move_from.tick_clock_), now_(move_from.now_) {
move_from.tick_clock_ = nullptr;
move_from.now_ = base::TimeTicks();
}
base::TimeTicks LazyNow::Now() {
if (!now_)
// TickClock might return null values only in tests, we're okay with
// extra calls which might occur in that case.
if (now_.is_null()) {
DCHECK(tick_clock_); // It can fire only on use after std::move.
now_ = tick_clock_->NowTicks();
return now_.value();
}
return now_;
}
} // namespace scheduler
......
......@@ -17,21 +17,23 @@ namespace blink {
namespace scheduler {
// Now() is somewhat expensive so it makes sense not to call Now() unless we
// really need to.
// really need to and to avoid subsequent calls if already called once.
// LazyNow objects are expected to be short-living to represent accurate time.
class PLATFORM_EXPORT LazyNow {
public:
explicit LazyNow(base::TimeTicks now) : tick_clock_(nullptr), now_(now) {
}
explicit LazyNow(base::TimeTicks now);
explicit LazyNow(const base::TickClock* tick_clock);
explicit LazyNow(const base::TickClock* tick_clock)
: tick_clock_(tick_clock) {}
LazyNow(LazyNow&& move_from);
// Result will not be updated on any subsesequent calls.
base::TimeTicks Now();
private:
const base::TickClock* tick_clock_; // NOT OWNED
base::Optional<base::TimeTicks> now_;
const base::TickClock* tick_clock_; // Not owned.
base::TimeTicks now_;
DISALLOW_COPY_AND_ASSIGN(LazyNow);
};
} // namespace scheduler
......
......@@ -103,8 +103,8 @@ class TaskQueueSelectorTest : public testing::Test {
protected:
void SetUp() final {
virtual_time_domain_ = base::WrapUnique<VirtualTimeDomain>(
new VirtualTimeDomain(base::TimeTicks()));
virtual_time_domain_ = std::make_unique<VirtualTimeDomain>(
base::TimeTicks() + base::TimeDelta::FromSeconds(1));
for (size_t i = 0; i < kTaskQueueCount; i++) {
std::unique_ptr<TaskQueueImpl> task_queue =
std::make_unique<TaskQueueImpl>(nullptr, virtual_time_domain_.get(),
......
......@@ -233,7 +233,7 @@ TEST_F(TimeDomainTest, UnregisterQueue) {
TEST_F(TimeDomainTest, WakeUpReadyDelayedQueues) {
base::TimeDelta delay = base::TimeDelta::FromMilliseconds(50);
base::TimeTicks now = time_domain_->Now();
LazyNow lazy_now(now);
LazyNow lazy_now_1(now);
base::TimeTicks delayed_runtime = now + delay;
EXPECT_CALL(*time_domain_.get(), RequestWakeUpAt(_, delayed_runtime));
task_queue_->SetDelayedWakeUpForTesting(
......@@ -243,13 +243,13 @@ TEST_F(TimeDomainTest, WakeUpReadyDelayedQueues) {
ASSERT_TRUE(time_domain_->NextScheduledRunTime(&next_run_time));
EXPECT_EQ(delayed_runtime, next_run_time);
time_domain_->WakeUpReadyDelayedQueues(&lazy_now);
time_domain_->WakeUpReadyDelayedQueues(&lazy_now_1);
ASSERT_TRUE(time_domain_->NextScheduledRunTime(&next_run_time));
EXPECT_EQ(delayed_runtime, next_run_time);
time_domain_->SetNow(delayed_runtime);
lazy_now = time_domain_->CreateLazyNow();
time_domain_->WakeUpReadyDelayedQueues(&lazy_now);
LazyNow lazy_now_2(time_domain_->CreateLazyNow());
time_domain_->WakeUpReadyDelayedQueues(&lazy_now_2);
ASSERT_FALSE(time_domain_->NextScheduledRunTime(&next_run_time));
}
......
......@@ -654,8 +654,8 @@ TEST_P(TaskQueueThrottlerWithAutoAdvancingTimeTest,
base::TimeDelta::FromMilliseconds(1000)));
run_times.clear();
LazyNow lazy_now(clock_.get());
pool->DisableThrottling(&lazy_now);
LazyNow lazy_now_1(clock_.get());
pool->DisableThrottling(&lazy_now_1);
EXPECT_FALSE(pool->IsThrottlingEnabled());
// Pool should not be throttled now.
......@@ -669,8 +669,8 @@ TEST_P(TaskQueueThrottlerWithAutoAdvancingTimeTest,
base::TimeDelta::FromMilliseconds(2000)));
run_times.clear();
lazy_now = LazyNow(clock_.get());
pool->EnableThrottling(&lazy_now);
LazyNow lazy_now_2(clock_.get());
pool->EnableThrottling(&lazy_now_2);
EXPECT_TRUE(pool->IsThrottlingEnabled());
// Because time pool was disabled, time budget level did not replenish
......@@ -1014,16 +1014,16 @@ TEST_P(TaskQueueThrottlerWithAutoAdvancingTimeTest,
task_queue_throttler_->CreateCPUTimeBudgetPool("test");
task_queue_throttler_->IncreaseThrottleRefCount(timer_queue_.get());
LazyNow lazy_now(clock_.get());
pool->DisableThrottling(&lazy_now);
LazyNow lazy_now_1(clock_.get());
pool->DisableThrottling(&lazy_now_1);
pool->AddQueue(base::TimeTicks(), timer_queue_.get());
mock_task_runner_->RunUntilTime(base::TimeTicks() +
base::TimeDelta::FromMilliseconds(100));
lazy_now = LazyNow(clock_.get());
pool->EnableThrottling(&lazy_now);
LazyNow lazy_now_2(clock_.get());
pool->EnableThrottling(&lazy_now_2);
timer_queue_->PostDelayedTask(
FROM_HERE, base::BindOnce(&TestTask, &run_times, clock_.get()),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment