From 371298264a64c0f0634ee973ec7a9231786316e3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?P=C3=A1draig=20Brady?= Date: Mon, 22 Jan 2018 18:26:46 -0800 Subject: [PATCH] support folly::chrono::coarse_steady_clock with c++17 Summary: Move the coarse_steady_clock definition outside the c++ < 17 specific defines, as otherwise it's not defined when using -std=gnu++17. Reviewed By: yfeldblum Differential Revision: D6780601 fbshipit-source-id: 35cc5aa10db7d890e17bf03cc5e80803a9356d7a --- folly/Chrono.h | 63 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/folly/Chrono.h b/folly/Chrono.h index 72b2311c..2345e9e8 100644 --- a/folly/Chrono.h +++ b/folly/Chrono.h @@ -45,35 +45,6 @@ namespace chrono { namespace folly { namespace chrono { -namespace detail { -[[noreturn]] FOLLY_NOINLINE inline void throw_coarse_steady_clock_now_exn() { - throw std::runtime_error("Error using CLOCK_MONOTONIC_COARSE."); -} -} // namespace detail - -struct coarse_steady_clock { - using rep = std::chrono::milliseconds::rep; - using period = std::chrono::milliseconds::period; - using duration = std::chrono::duration; - using time_point = std::chrono::time_point; - constexpr static bool is_steady = true; - - static time_point now() { -#ifndef CLOCK_MONOTONIC_COARSE - return time_point(std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch())); -#else - timespec ts; - auto ret = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); - if (ret != 0) { - detail::throw_coarse_steady_clock_now_exn(); - } - return time_point(std::chrono::duration_cast( - std::chrono::seconds(ts.tv_sec) + - std::chrono::nanoseconds(ts.tv_nsec))); -#endif - } -}; namespace detail { @@ -185,3 +156,37 @@ constexpr std::chrono::time_point round( } // namespace folly #endif + +namespace folly { +namespace chrono { +namespace detail { +[[noreturn]] FOLLY_NOINLINE inline void throw_coarse_steady_clock_now_exn() { + throw std::runtime_error("Error using CLOCK_MONOTONIC_COARSE."); +} +} // namespace detail + +struct coarse_steady_clock { + using rep = std::chrono::milliseconds::rep; + using period = std::chrono::milliseconds::period; + using duration = std::chrono::duration; + using time_point = std::chrono::time_point; + constexpr static bool is_steady = true; + + static time_point now() { +#ifndef CLOCK_MONOTONIC_COARSE + return time_point(std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch())); +#else + timespec ts; + auto ret = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); + if (ret != 0) { + detail::throw_coarse_steady_clock_now_exn(); + } + return time_point(std::chrono::duration_cast( + std::chrono::seconds(ts.tv_sec) + + std::chrono::nanoseconds(ts.tv_nsec))); +#endif + } +}; +} // namespace chrono +} // namespace folly -- 2.34.1