From 56d54ca2b245f0f102aeabdec4c20326cdf5abe6 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Sat, 30 Dec 2017 14:26:38 -0800 Subject: [PATCH] Shift monotonic_coarse_clock into Chrono.h and rename it to coarse_steady_clock Summary: The rename is to bring it closer in line with the naming conventions in the standard library, and the move is because it doesn't make sense to have clocks defined in stop_watch. Reviewed By: Orvid Differential Revision: D6329551 fbshipit-source-id: 09d9a48eb47b8fd3761a1bd4350d9ca748fe1f96 --- folly/Chrono.h | 33 ++++++++++++++++++++++++++++++++ folly/stop_watch.h | 35 ++++++++-------------------------- folly/test/SharedMutexTest.cpp | 2 +- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/folly/Chrono.h b/folly/Chrono.h index 53cf480f..6a861c5d 100644 --- a/folly/Chrono.h +++ b/folly/Chrono.h @@ -17,8 +17,12 @@ #pragma once #include +#include #include +#include +#include + /*** * include or backport: * * std::chrono::ceil @@ -41,6 +45,35 @@ 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 { diff --git a/folly/stop_watch.h b/folly/stop_watch.h index 5bac22a2..5fbbda9b 100644 --- a/folly/stop_watch.h +++ b/folly/stop_watch.h @@ -16,34 +16,14 @@ #pragma once -#include #include #include #include -namespace folly { - -#ifdef CLOCK_MONOTONIC_COARSE -struct monotonic_coarse_clock { - typedef std::chrono::milliseconds::rep rep; - typedef std::chrono::milliseconds::period period; - typedef std::chrono::milliseconds duration; - typedef std::chrono::time_point time_point; - constexpr static bool is_steady = true; +#include +#include - static time_point now() { - timespec ts; - auto ret = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); - if (ret != 0) { - throw std::runtime_error("Error using CLOCK_MONOTONIC_COARSE."); - } - return time_point( - duration((ts.tv_sec * 1000) + ((ts.tv_nsec / 1000) / 1000))); - } -}; -#else -using monotonic_coarse_clock = std::chrono::steady_clock; -#endif +namespace folly { using monotonic_clock = std::chrono::steady_clock; @@ -299,8 +279,9 @@ struct custom_stop_watch { * * @author: Marcelo Juchem */ -template -using coarse_stop_watch = custom_stop_watch; +template +using coarse_stop_watch = + custom_stop_watch; /** * A type alias for `custom_stop_watch` that uses a monotonic clock as the time @@ -319,6 +300,6 @@ using coarse_stop_watch = custom_stop_watch; * * @author: Marcelo Juchem */ -template -using stop_watch = custom_stop_watch; +template +using stop_watch = custom_stop_watch; } // namespace folly diff --git a/folly/test/SharedMutexTest.cpp b/folly/test/SharedMutexTest.cpp index f27ba1bc..53be67bd 100644 --- a/folly/test/SharedMutexTest.cpp +++ b/folly/test/SharedMutexTest.cpp @@ -33,7 +33,7 @@ using namespace folly; using namespace folly::test; using namespace std; -using namespace chrono; +using namespace std::chrono; typedef DeterministicSchedule DSched; typedef SharedMutexImpl -- 2.34.1