add waitWithSemaphore to folly::wangle
[folly.git] / folly / wangle / Future.h
index 3b13b194bdfef95178c53ac3a4ce45dc4d6adbbf..a0030f84b3e669556be55c0eafac126dcb86a287 100644 (file)
@@ -21,6 +21,7 @@
 #include <functional>
 #include <memory>
 #include <type_traits>
+#include <vector>
 
 #include "folly/MoveWrapper.h"
 #include "Promise.h"
@@ -58,8 +59,23 @@ class Future {
   typename std::add_lvalue_reference<const T>::type
   value() const;
 
+  /// Returns a future which will call back on the other side of executor.
+  ///
+  ///   f.via(e).then(a); // safe
+  ///
+  ///   f.via(e).then(a).then(b); // faux pas
+  ///
+  /// a will definitely execute in the intended thread, but b may execute
+  /// either in that thread, or in the current thread. If you need to
+  /// guarantee where b executes, use a Later.
   template <typename Executor>
-  Future<T> executeWithSameThread(Executor* executor);
+  Future<T> via(Executor* executor);
+
+  /// Deprecated alias for via
+  template <typename Executor>
+  Future<T> executeWithSameThread(Executor* executor) {
+    return via(executor);
+  }
 
   /**
      Thread-safe version of executeWith
@@ -71,6 +87,8 @@ class Future {
      Instead, you may pass in a Promise so that we can set up
      the rest of the chain in advance, without any racey
      modifications of the continuation
+
+     Deprecated. Use a Later.
    */
   template <typename Executor>
   void executeWith(Executor* executor, Promise<T>&& cont_promise);
@@ -240,6 +258,10 @@ template <class T, class E>
 typename std::enable_if<std::is_base_of<std::exception, E>::value, Future<T>>::type
 makeFuture(E const& e);
 
+/** Make a Future out of a Try */
+template <class T>
+Future<T> makeFuture(Try<T>&& t);
+
 /** When all the input Futures complete, the returned Future will complete.
   Errors do not cause early termination; this Future will always succeed
   after all its Futures have finished (whether successfully or with an
@@ -291,6 +313,15 @@ Future<std::vector<std::pair<
   Try<typename std::iterator_traits<InputIterator>::value_type::value_type>>>>
 whenN(InputIterator first, InputIterator last, size_t n);
 
+/** Wait for the given future to complete on a semaphore. Returns the result of
+ * the given future.
+ *
+ * NB if the promise for the future would be fulfilled in the same thread that
+ * you call this, it will deadlock.
+ */
+template <class F>
+typename F::value_type waitWithSemaphore(F&& f);
+
 }} // folly::wangle
 
 #include "Future-inl.h"