mark a. foltz | c28ca40 | 2018-07-19 16:11:32 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium 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 | #ifndef PLATFORM_API_TIME_H_ |
| 6 | #define PLATFORM_API_TIME_H_ |
| 7 | |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame^] | 8 | #include <chrono> |
mark a. foltz | c28ca40 | 2018-07-19 16:11:32 -0700 | [diff] [blame] | 9 | |
| 10 | namespace openscreen { |
| 11 | namespace platform { |
| 12 | |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame^] | 13 | // For proper behavior of the OpenScreen library, the Clock implementation must |
| 14 | // tick at least 10000 times per second. |
| 15 | using kRequiredClockResolution = std::ratio<1, 10000>; |
| 16 | |
| 17 | // A monotonic clock that meets all the C++14 requirements of a TrivialClock, |
| 18 | // for use with the chrono library. The default platform implementation bases |
| 19 | // this on std::chrono::steady_clock or std::chrono::high_resolution_clock, but |
| 20 | // a custom implementation may use a different source of time (e.g., an |
| 21 | // embedder's time library, a simulated time source, or a mock). |
| 22 | class Clock { |
btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame] | 23 | public: |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame^] | 24 | // TrivialClock named requirements: std::chrono templates can/may use these. |
| 25 | using duration = std::chrono::microseconds; |
| 26 | using rep = duration::rep; |
| 27 | using period = duration::period; |
| 28 | using time_point = std::chrono::time_point<Clock, duration>; |
| 29 | static constexpr bool is_steady = true; |
mark a. foltz | c28ca40 | 2018-07-19 16:11:32 -0700 | [diff] [blame] | 30 | |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame^] | 31 | static time_point now() noexcept; |
btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame] | 32 | |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame^] | 33 | // In <chrono>, a Clock is just some type properties plus a static now() |
| 34 | // function. So, there's nothing to instantiate here. |
| 35 | Clock() = delete; |
| 36 | ~Clock() = delete; |
btolsch | c11dc34 | 2019-01-16 16:37:34 -0800 | [diff] [blame] | 37 | |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame^] | 38 | // "Trivially copyable" is necessary for use in std::atomic<>. |
| 39 | static_assert(std::is_trivially_copyable<duration>(), |
| 40 | "duration is not trivially copyable"); |
| 41 | static_assert(std::is_trivially_copyable<time_point>(), |
| 42 | "time_point is not trivially copyable"); |
btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame^] | 45 | // Convenience, for injecting Clocks into classes. Note: The 'noexcept' keyword |
| 46 | // is dropped here to avoid a well-known Clang compiler warning (about an |
| 47 | // upcoming C++20 ABI change). |
| 48 | using ClockNowFunctionPtr = Clock::time_point (*)(); |
| 49 | |
| 50 | // Returns the number of seconds since UNIX epoch (1 Jan 1970, midnight) |
| 51 | // according to the wall clock, which is subject to adjustments (e.g., via |
| 52 | // NTP). Note that this is NOT the same time source as Clock::now() above, and |
| 53 | // is NOT guaranteed to be monotonically non-decreasing. |
| 54 | std::chrono::seconds GetWallTimeSinceUnixEpoch() noexcept; |
mark a. foltz | c28ca40 | 2018-07-19 16:11:32 -0700 | [diff] [blame] | 55 | |
| 56 | } // namespace platform |
| 57 | } // namespace openscreen |
| 58 | |
btolsch | a21e8ed | 2018-08-30 15:13:48 -0700 | [diff] [blame] | 59 | #endif // PLATFORM_API_TIME_H_ |