Commit 8e00d117 authored by Lee Howes's avatar Lee Howes Committed by Facebook Github Bot

Remove Promise::getFuture from Future-inl.h 1/4 - simple cases

Summary:
Removing calls to folly::Promise::getFuture from Future-inl.h's simpler cases. Replacing with getSemiFuture.

In the case of the collect variants I continue to return a Future, completing inline, until those functions are modified to return SemiFuture.

Reviewed By: yfeldblum

Differential Revision: D7103938

fbshipit-source-id: 3737887a934cd0fe8f7c1511ae2ba252ea047a49
parent 4503a319
...@@ -294,8 +294,10 @@ FutureBase<T>::thenImplementation( ...@@ -294,8 +294,10 @@ FutureBase<T>::thenImplementation(
p.core_->setInterruptHandlerNoLock(this->core_->getInterruptHandler()); p.core_->setInterruptHandlerNoLock(this->core_->getInterruptHandler());
// grab the Future now before we lose our handle on the Promise // grab the Future now before we lose our handle on the Promise
auto f = p.getFuture(); auto sf = p.getSemiFuture();
f.core_->setExecutorNoLock(this->getExecutor()); sf.core_->setExecutorNoLock(this->getExecutor());
auto f = Future<B>(sf.core_);
sf.core_ = nullptr;
/* This is a bit tricky. /* This is a bit tricky.
...@@ -356,8 +358,10 @@ FutureBase<T>::thenImplementation( ...@@ -356,8 +358,10 @@ FutureBase<T>::thenImplementation(
p.core_->setInterruptHandlerNoLock(this->core_->getInterruptHandler()); p.core_->setInterruptHandlerNoLock(this->core_->getInterruptHandler());
// grab the Future now before we lose our handle on the Promise // grab the Future now before we lose our handle on the Promise
auto f = p.getFuture(); auto sf = p.getSemiFuture();
f.core_->setExecutorNoLock(this->getExecutor()); sf.core_->setExecutorNoLock(this->getExecutor());
auto f = Future<B>(sf.core_);
sf.core_ = nullptr;
this->setCallback_( this->setCallback_(
[state = futures::detail::makeCoreCallbackState( [state = futures::detail::makeCoreCallbackState(
...@@ -780,13 +784,18 @@ template <class T> ...@@ -780,13 +784,18 @@ template <class T>
inline Future<T> Future<T>::via(Executor* executor, int8_t priority) & { inline Future<T> Future<T>::via(Executor* executor, int8_t priority) & {
this->throwIfInvalid(); this->throwIfInvalid();
Promise<T> p; Promise<T> p;
auto f = p.getFuture(); auto sf = p.getSemiFuture();
auto func = [p = std::move(p)](Try<T>&& t) mutable { auto func = [p = std::move(p)](Try<T>&& t) mutable {
p.setTry(std::move(t)); p.setTry(std::move(t));
}; };
using R = futures::detail::callableResult<T, decltype(func)>; using R = futures::detail::callableResult<T, decltype(func)>;
this->template thenImplementation<decltype(func), R>( this->template thenImplementation<decltype(func), R>(
std::move(func), typename R::Arg()); std::move(func), typename R::Arg());
// Construct future from semifuture manually because this may not have
// an executor set due to legacy code. This means we can bypass the executor
// check in SemiFuture::via
auto f = Future<T>(sf.core_);
sf.core_ = nullptr;
return std::move(f).via(executor, priority); return std::move(f).via(executor, priority);
} }
...@@ -1047,6 +1056,7 @@ void mapSetCallback(InputIterator first, InputIterator last, F func) { ...@@ -1047,6 +1056,7 @@ void mapSetCallback(InputIterator first, InputIterator last, F func) {
// collectAll (variadic) // collectAll (variadic)
// TODO(T26439406): Make return SemiFuture
template <typename... Fs> template <typename... Fs>
typename futures::detail::CollectAllVariadicContext< typename futures::detail::CollectAllVariadicContext<
typename std::decay<Fs>::type::value_type...>::type typename std::decay<Fs>::type::value_type...>::type
...@@ -1055,11 +1065,12 @@ collectAll(Fs&&... fs) { ...@@ -1055,11 +1065,12 @@ collectAll(Fs&&... fs) {
typename std::decay<Fs>::type::value_type...>>(); typename std::decay<Fs>::type::value_type...>>();
futures::detail::collectVariadicHelper< futures::detail::collectVariadicHelper<
futures::detail::CollectAllVariadicContext>(ctx, std::forward<Fs>(fs)...); futures::detail::CollectAllVariadicContext>(ctx, std::forward<Fs>(fs)...);
return ctx->p.getFuture(); return ctx->p.getSemiFuture().via(&folly::InlineExecutor::instance());
} }
// collectAll (iterator) // collectAll (iterator)
// TODO(T26439406): Make return SemiFuture
template <class InputIterator> template <class InputIterator>
Future< Future<
std::vector< std::vector<
...@@ -1082,7 +1093,7 @@ collectAll(InputIterator first, InputIterator last) { ...@@ -1082,7 +1093,7 @@ collectAll(InputIterator first, InputIterator last) {
mapSetCallback<T>(first, last, [ctx](size_t i, Try<T>&& t) { mapSetCallback<T>(first, last, [ctx](size_t i, Try<T>&& t) {
ctx->results[i] = std::move(t); ctx->results[i] = std::move(t);
}); });
return ctx->p.getFuture(); return ctx->p.getSemiFuture().via(&folly::InlineExecutor::instance());
} }
// collect (iterator) // collect (iterator)
...@@ -1130,6 +1141,7 @@ struct CollectContext { ...@@ -1130,6 +1141,7 @@ struct CollectContext {
} // namespace detail } // namespace detail
} // namespace futures } // namespace futures
// TODO(T26439406): Make return SemiFuture
template <class InputIterator> template <class InputIterator>
Future<typename futures::detail::CollectContext<typename std::iterator_traits< Future<typename futures::detail::CollectContext<typename std::iterator_traits<
InputIterator>::value_type::value_type>::Result> InputIterator>::value_type::value_type>::Result>
...@@ -1148,11 +1160,12 @@ collect(InputIterator first, InputIterator last) { ...@@ -1148,11 +1160,12 @@ collect(InputIterator first, InputIterator last) {
ctx->setPartialResult(i, t); ctx->setPartialResult(i, t);
} }
}); });
return ctx->p.getFuture(); return ctx->p.getSemiFuture().via(&folly::InlineExecutor::instance());
} }
// collect (variadic) // collect (variadic)
// TODO(T26439406): Make return SemiFuture
template <typename... Fs> template <typename... Fs>
typename futures::detail::CollectVariadicContext< typename futures::detail::CollectVariadicContext<
typename std::decay<Fs>::type::value_type...>::type typename std::decay<Fs>::type::value_type...>::type
...@@ -1161,11 +1174,12 @@ collect(Fs&&... fs) { ...@@ -1161,11 +1174,12 @@ collect(Fs&&... fs) {
typename std::decay<Fs>::type::value_type...>>(); typename std::decay<Fs>::type::value_type...>>();
futures::detail::collectVariadicHelper< futures::detail::collectVariadicHelper<
futures::detail::CollectVariadicContext>(ctx, std::forward<Fs>(fs)...); futures::detail::CollectVariadicContext>(ctx, std::forward<Fs>(fs)...);
return ctx->p.getFuture(); return ctx->p.getSemiFuture().via(&folly::InlineExecutor::instance());
} }
// collectAny (iterator) // collectAny (iterator)
// TODO(T26439406): Make return SemiFuture
template <class InputIterator> template <class InputIterator>
Future< Future<
std::pair<size_t, std::pair<size_t,
...@@ -1188,11 +1202,12 @@ collectAny(InputIterator first, InputIterator last) { ...@@ -1188,11 +1202,12 @@ collectAny(InputIterator first, InputIterator last) {
ctx->p.setValue(std::make_pair(i, std::move(t))); ctx->p.setValue(std::make_pair(i, std::move(t)));
} }
}); });
return ctx->p.getFuture(); return ctx->p.getSemiFuture().via(&folly::InlineExecutor::instance());
} }
// collectAnyWithoutException (iterator) // collectAnyWithoutException (iterator)
// TODO(T26439406): Make return SemiFuture
template <class InputIterator> template <class InputIterator>
Future<std::pair< Future<std::pair<
size_t, size_t,
...@@ -1219,11 +1234,12 @@ collectAnyWithoutException(InputIterator first, InputIterator last) { ...@@ -1219,11 +1234,12 @@ collectAnyWithoutException(InputIterator first, InputIterator last) {
ctx->p.setException(t.exception()); ctx->p.setException(t.exception());
} }
}); });
return ctx->p.getFuture(); return ctx->p.getSemiFuture().via(&folly::InlineExecutor::instance());
} }
// collectN (iterator) // collectN (iterator)
// TODO(T26439406): Make return SemiFuture
template <class InputIterator> template <class InputIterator>
Future<std::vector<std::pair<size_t, Try<typename Future<std::vector<std::pair<size_t, Try<typename
std::iterator_traits<InputIterator>::value_type::value_type>>>> std::iterator_traits<InputIterator>::value_type::value_type>>>>
...@@ -1257,7 +1273,7 @@ collectN(InputIterator first, InputIterator last, size_t n) { ...@@ -1257,7 +1273,7 @@ collectN(InputIterator first, InputIterator last, size_t n) {
}); });
} }
return ctx->p.getFuture(); return ctx->p.getSemiFuture().via(&folly::InlineExecutor::instance());
} }
// reduce (iterator) // reduce (iterator)
...@@ -1375,6 +1391,7 @@ Future<I> Future<T>::reduce(I&& initial, F&& func) { ...@@ -1375,6 +1391,7 @@ Future<I> Future<T>::reduce(I&& initial, F&& func) {
// unorderedReduce (iterator) // unorderedReduce (iterator)
// TODO(T26439406): Make return SemiFuture
template <class It, class T, class F, class ItT, class Arg> template <class It, class T, class F, class ItT, class Arg>
Future<T> unorderedReduce(It first, It last, T initial, F func) { Future<T> unorderedReduce(It first, It last, T initial, F func) {
if (first == last) { if (first == last) {
...@@ -1423,7 +1440,7 @@ Future<T> unorderedReduce(It first, It last, T initial, F func) { ...@@ -1423,7 +1440,7 @@ Future<T> unorderedReduce(It first, It last, T initial, F func) {
} }
}); });
return ctx->promise_.getFuture(); return ctx->promise_.getSemiFuture().via(&folly::InlineExecutor::instance());
} }
// within // within
...@@ -1494,7 +1511,9 @@ Future<T> Future<T>::within(Duration dur, E e, Timekeeper* tk) { ...@@ -1494,7 +1511,9 @@ Future<T> Future<T>::within(Duration dur, E e, Timekeeper* tk) {
} }
}); });
return ctx->promise.getFuture().via(this->getExecutor()); auto* currentExecutor = this->getExecutor();
return ctx->promise.getSemiFuture().via(
currentExecutor ? currentExecutor : &folly::InlineExecutor::instance());
} }
// delayed // delayed
......
...@@ -327,6 +327,8 @@ class SemiFuture : private futures::detail::FutureBase<T> { ...@@ -327,6 +327,8 @@ class SemiFuture : private futures::detail::FutureBase<T> {
friend class futures::detail::FutureBase; friend class futures::detail::FutureBase;
template <class> template <class>
friend class SemiFuture; friend class SemiFuture;
template <class>
friend class Future;
using typename Base::corePtr; using typename Base::corePtr;
using Base::setExecutor; using Base::setExecutor;
......
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