From: Shubhanshu Agrawal Date: Fri, 20 May 2016 05:03:16 +0000 (-0700) Subject: templating folly::fibers::await by Baton X-Git-Tag: 2016.07.26~213 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=10c767929b2c3d7340f7f0db15da7f983a08fe51;p=folly.git templating folly::fibers::await by Baton Summary: This diff templates folly::fibers::await by Baton. This would be helpful in passing in custom BatonType's, which is needed for D3007734 Depends on: D3314891 Reviewed By: andriigrynenko Differential Revision: D3314925 fbshipit-source-id: 9052dc503b9509f16cd41b5e3ede0479a98067aa --- diff --git a/folly/fibers/FiberManager-inl.h b/folly/fibers/FiberManager-inl.h index 0e14525d..003da1bd 100644 --- a/folly/fibers/FiberManager-inl.h +++ b/folly/fibers/FiberManager-inl.h @@ -545,8 +545,9 @@ FiberManager::FiberManager( template typename FirstArgOf::type::value_type inline await(F&& func) { typedef typename FirstArgOf::type::value_type Result; + typedef typename FirstArgOf::type::baton_type BatonT; - return Promise::await(std::forward(func)); + return Promise::await(std::forward(func)); } } } diff --git a/folly/fibers/Promise-inl.h b/folly/fibers/Promise-inl.h index 2adaf781..f4219c51 100644 --- a/folly/fibers/Promise-inl.h +++ b/folly/fibers/Promise-inl.h @@ -18,46 +18,46 @@ namespace folly { namespace fibers { -template -Promise::Promise(folly::Try& value, Baton& baton) +template +Promise::Promise(folly::Try& value, BatonT& baton) : value_(&value), baton_(&baton) {} -template -Promise::Promise(Promise&& other) noexcept +template +Promise::Promise(Promise&& other) noexcept : value_(other.value_), baton_(other.baton_) { other.value_ = nullptr; other.baton_ = nullptr; } -template -Promise& Promise::operator=(Promise&& other) { +template +Promise& Promise::operator=(Promise&& other) { std::swap(value_, other.value_); std::swap(baton_, other.baton_); return *this; } -template -void Promise::throwIfFulfilled() const { +template +void Promise::throwIfFulfilled() const { if (!value_) { throw std::logic_error("promise already fulfilled"); } } -template -Promise::~Promise() { +template +Promise::~Promise() { if (value_) { setException(folly::make_exception_wrapper( "promise not fulfilled")); } } -template -void Promise::setException(folly::exception_wrapper e) { +template +void Promise::setException(folly::exception_wrapper e) { setTry(folly::Try(e)); } -template -void Promise::setTry(folly::Try&& t) { +template +void Promise::setTry(folly::Try&& t) { throwIfFulfilled(); *value_ = std::move(t); @@ -68,37 +68,37 @@ void Promise::setTry(folly::Try&& t) { baton_->post(); } -template +template template -void Promise::setValue(M&& v) { +void Promise::setValue(M&& v) { static_assert(!std::is_same::value, "Use setValue() instead"); setTry(folly::Try(std::forward(v))); } -template -void Promise::setValue() { +template +void Promise::setValue() { static_assert(std::is_same::value, "Use setValue(value) instead"); setTry(folly::Try()); } -template +template template -void Promise::setWith(F&& func) { +void Promise::setWith(F&& func) { setTry(makeTryWith(std::forward(func))); } -template +template template -typename Promise::value_type Promise::await(F&& func) { +typename Promise::value_type Promise::await(F&& func) { folly::Try result; std::exception_ptr funcException; - Baton baton; + BatonT baton; baton.wait([&func, &result, &baton, &funcException]() mutable { try { - func(Promise(result, baton)); + func(Promise(result, baton)); } catch (...) { // Save the exception, but still wait for baton to be posted by user code // or promise destructor. diff --git a/folly/fibers/Promise.h b/folly/fibers/Promise.h index fde863da..6e4a99a8 100644 --- a/folly/fibers/Promise.h +++ b/folly/fibers/Promise.h @@ -23,10 +23,11 @@ namespace fibers { class Baton; -template +template class Promise { public: typedef T value_type; + typedef BatonT baton_type; ~Promise(); @@ -80,9 +81,9 @@ class Promise { static value_type await(F&& func); private: - Promise(folly::Try& value, Baton& baton); + Promise(folly::Try& value, BatonT& baton); folly::Try* value_; - Baton* baton_; + BatonT* baton_; void throwIfFulfilled() const;