Commit 96e1a8b6 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

mark clocks with spec tag

Summary:
So that if `std::chrono::steady_clock` and `folly::chrono::coarse_steady_clock` have the same spec, time points from the two clocks are compatible.

The intention is to provide a generic interface to determine whether it is safe to interpret a time-point from one clock as a time-point from another clock.

For example, it is safe to interpret a time-point from the coarse-steady-clock as a time-point from the steady-clock, and the code marks this by giving both clocks the same spec tag, but it is not safe to interpret a time-point from the system-clock as a time-point from the steady-clock, and the code marks this by giving the two clocks different or void spec tags.

Concrete code can already do this at will. The idea is to have a generic way to do this, from within an algorithm templated over all the clock types and having no special knowledge of any clock type.

Reviewed By: simpkins

Differential Revision: D29222091

fbshipit-source-id: b9442acd08716a46e704b6d800cc1cd1d0583e25
parent 2157eecd
......@@ -175,7 +175,43 @@ constexpr std::chrono::time_point<Clock, To> round(
namespace folly {
namespace chrono {
// steady_clock_spec
//
// All clocks with this spec share epoch and tick rate.
struct steady_clock_spec {};
// system_clock_spec
//
// All clocks with this spec share epoch and tick rate.
struct system_clock_spec {};
// clock_traits
//
// Detects and reexports per-clock traits.
//
// Specializable for clocks for which trait detection fails..
template <typename Clock>
struct clock_traits {
private:
template <typename C>
using detect_spec_ = typename C::folly_spec;
public:
using spec = detected_or_t<void, detect_spec_, Clock>;
};
template <>
struct clock_traits<std::chrono::steady_clock> {
using spec = steady_clock_spec;
};
template <>
struct clock_traits<std::chrono::system_clock> {
using spec = system_clock_spec;
};
struct coarse_steady_clock {
using folly_spec = steady_clock_spec;
using duration = std::chrono::steady_clock::duration;
using rep = duration::rep;
using period = duration::period;
......@@ -200,6 +236,8 @@ struct coarse_steady_clock {
};
struct coarse_system_clock {
using folly_spec = system_clock_spec;
using duration = std::chrono::system_clock::duration;
using rep = duration::rep;
using period = duration::period;
......
......@@ -21,11 +21,19 @@
using namespace std::chrono;
using namespace folly::chrono;
static_assert( //
std::is_same_v<
clock_traits<steady_clock>::spec,
clock_traits<coarse_steady_clock>::spec>);
static_assert( //
std::is_same_v<
steady_clock::time_point::duration,
coarse_steady_clock::time_point::duration>);
static_assert( //
std::is_same_v<
clock_traits<system_clock>::spec,
clock_traits<coarse_system_clock>::spec>);
static_assert( //
std::is_same_v<
system_clock::time_point::duration,
......
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