Skip to content

Commit

Permalink
TimePoint::Now uses DartTimestampProvider (flutter#27737)
Browse files Browse the repository at this point in the history
  • Loading branch information
iskakaushik committed Aug 2, 2021
1 parent bf06b19 commit d0343f1
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ 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/dart_timestamp_provider.cc
FILE: ../../../flutter/fml/time/dart_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
Expand Down
2 changes: 2 additions & 0 deletions fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ source_set("fml") {
"thread.h",
"thread_local.cc",
"thread_local.h",
"time/dart_timestamp_provider.cc",
"time/dart_timestamp_provider.h",
"time/time_delta.h",
"time/time_point.cc",
"time/time_point.h",
Expand Down
2 changes: 0 additions & 2 deletions fml/time/chrono_timestamp_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include <chrono>

#include "fml/time/time_delta.h"

namespace fml {

ChronoTimestampProvider::ChronoTimestampProvider() = default;
Expand Down
2 changes: 1 addition & 1 deletion fml/time/chrono_timestamp_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "flutter/fml/time/timestamp_provider.h"

#include "flutter/fml/macros.h"
#include "fml/time/time_point.h"
#include "flutter/fml/time/time_point.h"

namespace fml {

Expand Down
38 changes: 38 additions & 0 deletions fml/time/dart_timestamp_provider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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/dart_timestamp_provider.h"

#include "dart_tools_api.h"

namespace fml {

DartTimestampProvider::DartTimestampProvider() = default;

DartTimestampProvider::~DartTimestampProvider() = default;

int64_t DartTimestampProvider::ConvertToNanos(int64_t ticks,
int64_t frequency) {
int64_t nano_seconds = (ticks / frequency) * kNanosPerSecond;
int64_t leftover_ticks = ticks % frequency;
int64_t leftover_nanos = (leftover_ticks * kNanosPerSecond) / frequency;
return nano_seconds + leftover_nanos;
}

fml::TimePoint DartTimestampProvider::Now() {
const int64_t ticks = Dart_TimelineGetTicks();
const int64_t frequency = Dart_TimelineGetTicksFrequency();
// optimization for the most common case.
if (frequency != kNanosPerSecond) {
return fml::TimePoint::FromTicks(ConvertToNanos(ticks, frequency));
} else {
return fml::TimePoint::FromTicks(ticks);
}
}

fml::TimePoint DartTimelineTicksSinceEpoch() {
return DartTimestampProvider::Instance().Now();
}

} // namespace fml
41 changes: 41 additions & 0 deletions fml/time/dart_timestamp_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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_DART_TIMESTAMP_PROVIDER_H_
#define FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_

#include "flutter/fml/time/timestamp_provider.h"

#include "flutter/fml/macros.h"
#include "flutter/fml/time/time_point.h"

namespace fml {

fml::TimePoint DartTimelineTicksSinceEpoch();

/// TimestampProvider implementation that is backed by Dart_TimelineGetTicks
class DartTimestampProvider : TimestampProvider {
public:
static DartTimestampProvider& Instance() {
static DartTimestampProvider instance;
return instance;
}

~DartTimestampProvider() override;

fml::TimePoint Now() override;

private:
static constexpr int64_t kNanosPerSecond = 1000000000;

int64_t ConvertToNanos(int64_t ticks, int64_t frequency);

DartTimestampProvider();

FML_DISALLOW_COPY_AND_ASSIGN(DartTimestampProvider);
};

} // namespace fml

#endif // FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_
4 changes: 2 additions & 2 deletions fml/time/time_point.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "flutter/fml/build_config.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/time/dart_timestamp_provider.h"

#if defined(OS_FUCHSIA)
#include <zircon/syscalls.h>
Expand Down Expand Up @@ -36,8 +37,7 @@ static int64_t NanosSinceEpoch(
}

TimePoint TimePoint::Now() {
const int64_t nanos = NanosSinceEpoch(std::chrono::steady_clock::now());
return TimePoint(nanos);
return DartTimelineTicksSinceEpoch();
}

TimePoint TimePoint::CurrentWallTime() {
Expand Down
1 change: 1 addition & 0 deletions fml/time/time_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TimePoint {
return TimePoint(ticks.ToNanoseconds());
}

// Expects ticks in nanos.
static constexpr TimePoint FromTicks(int64_t ticks) {
return TimePoint(ticks);
}
Expand Down
17 changes: 17 additions & 0 deletions fml/time/time_point_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

#include "flutter/fml/time/chrono_timestamp_provider.h"

#include "flutter/fml/time/dart_timestamp_provider.h"

#include <thread>

#include "gtest/gtest.h"

namespace fml {
Expand All @@ -14,5 +18,18 @@ TEST(TimePoint, Control) {
EXPECT_GT(TimePoint::Max(), ChronoTicksSinceEpoch());
}

TEST(TimePoint, DartClockIsMonotonic) {
using namespace std::chrono_literals;
const auto t1 = DartTimelineTicksSinceEpoch();
std::this_thread::sleep_for(1us);
const auto t2 = DartTimelineTicksSinceEpoch();
std::this_thread::sleep_for(1us);
const auto t3 = DartTimelineTicksSinceEpoch();
EXPECT_LT(TimePoint::Min(), t1);
EXPECT_LE(t1, t2);
EXPECT_LE(t2, t3);
EXPECT_LT(t3, TimePoint::Max());
}

} // namespace
} // namespace fml

0 comments on commit d0343f1

Please sign in to comment.