return f;
}
-template <class T>
-template <typename Executor>
-inline void Future<T>::executeWith(
- Executor* executor, Promise<T>&& cont_promise) {
- throwIfInvalid();
-
- folly::MoveWrapper<Promise<T>> p(std::move(cont_promise));
-
- setContinuation([executor, p](Try<T>&& t) mutable {
- folly::MoveWrapper<Try<T>> tt(std::move(t));
- executor->add([p, tt]() mutable {
- p->fulfilTry(std::move(*tt));
- });
- });
-}
-
template <class T>
bool Future<T>::isReady() const {
throwIfInvalid();
template <typename Executor>
Future<T> via(Executor* executor);
- /// Deprecated alias for via
- template <typename Executor>
- Future<T> executeWithSameThread(Executor* executor) {
- return via(executor);
- }
-
- /**
- Thread-safe version of executeWith
-
- Since an executor would likely start executing the Future chain
- right away, it would be a race condition to call:
- Future.executeWith(...).then(...), as there would be race
- condition between the then and the running Future.
- Instead, you may pass in a Promise so that we can set up
- the rest of the chain in advance, without any racey
- modifications of the continuation
-
- Deprecated. Use a Later.
- */
- template <typename Executor>
- void executeWith(Executor* executor, Promise<T>&& cont_promise);
-
/** True when the result (or exception) is ready. */
bool isReady() const;
*/
/* TODO n3428 and other async frameworks have something like then(scheduler,
Future), we probably want to support a similar API (instead of
- executeWith). */
+ via. or rather, via should return a cold future (Later) and we provide
+ then(scheduler, Future) ). */
template <class F>
typename std::enable_if<
!isFuture<typename std::result_of<F(Try<T>&&)>::type>::value,
/// Exceptions still propagate.
Future<void> then();
- /// Use of this method is advanced wizardry.
- /// XXX should this be protected?
+ /// This is not the method you're looking for.
+ ///
+ /// This needs to be public because it's used by make* and when*, and it's
+ /// not worth listing all those and their fancy template signatures as
+ /// friends. But it's not for public consumption.
template <class F>
void setContinuation(F&& func);
template <class T>
Later<T> Later<T>::via(Executor* executor) {
- Promise<T> promise;
+ folly::MoveWrapper<Promise<T>> promise;
Later<T> later(std::move(starter_));
- later.future_ = promise.getFuture();
- future_->executeWith(executor, std::move(promise));
+ later.future_ = promise->getFuture();
+
+ future_->setContinuation([executor, promise](Try<T>&& t) mutable {
+ folly::MoveWrapper<Try<T>> tt(std::move(t));
+ executor->add([promise, tt]() mutable {
+ promise->fulfilTry(std::move(*tt));
+ });
+ });
+
return later;
}