| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "flutter/fml/time/time_point.h" |
| 6 | |
| 7 | #include <atomic> |
| 8 | |
| 9 | #include "flutter/fml/build_config.h" |
| 10 | #include "flutter/fml/logging.h" |
| 11 | |
| 12 | #if defined(OS_FUCHSIA) |
| 13 | #include <zircon/syscalls.h> |
| 14 | #else |
| 15 | #include <chrono> |
| 16 | #endif |
| 17 | |
| 18 | namespace fml { |
| 19 | |
| 20 | #if defined(OS_FUCHSIA) |
| 21 | |
| 22 | // static |
| 23 | TimePoint TimePoint::Now() { |
| 24 | return TimePoint(zx_clock_get_monotonic()); |
| 25 | } |
| 26 | |
| 27 | TimePoint TimePoint::CurrentWallTime() { |
| 28 | return Now(); |
| 29 | } |
| 30 | |
| 31 | void TimePoint::SetClockSource(ClockSource source) {} |
| 32 | #else |
| 33 | |
| 34 | namespace { |
| 35 | std::atomic<TimePoint::ClockSource> gSteadyClockSource; |
| 36 | } |
| 37 | |
| 38 | template <typename Clock, typename Duration> |
| 39 | static int64_t NanosSinceEpoch( |
| 40 | std::chrono::time_point<Clock, Duration> time_point) { |
| 41 | const auto elapsed = time_point.time_since_epoch(); |
| 42 | return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count(); |
| 43 | } |
| 44 | |
| 45 | void TimePoint::SetClockSource(ClockSource source) { |
| 46 | gSteadyClockSource = source; |
| 47 | } |
| 48 | |
| 49 | TimePoint TimePoint::Now() { |
| 50 | if (gSteadyClockSource) { |
| 51 | return gSteadyClockSource.load()(); |
| 52 | } |
| 53 | const int64_t nanos = NanosSinceEpoch(time_point: std::chrono::steady_clock::now()); |
| 54 | return TimePoint(nanos); |
| 55 | } |
| 56 | |
| 57 | TimePoint TimePoint::CurrentWallTime() { |
| 58 | const int64_t nanos = NanosSinceEpoch(time_point: std::chrono::system_clock::now()); |
| 59 | return TimePoint(nanos); |
| 60 | } |
| 61 | |
| 62 | #endif |
| 63 | |
| 64 | } // namespace fml |
| 65 |
