Adds writer test case for RCU
[folly.git] / folly / Chrono.h
index 852a74b7f6d96ae877446056cdeb44dc79b9f814..2345e9e85fa2f78a3e93a47653715ab1d1fde376 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2017-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #pragma once
 
 #include <chrono>
+#include <stdexcept>
 #include <type_traits>
 
+#include <folly/Portability.h>
+#include <folly/portability/Time.h>
+
 /***
  *  include or backport:
  *  * std::chrono::ceil
@@ -74,7 +78,7 @@ template <typename To, typename Duration>
 constexpr To round_impl(Duration const& d, To const& t0) {
   return round_impl(d, t0, t0 + To{1});
 }
-}
+} // namespace detail
 
 //  mimic: std::chrono::ceil, C++17
 //  from: http://en.cppreference.com/w/cpp/chrono/duration/ceil, CC-BY-SA
@@ -148,7 +152,41 @@ constexpr std::chrono::time_point<Clock, To> round(
     std::chrono::time_point<Clock, Duration> const& tp) {
   return std::chrono::time_point<Clock, To>{round<To>(tp.time_since_epoch())};
 }
-}
-}
+} // namespace chrono
+} // 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<rep, period>;
+  using time_point = std::chrono::time_point<coarse_steady_clock, duration>;
+  constexpr static bool is_steady = true;
+
+  static time_point now() {
+#ifndef CLOCK_MONOTONIC_COARSE
+    return time_point(std::chrono::duration_cast<duration>(
+        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<duration>(
+        std::chrono::seconds(ts.tv_sec) +
+        std::chrono::nanoseconds(ts.tv_nsec)));
 #endif
+  }
+};
+} // namespace chrono
+} // namespace folly