Add getVia and getTryVia to SemiFuture.
[folly.git] / folly / futures / Future-inl.h
index 5818244cdc3443818ec8d887815eb1c8aef9c2dd..25f8f464f0d1a2e5af1726cfa5bb5457366b4c39 100644 (file)
 #include <chrono>
 #include <thread>
 
-#include <folly/Baton.h>
 #include <folly/Optional.h>
 #include <folly/executors/InlineExecutor.h>
 #include <folly/futures/Timekeeper.h>
 #include <folly/futures/detail/Core.h>
+#include <folly/synchronization/Baton.h>
 
 #ifndef FOLLY_FUTURE_USING_FIBER
 #if FOLLY_MOBILE || defined(__APPLE__)
@@ -208,12 +208,12 @@ bool FutureBase<T>::isReady() const {
 
 template <class T>
 bool FutureBase<T>::hasValue() {
-  return getTry().hasValue();
+  return core_->getTry().hasValue();
 }
 
 template <class T>
 bool FutureBase<T>::hasException() {
-  return getTry().hasException();
+  return core_->getTry().hasException();
 }
 
 template <class T>
@@ -224,13 +224,6 @@ void FutureBase<T>::detach() {
   }
 }
 
-template <class T>
-Try<T>& FutureBase<T>::getTry() {
-  throwIfInvalid();
-
-  return core_->getTry();
-}
-
 template <class T>
 void FutureBase<T>::throwIfInvalid() const {
   if (!core_) {
@@ -795,11 +788,6 @@ Future<T>::onError(F&& func) {
   return f;
 }
 
-template <class T>
-Try<T>& Future<T>::getTryVia(DrivableExecutor* e) {
-  return waitVia(e).getTry();
-}
-
 template <class Func>
 auto via(Executor* x, Func&& func)
     -> Future<typename isFuture<decltype(std::declval<Func>()())>::Inner> {
@@ -1379,7 +1367,7 @@ void waitImpl(FutureType& f, Duration dur) {
   });
   doBoost(f);
   f = std::move(ret);
-  if (baton->timed_wait(dur)) {
+  if (baton->try_wait_for(dur)) {
     assert(f.isReady());
   }
 }
@@ -1399,6 +1387,21 @@ void waitViaImpl(Future<T>& f, DrivableExecutor* e) {
   assert(f.isReady());
 }
 
+template <class T>
+void waitViaImpl(SemiFuture<T>& f, DrivableExecutor* e) {
+  // Set callback so to ensure that the via executor has something on it
+  // so that once the preceding future triggers this callback, drive will
+  // always have a callback to satisfy it
+  if (f.isReady()) {
+    return;
+  }
+  f = std::move(f).via(e).then([](T&& t) { return std::move(t); });
+  while (!f.isReady()) {
+    e->drive();
+  }
+  assert(f.isReady());
+}
+
 } // namespace detail
 } // namespace futures
 
@@ -1426,6 +1429,18 @@ SemiFuture<T>&& SemiFuture<T>::wait(Duration dur) && {
   return std::move(*this);
 }
 
+template <class T>
+SemiFuture<T>& SemiFuture<T>::waitVia(DrivableExecutor* e) & {
+  futures::detail::waitViaImpl(*this, e);
+  return *this;
+}
+
+template <class T>
+SemiFuture<T>&& SemiFuture<T>::waitVia(DrivableExecutor* e) && {
+  futures::detail::waitViaImpl(*this, e);
+  return std::move(*this);
+}
+
 template <class T>
 T SemiFuture<T>::get() && {
   return std::move(wait().value());
@@ -1441,6 +1456,23 @@ T SemiFuture<T>::get(Duration dur) && {
   }
 }
 
+template <class T>
+Try<T> SemiFuture<T>::getTry() && {
+  wait();
+  return std::move(this->core_->getTry());
+}
+
+template <class T>
+T SemiFuture<T>::getVia(DrivableExecutor* e) && {
+  return std::move(waitVia(e).value());
+}
+
+template <class T>
+Try<T> SemiFuture<T>::getTryVia(DrivableExecutor* e) && {
+  waitVia(e);
+  return std::move(this->core_->getTry());
+}
+
 template <class T>
 Future<T>& Future<T>::wait() & {
   futures::detail::waitImpl(*this);
@@ -1492,11 +1524,23 @@ T Future<T>::get(Duration dur) {
   }
 }
 
+template <class T>
+Try<T>& Future<T>::getTry() {
+  throwIfInvalid();
+
+  return this->core_->getTry();
+}
+
 template <class T>
 T Future<T>::getVia(DrivableExecutor* e) {
   return std::move(waitVia(e).value());
 }
 
+template <class T>
+Try<T>& Future<T>::getTryVia(DrivableExecutor* e) {
+  return waitVia(e).getTry();
+}
+
 namespace futures {
 namespace detail {
 template <class T>