Create std::chrono timestamp provider and move fml_unittests to use this (flutter/engine#27708)
This commit is contained in:
@@ -290,12 +290,15 @@ FILE: ../../../flutter/fml/thread_local.cc
|
||||
FILE: ../../../flutter/fml/thread_local.h
|
||||
FILE: ../../../flutter/fml/thread_local_unittests.cc
|
||||
FILE: ../../../flutter/fml/thread_unittests.cc
|
||||
FILE: ../../../flutter/fml/time/chrono_timestamp_provider.cc
|
||||
FILE: ../../../flutter/fml/time/chrono_timestamp_provider.h
|
||||
FILE: ../../../flutter/fml/time/time_delta.h
|
||||
FILE: ../../../flutter/fml/time/time_delta_unittest.cc
|
||||
FILE: ../../../flutter/fml/time/time_point.cc
|
||||
FILE: ../../../flutter/fml/time/time_point.h
|
||||
FILE: ../../../flutter/fml/time/time_point_unittest.cc
|
||||
FILE: ../../../flutter/fml/time/time_unittest.cc
|
||||
FILE: ../../../flutter/fml/time/timestamp_provider.h
|
||||
FILE: ../../../flutter/fml/trace_event.cc
|
||||
FILE: ../../../flutter/fml/trace_event.h
|
||||
FILE: ../../../flutter/fml/unique_fd.cc
|
||||
|
||||
@@ -80,6 +80,7 @@ source_set("fml") {
|
||||
"time/time_delta.h",
|
||||
"time/time_point.cc",
|
||||
"time/time_point.h",
|
||||
"time/timestamp_provider.h",
|
||||
"trace_event.cc",
|
||||
"trace_event.h",
|
||||
"unique_fd.cc",
|
||||
@@ -281,6 +282,8 @@ if (enable_unittests) {
|
||||
"task_source_unittests.cc",
|
||||
"thread_local_unittests.cc",
|
||||
"thread_unittests.cc",
|
||||
"time/chrono_timestamp_provider.cc",
|
||||
"time/chrono_timestamp_provider.h",
|
||||
"time/time_delta_unittest.cc",
|
||||
"time/time_point_unittest.cc",
|
||||
"time/time_unittest.cc",
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "flutter/fml/message_loop_task_queues.h"
|
||||
#include "flutter/fml/synchronization/count_down_latch.h"
|
||||
#include "flutter/fml/synchronization/waitable_event.h"
|
||||
#include "flutter/fml/time/chrono_timestamp_provider.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace fml {
|
||||
@@ -29,7 +30,7 @@ class TestWakeable : public fml::Wakeable {
|
||||
static int CountRemainingTasks(fml::RefPtr<MessageLoopTaskQueues> task_queue,
|
||||
const TaskQueueId& queue_id,
|
||||
bool run_invocation = false) {
|
||||
const auto now = fml::TimePoint::Now();
|
||||
const auto now = ChronoTicksSinceEpoch();
|
||||
int count = 0;
|
||||
fml::closure invocation;
|
||||
do {
|
||||
@@ -53,12 +54,12 @@ TEST(MessageLoopTaskQueueMergeUnmerge,
|
||||
auto queue_id_2 = task_queue->CreateTaskQueue();
|
||||
|
||||
task_queue->RegisterTask(
|
||||
queue_id_1, []() {}, fml::TimePoint::Now());
|
||||
queue_id_1, []() {}, ChronoTicksSinceEpoch());
|
||||
ASSERT_EQ(1u, task_queue->GetNumPendingTasks(queue_id_1));
|
||||
|
||||
task_queue->Merge(queue_id_1, queue_id_2);
|
||||
task_queue->RegisterTask(
|
||||
queue_id_1, []() {}, fml::TimePoint::Now());
|
||||
queue_id_1, []() {}, ChronoTicksSinceEpoch());
|
||||
|
||||
ASSERT_EQ(2u, task_queue->GetNumPendingTasks(queue_id_1));
|
||||
ASSERT_EQ(0u, task_queue->GetNumPendingTasks(queue_id_2));
|
||||
@@ -72,7 +73,7 @@ TEST(MessageLoopTaskQueueMergeUnmerge,
|
||||
auto queue_id_2 = task_queue->CreateTaskQueue();
|
||||
|
||||
task_queue->RegisterTask(
|
||||
queue_id_2, []() {}, fml::TimePoint::Now());
|
||||
queue_id_2, []() {}, ChronoTicksSinceEpoch());
|
||||
ASSERT_EQ(1u, task_queue->GetNumPendingTasks(queue_id_2));
|
||||
|
||||
task_queue->Merge(queue_id_1, queue_id_2);
|
||||
@@ -87,9 +88,9 @@ TEST(MessageLoopTaskQueueMergeUnmerge, MergeUnmergeTasksPreserved) {
|
||||
auto queue_id_2 = task_queue->CreateTaskQueue();
|
||||
|
||||
task_queue->RegisterTask(
|
||||
queue_id_1, []() {}, fml::TimePoint::Now());
|
||||
queue_id_1, []() {}, ChronoTicksSinceEpoch());
|
||||
task_queue->RegisterTask(
|
||||
queue_id_2, []() {}, fml::TimePoint::Now());
|
||||
queue_id_2, []() {}, ChronoTicksSinceEpoch());
|
||||
|
||||
ASSERT_EQ(1u, task_queue->GetNumPendingTasks(queue_id_1));
|
||||
ASSERT_EQ(1u, task_queue->GetNumPendingTasks(queue_id_2));
|
||||
@@ -145,7 +146,7 @@ TEST(MessageLoopTaskQueueMergeUnmerge, MergeInvokesBothWakeables) {
|
||||
new TestWakeable([&](fml::TimePoint wake_time) { latch.CountDown(); }));
|
||||
|
||||
task_queue->RegisterTask(
|
||||
queue_id_1, []() {}, fml::TimePoint::Now());
|
||||
queue_id_1, []() {}, ChronoTicksSinceEpoch());
|
||||
|
||||
task_queue->Merge(queue_id_1, queue_id_2);
|
||||
|
||||
@@ -171,9 +172,9 @@ TEST(MessageLoopTaskQueueMergeUnmerge,
|
||||
new TestWakeable([&](fml::TimePoint wake_time) { latch_2.Signal(); }));
|
||||
|
||||
task_queue->RegisterTask(
|
||||
queue_id_1, []() {}, fml::TimePoint::Now());
|
||||
queue_id_1, []() {}, ChronoTicksSinceEpoch());
|
||||
task_queue->RegisterTask(
|
||||
queue_id_2, []() {}, fml::TimePoint::Now());
|
||||
queue_id_2, []() {}, ChronoTicksSinceEpoch());
|
||||
|
||||
task_queue->Merge(queue_id_1, queue_id_2);
|
||||
task_queue->Unmerge(queue_id_1);
|
||||
@@ -197,7 +198,7 @@ TEST(MessageLoopTaskQueueMergeUnmerge, GetTasksToRunNowBlocksMerge) {
|
||||
merge_end;
|
||||
|
||||
task_queue->RegisterTask(
|
||||
queue_id_1, []() {}, fml::TimePoint::Now());
|
||||
queue_id_1, []() {}, ChronoTicksSinceEpoch());
|
||||
task_queue->SetWakeable(queue_id_1,
|
||||
new TestWakeable([&](fml::TimePoint wake_time) {
|
||||
wake_up_start.Signal();
|
||||
@@ -246,10 +247,10 @@ TEST(MessageLoopTaskQueueMergeUnmerge,
|
||||
|
||||
task_queue->RegisterTask(
|
||||
queue_id_2, [&]() { task_queue->Merge(queue_id_1, queue_id_2); },
|
||||
fml::TimePoint::Now());
|
||||
ChronoTicksSinceEpoch());
|
||||
|
||||
task_queue->RegisterTask(
|
||||
queue_id_2, []() {}, fml::TimePoint::Now());
|
||||
queue_id_2, []() {}, ChronoTicksSinceEpoch());
|
||||
|
||||
ASSERT_EQ(CountRemainingTasks(task_queue, queue_id_2, true), 1);
|
||||
ASSERT_EQ(CountRemainingTasks(task_queue, queue_id_1, true), 1);
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "flutter/fml/synchronization/count_down_latch.h"
|
||||
#include "flutter/fml/synchronization/waitable_event.h"
|
||||
#include "flutter/fml/time/chrono_timestamp_provider.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace fml {
|
||||
@@ -55,7 +56,7 @@ TEST(MessageLoopTaskQueue, RegisterTwoTasksAndCount) {
|
||||
auto task_queue = fml::MessageLoopTaskQueues::GetInstance();
|
||||
auto queue_id = task_queue->CreateTaskQueue();
|
||||
task_queue->RegisterTask(
|
||||
queue_id, [] {}, fml::TimePoint::Now());
|
||||
queue_id, [] {}, ChronoTicksSinceEpoch());
|
||||
task_queue->RegisterTask(
|
||||
queue_id, [] {}, fml::TimePoint::Max());
|
||||
ASSERT_TRUE(task_queue->HasPendingTasks(queue_id));
|
||||
@@ -69,13 +70,13 @@ TEST(MessageLoopTaskQueue, PreserveTaskOrdering) {
|
||||
|
||||
// order: 0
|
||||
task_queue->RegisterTask(
|
||||
queue_id, [&test_val]() { test_val = 1; }, fml::TimePoint::Now());
|
||||
queue_id, [&test_val]() { test_val = 1; }, ChronoTicksSinceEpoch());
|
||||
|
||||
// order: 1
|
||||
task_queue->RegisterTask(
|
||||
queue_id, [&test_val]() { test_val = 2; }, fml::TimePoint::Now());
|
||||
queue_id, [&test_val]() { test_val = 2; }, ChronoTicksSinceEpoch());
|
||||
|
||||
const auto now = fml::TimePoint::Now();
|
||||
const auto now = ChronoTicksSinceEpoch();
|
||||
int expected_value = 1;
|
||||
for (;;) {
|
||||
fml::closure invocation = task_queue->GetNextTaskToRun(queue_id, now);
|
||||
@@ -124,7 +125,7 @@ TEST(MessageLoopTaskQueue, WakeUpIndependentOfTime) {
|
||||
[&num_wakes](fml::TimePoint wake_time) { ++num_wakes; }));
|
||||
|
||||
task_queue->RegisterTask(
|
||||
queue_id, []() {}, fml::TimePoint::Now());
|
||||
queue_id, []() {}, ChronoTicksSinceEpoch());
|
||||
task_queue->RegisterTask(
|
||||
queue_id, []() {}, fml::TimePoint::Max());
|
||||
|
||||
@@ -147,7 +148,7 @@ TEST(MessageLoopTaskQueue, WokenUpWithNewerTime) {
|
||||
task_queue->RegisterTask(
|
||||
queue_id, []() {}, fml::TimePoint::Max());
|
||||
|
||||
const auto now = fml::TimePoint::Now();
|
||||
const auto now = ChronoTicksSinceEpoch();
|
||||
expected = now;
|
||||
task_queue->RegisterTask(
|
||||
queue_id, []() {}, now);
|
||||
@@ -221,7 +222,7 @@ TEST(MessageLoopTaskQueue, ConcurrentQueueAndTaskCreatingCounts) {
|
||||
task_queue_ids[std::rand() % kTaskQueuesCount];
|
||||
const auto empty_task = []() {};
|
||||
// The timepoint doesn't matter as the queue is never going to be drained.
|
||||
const auto task_timepoint = fml::TimePoint::Now();
|
||||
const auto task_timepoint = ChronoTicksSinceEpoch();
|
||||
|
||||
task_queues->RegisterTask(current_task_queue_id, empty_task,
|
||||
task_timepoint);
|
||||
@@ -271,8 +272,8 @@ TEST(MessageLoopTaskQueue, RegisterTaskWakesUpOwnerQueue) {
|
||||
ASSERT_FALSE(true);
|
||||
}));
|
||||
|
||||
auto time1 = fml::TimePoint::Now() + fml::TimeDelta::FromMilliseconds(1);
|
||||
auto time2 = fml::TimePoint::Now() + fml::TimeDelta::FromMilliseconds(2);
|
||||
auto time1 = ChronoTicksSinceEpoch() + fml::TimeDelta::FromMilliseconds(1);
|
||||
auto time2 = ChronoTicksSinceEpoch() + fml::TimeDelta::FromMilliseconds(2);
|
||||
|
||||
ASSERT_EQ(0UL, wakes.size());
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "flutter/fml/synchronization/count_down_latch.h"
|
||||
#include "flutter/fml/synchronization/waitable_event.h"
|
||||
#include "flutter/fml/task_runner.h"
|
||||
#include "flutter/fml/time/chrono_timestamp_provider.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#define TIMESENSITIVE(x) TimeSensitiveTest_##x
|
||||
@@ -116,7 +117,7 @@ TEST(MessageLoop, DelayedTasksAtSameTimeAreRunInOrder) {
|
||||
auto& loop = fml::MessageLoop::GetCurrent();
|
||||
size_t current = 0;
|
||||
const auto now_plus_some =
|
||||
fml::TimePoint::Now() + fml::TimeDelta::FromMilliseconds(2);
|
||||
fml::ChronoTicksSinceEpoch() + fml::TimeDelta::FromMilliseconds(2);
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
loop.GetTaskRunner()->PostTaskForTime(
|
||||
PLATFORM_SPECIFIC_CAPTURE(&terminated, i, ¤t)() {
|
||||
@@ -159,10 +160,10 @@ TEST(MessageLoop, TIMESENSITIVE(SingleDelayedTaskByDelta)) {
|
||||
std::thread thread([&checked]() {
|
||||
fml::MessageLoop::EnsureInitializedForCurrentThread();
|
||||
auto& loop = fml::MessageLoop::GetCurrent();
|
||||
auto begin = fml::TimePoint::Now();
|
||||
auto begin = fml::ChronoTicksSinceEpoch();
|
||||
loop.GetTaskRunner()->PostDelayedTask(
|
||||
[begin, &checked]() {
|
||||
auto delta = fml::TimePoint::Now() - begin;
|
||||
auto delta = fml::ChronoTicksSinceEpoch() - begin;
|
||||
auto ms = delta.ToMillisecondsF();
|
||||
ASSERT_GE(ms, 3);
|
||||
ASSERT_LE(ms, 7);
|
||||
@@ -181,17 +182,17 @@ TEST(MessageLoop, TIMESENSITIVE(SingleDelayedTaskForTime)) {
|
||||
std::thread thread([&checked]() {
|
||||
fml::MessageLoop::EnsureInitializedForCurrentThread();
|
||||
auto& loop = fml::MessageLoop::GetCurrent();
|
||||
auto begin = fml::TimePoint::Now();
|
||||
auto begin = fml::ChronoTicksSinceEpoch();
|
||||
loop.GetTaskRunner()->PostTaskForTime(
|
||||
[begin, &checked]() {
|
||||
auto delta = fml::TimePoint::Now() - begin;
|
||||
auto delta = fml::ChronoTicksSinceEpoch() - begin;
|
||||
auto ms = delta.ToMillisecondsF();
|
||||
ASSERT_GE(ms, 3);
|
||||
ASSERT_LE(ms, 7);
|
||||
checked = true;
|
||||
fml::MessageLoop::GetCurrent().Terminate();
|
||||
},
|
||||
fml::TimePoint::Now() + fml::TimeDelta::FromMilliseconds(5));
|
||||
fml::ChronoTicksSinceEpoch() + fml::TimeDelta::FromMilliseconds(5));
|
||||
loop.Run();
|
||||
});
|
||||
thread.join();
|
||||
@@ -205,10 +206,10 @@ TEST(MessageLoop, TIMESENSITIVE(MultipleDelayedTasksWithIncreasingDeltas)) {
|
||||
fml::MessageLoop::EnsureInitializedForCurrentThread();
|
||||
auto& loop = fml::MessageLoop::GetCurrent();
|
||||
for (int target_ms = 0 + 2; target_ms < count + 2; target_ms++) {
|
||||
auto begin = fml::TimePoint::Now();
|
||||
auto begin = fml::ChronoTicksSinceEpoch();
|
||||
loop.GetTaskRunner()->PostDelayedTask(
|
||||
PLATFORM_SPECIFIC_CAPTURE(begin, target_ms, &checked)() {
|
||||
auto delta = fml::TimePoint::Now() - begin;
|
||||
auto delta = fml::ChronoTicksSinceEpoch() - begin;
|
||||
auto ms = delta.ToMillisecondsF();
|
||||
ASSERT_GE(ms, target_ms - 2);
|
||||
ASSERT_LE(ms, target_ms + 2);
|
||||
@@ -232,10 +233,10 @@ TEST(MessageLoop, TIMESENSITIVE(MultipleDelayedTasksWithDecreasingDeltas)) {
|
||||
fml::MessageLoop::EnsureInitializedForCurrentThread();
|
||||
auto& loop = fml::MessageLoop::GetCurrent();
|
||||
for (int target_ms = count + 2; target_ms > 0 + 2; target_ms--) {
|
||||
auto begin = fml::TimePoint::Now();
|
||||
auto begin = fml::ChronoTicksSinceEpoch();
|
||||
loop.GetTaskRunner()->PostDelayedTask(
|
||||
PLATFORM_SPECIFIC_CAPTURE(begin, target_ms, &checked)() {
|
||||
auto delta = fml::TimePoint::Now() - begin;
|
||||
auto delta = fml::ChronoTicksSinceEpoch() - begin;
|
||||
auto ms = delta.ToMillisecondsF();
|
||||
ASSERT_GE(ms, target_ms - 2);
|
||||
ASSERT_LE(ms, target_ms + 2);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/fml/task_source.h"
|
||||
#include "flutter/fml/time/chrono_timestamp_provider.h"
|
||||
#include "flutter/fml/time/time_delta.h"
|
||||
#include "flutter/fml/time/time_point.h"
|
||||
#include "gtest/gtest.h"
|
||||
@@ -17,24 +18,24 @@ namespace testing {
|
||||
TEST(TaskSourceTests, SimpleInitialization) {
|
||||
TaskSource task_source = TaskSource(TaskQueueId(1));
|
||||
task_source.RegisterTask(
|
||||
{1, [] {}, fml::TimePoint::Now(), TaskSourceGrade::kUnspecified});
|
||||
{1, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kUnspecified});
|
||||
ASSERT_EQ(task_source.GetNumPendingTasks(), 1u);
|
||||
}
|
||||
|
||||
TEST(TaskSourceTests, MultipleTaskGrades) {
|
||||
TaskSource task_source = TaskSource(TaskQueueId(1));
|
||||
task_source.RegisterTask(
|
||||
{1, [] {}, fml::TimePoint::Now(), TaskSourceGrade::kUnspecified});
|
||||
{1, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kUnspecified});
|
||||
task_source.RegisterTask(
|
||||
{2, [] {}, fml::TimePoint::Now(), TaskSourceGrade::kUserInteraction});
|
||||
{2, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kUserInteraction});
|
||||
task_source.RegisterTask(
|
||||
{3, [] {}, fml::TimePoint::Now(), TaskSourceGrade::kDartMicroTasks});
|
||||
{3, [] {}, ChronoTicksSinceEpoch(), TaskSourceGrade::kDartMicroTasks});
|
||||
ASSERT_EQ(task_source.GetNumPendingTasks(), 3u);
|
||||
}
|
||||
|
||||
TEST(TaskSourceTests, SimpleOrdering) {
|
||||
TaskSource task_source = TaskSource(TaskQueueId(1));
|
||||
auto time_stamp = fml::TimePoint::Now();
|
||||
auto time_stamp = ChronoTicksSinceEpoch();
|
||||
int value = 0;
|
||||
task_source.RegisterTask(
|
||||
{1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kUnspecified});
|
||||
@@ -51,7 +52,7 @@ TEST(TaskSourceTests, SimpleOrdering) {
|
||||
|
||||
TEST(TaskSourceTests, SimpleOrderingMultiTaskHeaps) {
|
||||
TaskSource task_source = TaskSource(TaskQueueId(1));
|
||||
auto time_stamp = fml::TimePoint::Now();
|
||||
auto time_stamp = ChronoTicksSinceEpoch();
|
||||
int value = 0;
|
||||
task_source.RegisterTask(
|
||||
{1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kDartMicroTasks});
|
||||
@@ -71,7 +72,7 @@ TEST(TaskSourceTests, SimpleOrderingMultiTaskHeaps) {
|
||||
|
||||
TEST(TaskSourceTests, OrderingMultiTaskHeapsSecondaryPaused) {
|
||||
TaskSource task_source = TaskSource(TaskQueueId(1));
|
||||
auto time_stamp = fml::TimePoint::Now();
|
||||
auto time_stamp = ChronoTicksSinceEpoch();
|
||||
int value = 0;
|
||||
task_source.RegisterTask(
|
||||
{1, [&] { value = 1; }, time_stamp, TaskSourceGrade::kDartMicroTasks});
|
||||
|
||||
27
engine/src/flutter/fml/time/chrono_timestamp_provider.cc
Normal file
27
engine/src/flutter/fml/time/chrono_timestamp_provider.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright 2013 The Flutter 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 "flutter/fml/time/chrono_timestamp_provider.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include "fml/time/time_delta.h"
|
||||
|
||||
namespace fml {
|
||||
|
||||
ChronoTimestampProvider::ChronoTimestampProvider() = default;
|
||||
|
||||
ChronoTimestampProvider::~ChronoTimestampProvider() = default;
|
||||
|
||||
fml::TimePoint ChronoTimestampProvider::Now() {
|
||||
const auto chrono_time_point = std::chrono::steady_clock::now();
|
||||
const auto ticks_since_epoch = chrono_time_point.time_since_epoch().count();
|
||||
return fml::TimePoint::FromTicks(ticks_since_epoch);
|
||||
}
|
||||
|
||||
fml::TimePoint ChronoTicksSinceEpoch() {
|
||||
return ChronoTimestampProvider::Instance().Now();
|
||||
}
|
||||
|
||||
} // namespace fml
|
||||
40
engine/src/flutter/fml/time/chrono_timestamp_provider.h
Normal file
40
engine/src/flutter/fml/time/chrono_timestamp_provider.h
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_FML_TIME_CHRONO_TIMESTAMP_PROVIDER_H_
|
||||
#define FLUTTER_FML_TIME_CHRONO_TIMESTAMP_PROVIDER_H_
|
||||
|
||||
#include "flutter/fml/time/timestamp_provider.h"
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "fml/time/time_point.h"
|
||||
|
||||
namespace fml {
|
||||
|
||||
/// TimestampProvider implementation that is backed by std::chrono::steady_clock
|
||||
/// meant to be used only in tests for `fml`. Other components needing the
|
||||
/// current time ticks since epoch should instantiate their own time stamp
|
||||
/// provider backed by Dart clock.
|
||||
class ChronoTimestampProvider : TimestampProvider {
|
||||
public:
|
||||
static ChronoTimestampProvider& Instance() {
|
||||
static ChronoTimestampProvider instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
~ChronoTimestampProvider() override;
|
||||
|
||||
fml::TimePoint Now() override;
|
||||
|
||||
private:
|
||||
ChronoTimestampProvider();
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ChronoTimestampProvider);
|
||||
};
|
||||
|
||||
fml::TimePoint ChronoTicksSinceEpoch();
|
||||
|
||||
} // namespace fml
|
||||
|
||||
#endif // FLUTTER_FML_TIME_CHRONO_TIMESTAMP_PROVIDER_H_
|
||||
@@ -39,6 +39,10 @@ class TimePoint {
|
||||
return TimePoint(ticks.ToNanoseconds());
|
||||
}
|
||||
|
||||
static constexpr TimePoint FromTicks(int64_t ticks) {
|
||||
return TimePoint(ticks);
|
||||
}
|
||||
|
||||
TimeDelta ToEpochDelta() const { return TimeDelta::FromNanoseconds(ticks_); }
|
||||
|
||||
// Compute the difference between two time points.
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flutter/fml/time/time_point.h"
|
||||
#include "flutter/fml/time/chrono_timestamp_provider.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
@@ -10,8 +10,8 @@ namespace fml {
|
||||
namespace {
|
||||
|
||||
TEST(TimePoint, Control) {
|
||||
EXPECT_LT(TimePoint::Min(), TimePoint::Now());
|
||||
EXPECT_GT(TimePoint::Max(), TimePoint::Now());
|
||||
EXPECT_LT(TimePoint::Min(), ChronoTicksSinceEpoch());
|
||||
EXPECT_GT(TimePoint::Max(), ChronoTicksSinceEpoch());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -4,17 +4,17 @@
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include "flutter/fml/time/chrono_timestamp_provider.h"
|
||||
#include "flutter/fml/time/time_delta.h"
|
||||
#include "flutter/fml/time/time_point.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace fml {
|
||||
namespace {
|
||||
|
||||
TEST(Time, Now) {
|
||||
auto start = TimePoint::Now();
|
||||
auto start = ChronoTicksSinceEpoch();
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
auto now = TimePoint::Now();
|
||||
auto now = ChronoTicksSinceEpoch();
|
||||
EXPECT_GE(now, start);
|
||||
std::this_thread::yield();
|
||||
}
|
||||
|
||||
26
engine/src/flutter/fml/time/timestamp_provider.h
Normal file
26
engine/src/flutter/fml/time/timestamp_provider.h
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_FML_TIME_TIMESTAMP_PROVIDER_H_
|
||||
#define FLUTTER_FML_TIME_TIMESTAMP_PROVIDER_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "flutter/fml/time/time_point.h"
|
||||
|
||||
namespace fml {
|
||||
|
||||
/// Pluggable provider of monotonic timestamps. Invocations of `Now` must return
|
||||
/// unique values. Any two consecutive invocations must be ordered.
|
||||
class TimestampProvider {
|
||||
public:
|
||||
virtual ~TimestampProvider(){};
|
||||
|
||||
// Returns the number of ticks elapsed by a monotonic clock since epoch.
|
||||
virtual fml::TimePoint Now() = 0;
|
||||
};
|
||||
|
||||
} // namespace fml
|
||||
|
||||
#endif // FLUTTER_FML_TIME_TIMESTAMP_PROVIDER_H_
|
||||
Reference in New Issue
Block a user