From: Shubhanshu Agrawal Date: Thu, 19 May 2016 03:42:24 +0000 (-0700) Subject: adding Promise::await X-Git-Tag: 2016.07.26~218 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=6698301ad7165dd89f36a5c3f5432104539be08d;p=folly.git adding Promise::await 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 --- diff --git a/folly/experimental/fibers/FiberManager-inl.h b/folly/experimental/fibers/FiberManager-inl.h index 5d0d5a90..a4783c5c 100644 --- a/folly/experimental/fibers/FiberManager-inl.h +++ b/folly/experimental/fibers/FiberManager-inl.h @@ -546,25 +546,7 @@ template typename FirstArgOf::type::value_type inline await(F&& func) { typedef typename FirstArgOf::type::value_type Result; - folly::Try result; - std::exception_ptr funcException; - - Baton baton; - baton.wait([&func, &result, &baton, &funcException]() mutable { - try { - func(Promise(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::await(std::forward(func)); } } } diff --git a/folly/experimental/fibers/Promise-inl.h b/folly/experimental/fibers/Promise-inl.h index 67459855..a50d81c7 100644 --- a/folly/experimental/fibers/Promise-inl.h +++ b/folly/experimental/fibers/Promise-inl.h @@ -88,5 +88,29 @@ template void Promise::setWith(F&& func) { setTry(makeTryWith(std::forward(func))); } + +template +template +typename Promise::value_type Promise::await(F&& func) { + folly::Try result; + std::exception_ptr funcException; + + Baton baton; + baton.wait([&func, &result, &baton, &funcException]() mutable { + try { + func(Promise(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); +} } } diff --git a/folly/experimental/fibers/Promise.h b/folly/experimental/fibers/Promise.h index 78388326..4dee6099 100644 --- a/folly/experimental/fibers/Promise.h +++ b/folly/experimental/fibers/Promise.h @@ -23,9 +23,6 @@ namespace fibers { class Baton; -template -typename FirstArgOf::type::value_type inline await(F&& func); - template class Promise { public: @@ -72,10 +69,17 @@ class Promise { */ void setException(folly::exception_wrapper); - private: - template - friend typename FirstArgOf::type::value_type await(F&&); + /** + * Blocks task execution until given promise is fulfilled. + * + * Calls function passing in a Promise, which has to be fulfilled. + * + * @return data which was used to fulfill the promise. + */ + template + static value_type await(F&& func); + private: Promise(folly::Try& value, Baton& baton); folly::Try* value_; Baton* baton_;