Commit f327d89e authored by Matt Dordal's avatar Matt Dordal Committed by Dave Watson

fix waitWithSemaphore return type

Summary:
waitWithSemaphore always tried to return a value, which is not what the
underlying implementation did. If the value_type was an object, it would
fail to compile.

Test Plan: unit tests (added one to compile all the variants)

Reviewed By: hans@fb.com

Subscribers: folly@lists, fugalh

FB internal diff: D1326916
parent ae442842
...@@ -420,14 +420,15 @@ whenN(InputIterator first, InputIterator last, size_t n) { ...@@ -420,14 +420,15 @@ whenN(InputIterator first, InputIterator last, size_t n) {
} }
template <typename F> template <typename F>
typename F::value_type waitWithSemaphore(F&& f) { typename std::add_lvalue_reference<typename F::value_type>::type
waitWithSemaphore(F&& f) {
LifoSem sem; LifoSem sem;
Try<typename F::value_type> done; auto done = f.then([&](Try<typename F::value_type> &&t) {
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 done.value(); return done.value();
} }
......
...@@ -320,7 +320,8 @@ whenN(InputIterator first, InputIterator last, size_t n); ...@@ -320,7 +320,8 @@ 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 F::value_type waitWithSemaphore(F&& f); typename std::add_lvalue_reference<typename F::value_type>::type
waitWithSemaphore(F&& f);
}} // folly::wangle }} // folly::wangle
......
...@@ -627,7 +627,18 @@ TEST(Future, throwIfFailed) { ...@@ -627,7 +627,18 @@ TEST(Future, throwIfFailed) {
TEST(Future, waitWithSemaphoreImmediate) { TEST(Future, waitWithSemaphoreImmediate) {
waitWithSemaphore(makeFuture()); waitWithSemaphore(makeFuture());
auto done = waitWithSemaphore(makeFuture(42)); auto done = waitWithSemaphore(makeFuture(42));
EXPECT_EQ(done, 42); EXPECT_EQ(42, done);
vector<int> v{1,2,3};
auto done_v = waitWithSemaphore(makeFuture(v));
EXPECT_EQ(v.size(), done_v.size());
EXPECT_EQ(v, done_v);
vector<Future<void>> v_f;
v_f.push_back(makeFuture());
v_f.push_back(makeFuture());
auto done_v_f = waitWithSemaphore(whenAll(v_f.begin(), v_f.end()));
EXPECT_EQ(2, done_v_f.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