Commit 6afcbf48 authored by Lee Howes's avatar Lee Howes Committed by Facebook Github Bot

Remove Promise::getFuture from Future-inl.h 2/4 - waitImpl

Summary: Replace calls to Promise::getFuture in waitImpl with getSemiFuture. There is a slight behavioural change here in that we now set the executor on the returned future, but this appears to be the correct behaviour.

Reviewed By: yfeldblum

Differential Revision: D7104627

fbshipit-source-id: fd19c3f6884b560f7d5ea1276f3c80c082dfc53d
parent a440441d
...@@ -1543,6 +1543,19 @@ void waitImpl(FutureType& f) { ...@@ -1543,6 +1543,19 @@ void waitImpl(FutureType& f) {
assert(f.isReady()); assert(f.isReady());
} }
template <class T>
void convertFuture(SemiFuture<T>&& sf, Future<T>& f) {
// Carry executor from f, inserting an inline executor if it did not have one
auto* currentExecutor = f.getExecutor();
f = std::move(sf).via(
currentExecutor ? currentExecutor : &folly::InlineExecutor::instance());
}
template <class T>
void convertFuture(SemiFuture<T>&& sf, SemiFuture<T>& f) {
f = std::move(sf);
}
template <class FutureType, typename T = typename FutureType::value_type> template <class FutureType, typename T = typename FutureType::value_type>
void waitImpl(FutureType& f, Duration dur) { void waitImpl(FutureType& f, Duration dur) {
// short-circuit if there's nothing to do // short-circuit if there's nothing to do
...@@ -1551,13 +1564,13 @@ void waitImpl(FutureType& f, Duration dur) { ...@@ -1551,13 +1564,13 @@ void waitImpl(FutureType& f, Duration dur) {
} }
Promise<T> promise; Promise<T> promise;
auto ret = promise.getFuture(); auto ret = promise.getSemiFuture();
auto baton = std::make_shared<FutureBatonType>(); auto baton = std::make_shared<FutureBatonType>();
f.setCallback_([baton, promise = std::move(promise)](Try<T>&& t) mutable { f.setCallback_([baton, promise = std::move(promise)](Try<T>&& t) mutable {
promise.setTry(std::move(t)); promise.setTry(std::move(t));
baton->post(); baton->post();
}); });
f = std::move(ret); convertFuture(std::move(ret), f);
if (baton->try_wait_for(dur)) { if (baton->try_wait_for(dur)) {
assert(f.isReady()); assert(f.isReady());
} }
......
...@@ -193,6 +193,8 @@ class FutureBase { ...@@ -193,6 +193,8 @@ class FutureBase {
typename std::enable_if<R::ReturnsFuture::value, typename R::Return>::type typename std::enable_if<R::ReturnsFuture::value, typename R::Return>::type
thenImplementation(F&& func, futures::detail::argResult<isTry, F, Args...>); thenImplementation(F&& func, futures::detail::argResult<isTry, F, Args...>);
}; };
template <class T>
void convertFuture(SemiFuture<T>&& sf, Future<T>& f);
} // namespace detail } // namespace detail
} // namespace futures } // namespace futures
...@@ -814,6 +816,11 @@ class Future : private futures::detail::FutureBase<T> { ...@@ -814,6 +816,11 @@ class Future : private futures::detail::FutureBase<T> {
/// predicate behaves like std::function<bool(void)> /// predicate behaves like std::function<bool(void)>
template <class P, class F> template <class P, class F>
friend Future<Unit> whileDo(P&& predicate, F&& thunk); friend Future<Unit> whileDo(P&& predicate, F&& thunk);
template <class FT>
friend void futures::detail::convertFuture(
SemiFuture<FT>&& sf,
Future<FT>& f);
}; };
} // namespace folly } // namespace folly
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment