don't try to run the poly tests on gcc-4.9. they will fail.
[folly.git] / folly / futures / Timekeeper.h
index 7bbdddc5763cd0674326558542f9a5ab47dfd915..f5223f43f80a5384c49a4dd8f4a2c3f20efd90a8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 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 <folly/Unit.h>
 #include <folly/futures/detail/Types.h>
 
-namespace folly { namespace wangle {
+namespace folly {
 
-template <class> struct Future;
+template <class> class Future;
 
 /// A Timekeeper handles the details of keeping time and fulfilling delay
-/// promises. The returned Future<void> will either complete after the
+/// promises. The returned Future<Unit> will either complete after the
 /// elapsed time, or in the event of some kind of exceptional error may hold
 /// an exception. These Futures respond to cancellation. If you use a lot of
 /// Delays and many of them ultimately are unneeded (as would be the case for
@@ -34,13 +35,20 @@ template <class> struct Future;
 /// use them implicitly behind the scenes by passing a timeout to some Future
 /// operation.
 ///
-/// Although we don't formally alias Delay = Future<void>,
+/// Although we don't formally alias Delay = Future<Unit>,
 /// that's an appropriate term for it. People will probably also call these
 /// Timeouts, and that's ok I guess, but that term is so overloaded I thought
 /// it made sense to introduce a cleaner term.
 ///
 /// Remember that Duration is a std::chrono duration (millisecond resolution
-/// at the time of writing).
+/// at the time of writing). When writing code that uses specific durations,
+/// prefer using the explicit std::chrono type, e.g. std::chrono::milliseconds
+/// over Duration. This makes the code more legible and means you won't be
+/// unpleasantly surprised if we redefine Duration to microseconds, or
+/// something.
+///
+///    timekeeper.after(std::chrono::duration_cast<Duration>(
+///      someNanoseconds))
 class Timekeeper {
  public:
   virtual ~Timekeeper() = default;
@@ -54,7 +62,7 @@ class Timekeeper {
   /// This future probably completes on the timer thread. You should almost
   /// certainly follow it with a via() call or the accuracy of other timers
   /// will suffer.
-  virtual Future<void> after(Duration) = 0;
+  virtual Future<Unit> after(Duration) = 0;
 
   /// Returns a future that will complete at the requested time.
   ///
@@ -65,26 +73,26 @@ class Timekeeper {
   /// the system clock but rather execute that many milliseconds in the future
   /// according to the steady clock.
   template <class Clock>
-  Future<void> at(std::chrono::time_point<Clock> when);
+  Future<Unit> at(std::chrono::time_point<Clock> when);
 };
 
-}}
+} // namespace folly
 
 // now get those definitions
 #include <folly/futures/Future.h>
 
 // finally we can use Future
-namespace folly { namespace wangle {
-
-  template <class Clock>
-  Future<void> Timekeeper::at(std::chrono::time_point<Clock> when) {
-    auto now = Clock::now();
+namespace folly {
 
-    if (when <= now) {
-      return makeFuture();
-    }
+template <class Clock>
+Future<Unit> Timekeeper::at(std::chrono::time_point<Clock> when) {
+  auto now = Clock::now();
 
-    return after(when - now);
+  if (when <= now) {
+    return makeFuture();
   }
 
-}}
+  return after(std::chrono::duration_cast<Duration>(when - now));
+}
+
+} // namespace folly