adding Promise::await
authorShubhanshu Agrawal <shubhanshu@fb.com>
Thu, 19 May 2016 03:42:24 +0000 (20:42 -0700)
committerFacebook Github Bot 9 <facebook-github-bot-9-bot@fb.com>
Thu, 19 May 2016 03:53:23 +0000 (20:53 -0700)
Summary:
This diff adds a static await method to Promise.
Also after this, folly::fibers::await is just a wrapper around Promise::await.
This also removes the friend relationship between Promise and await.

This would be helpful in making the folly::fibers::await templated by Baton changes easier to make.

Reviewed By: andriigrynenko

Differential Revision: D3314891

fbshipit-source-id: 361546c078caafd067734d2f474c617d7fb888b0

folly/experimental/fibers/FiberManager-inl.h
folly/experimental/fibers/Promise-inl.h
folly/experimental/fibers/Promise.h

index 5d0d5a90984dc4bb4eb6d831dcdfa047bd587037..a4783c5c645cbd36d6317c960bf6af4b70b43c94 100644 (file)
@@ -546,25 +546,7 @@ template <typename F>
 typename FirstArgOf<F>::type::value_type inline await(F&& func) {
   typedef typename FirstArgOf<F>::type::value_type Result;
 
-  folly::Try<Result> result;
-  std::exception_ptr funcException;
-
-  Baton baton;
-  baton.wait([&func, &result, &baton, &funcException]() mutable {
-    try {
-      func(Promise<Result>(result, baton));
-    } catch (...) {
-      // Save the exception, but still wait for baton to be posted by user code
-      // or promise destructor.
-      funcException = std::current_exception();
-    }
-  });
-
-  if (UNLIKELY(funcException != nullptr)) {
-    std::rethrow_exception(funcException);
-  }
-
-  return folly::moveFromTry(result);
+  return Promise<Result>::await(std::forward<F>(func));
 }
 }
 }
index 67459855a2f70eccdbf10edbb3543232963a7336..a50d81c7de67f68c70741a6e006c5e375c894151 100644 (file)
@@ -88,5 +88,29 @@ template <class F>
 void Promise<T>::setWith(F&& func) {
   setTry(makeTryWith(std::forward<F>(func)));
 }
+
+template <class T>
+template <class F>
+typename Promise<T>::value_type Promise<T>::await(F&& func) {
+  folly::Try<value_type> result;
+  std::exception_ptr funcException;
+
+  Baton baton;
+  baton.wait([&func, &result, &baton, &funcException]() mutable {
+    try {
+      func(Promise<value_type>(result, baton));
+    } catch (...) {
+      // Save the exception, but still wait for baton to be posted by user code
+      // or promise destructor.
+      funcException = std::current_exception();
+    }
+  });
+
+  if (UNLIKELY(funcException != nullptr)) {
+    std::rethrow_exception(funcException);
+  }
+
+  return folly::moveFromTry(result);
+}
 }
 }
index 78388326ffe8cd04fd15783c1728a1a88668fe02..4dee6099c22e2176b969a6763e42c0dc8759466f 100644 (file)
@@ -23,9 +23,6 @@ namespace fibers {
 
 class Baton;
 
-template <typename F>
-typename FirstArgOf<F>::type::value_type inline await(F&& func);
-
 template <typename T>
 class Promise {
  public:
@@ -72,10 +69,17 @@ class Promise {
     */
   void setException(folly::exception_wrapper);
 
- private:
-  template <typename F>
-  friend typename FirstArgOf<F>::type::value_type await(F&&);
+  /**
+   * Blocks task execution until given promise is fulfilled.
+   *
+   * Calls function passing in a Promise<T>, which has to be fulfilled.
+   *
+   * @return data which was used to fulfill the promise.
+   */
+  template <class F>
+  static value_type await(F&& func);
 
+ private:
   Promise(folly::Try<T>& value, Baton& baton);
   folly::Try<T>* value_;
   Baton* baton_;