Commit d82763a6 authored by Matt Dordal's avatar Matt Dordal Committed by Anton Likhtarov

add support for whenAll to waitWithSemaphore

Summary:
waitWithSemaphore currently doesn't support vector<Try<T>>, unless T is void.
Fix that, and also add a now-required void specialization.

Test Plan:
Add a test that uses vector<Try<bool>>, ensure that the tests compile
(and pass).

Reviewed By: hans@fb.com

Subscribers: folly@lists, fugalh

FB internal diff: D1338528

Tasks: 4389473
parent 3bb51070
...@@ -420,18 +420,28 @@ whenN(InputIterator first, InputIterator last, size_t n) { ...@@ -420,18 +420,28 @@ whenN(InputIterator first, InputIterator last, size_t n) {
} }
template <typename F> template <typename F>
typename std::add_lvalue_reference<typename F::value_type>::type typename F::value_type
waitWithSemaphore(F&& f) { waitWithSemaphore(F&& f) {
LifoSem sem; LifoSem sem;
auto done = f.then([&](Try<typename F::value_type> &&t) { Try<typename F::value_type> done;
f.then([&](Try<typename F::value_type> &&t) {
done = std::move(t);
sem.post(); sem.post();
return t.value();
}); });
sem.wait(); sem.wait();
while (!done.isReady()) {} return std::move(done.value());
return done.value();
} }
inline void waitWithSemaphore(Future<void>&& f) {
LifoSem sem;
Try<void> done;
f.then([&](Try<void> &&t) {
done = std::move(t);
sem.post();
});
sem.wait();
return done.value();
}
}} }}
// I haven't included a Future<T&> specialization because I don't forsee us // I haven't included a Future<T&> specialization because I don't forsee us
......
...@@ -320,7 +320,7 @@ whenN(InputIterator first, InputIterator last, size_t n); ...@@ -320,7 +320,7 @@ whenN(InputIterator first, InputIterator last, size_t n);
* you call this, it will deadlock. * you call this, it will deadlock.
*/ */
template <class F> template <class F>
typename std::add_lvalue_reference<typename F::value_type>::type typename F::value_type
waitWithSemaphore(F&& f); waitWithSemaphore(F&& f);
}} // folly::wangle }} // folly::wangle
......
...@@ -645,6 +645,13 @@ TEST(Future, waitWithSemaphoreImmediate) { ...@@ -645,6 +645,13 @@ TEST(Future, waitWithSemaphoreImmediate) {
v_f.push_back(makeFuture()); v_f.push_back(makeFuture());
auto done_v_f = waitWithSemaphore(whenAll(v_f.begin(), v_f.end())); auto done_v_f = waitWithSemaphore(whenAll(v_f.begin(), v_f.end()));
EXPECT_EQ(2, done_v_f.size()); EXPECT_EQ(2, done_v_f.size());
vector<Future<bool>> v_fb;
v_fb.push_back(makeFuture(true));
v_fb.push_back(makeFuture(false));
auto fut = whenAll(v_fb.begin(), v_fb.end());
auto done_v_fb = waitWithSemaphore(std::move(fut));
EXPECT_EQ(2, done_v_fb.size());
} }
TEST(Future, waitWithSemaphore) { TEST(Future, waitWithSemaphore) {
......
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