Commit a3949788 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

Implement collectAnyWithoutExceptionSemiFuture

Summary: Adding support for SemiFutures with deferred work.

Reviewed By: yfeldblum

Differential Revision: D12812057

fbshipit-source-id: 7da73124d27063eb730be1abe134ae9838494af0
parent 0bf289ce
......@@ -1670,6 +1670,10 @@ collectAnyWithoutException(InputIterator first, InputIterator last) {
size_t nTotal;
};
std::vector<folly::Executor::KeepAlive<futures::detail::DeferredExecutor>>
executors;
futures::detail::stealDeferredExecutors(executors, first, last);
auto ctx = std::make_shared<Context>(size_t(std::distance(first, last)));
for (size_t i = 0; first != last; ++first, ++i) {
first->setCallback_([i, ctx](Try<T>&& t) {
......@@ -1683,7 +1687,17 @@ collectAnyWithoutException(InputIterator first, InputIterator last) {
}
});
}
return ctx->p.getSemiFuture();
auto future = ctx->p.getSemiFuture();
if (!executors.empty()) {
future = std::move(future).defer(
[](Try<typename decltype(future)::value_type>&& t) {
return std::move(t).value();
});
auto deferredExecutor = futures::detail::getDeferredExecutor(future);
deferredExecutor->setNestedExecutors(std::move(executors));
}
return future;
}
// collectN (iterator)
......
......@@ -410,6 +410,28 @@ TEST(Collect, collectAnyWithoutException) {
EXPECT_TRUE(onef.isReady());
EXPECT_TRUE(onef.hasException());
}
// Deferred work
{
std::vector<Promise<int>> promises(10);
auto onef = [&] {
std::vector<SemiFuture<int>> futures;
for (auto& p : promises) {
futures.push_back(
p.getSemiFuture().deferValue([](auto v) { return v; }));
}
return collectAnyWithoutException(futures);
}();
/* futures were moved in, so these are invalid now */
promises[7].setValue(42);
auto idx_fut = std::move(onef).get();
EXPECT_EQ(7, idx_fut.first);
EXPECT_EQ(42, idx_fut.second);
}
}
TEST(Collect, alreadyCompleted) {
......
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