From: Matt Dordal Date: Mon, 19 May 2014 20:03:28 +0000 (-0700) Subject: fix waitWithSemaphore return type X-Git-Tag: v0.22.0~534 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=f327d89ea4e18f1fcc7b8a54af3d012df68c3ef7 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 --- diff --git a/folly/wangle/Future-inl.h b/folly/wangle/Future-inl.h index 10d844c2..940d8696 100644 --- a/folly/wangle/Future-inl.h +++ b/folly/wangle/Future-inl.h @@ -420,14 +420,15 @@ whenN(InputIterator first, InputIterator last, size_t n) { } template -typename F::value_type waitWithSemaphore(F&& f) { +typename std::add_lvalue_reference::type +waitWithSemaphore(F&& f) { LifoSem sem; - Try done; - f.then([&](Try &&t) { - done = std::move(t); + auto done = f.then([&](Try &&t) { sem.post(); + return t.value(); }); sem.wait(); + while (!done.isReady()) {} return done.value(); } diff --git a/folly/wangle/Future.h b/folly/wangle/Future.h index a0030f84..20ecce16 100644 --- a/folly/wangle/Future.h +++ b/folly/wangle/Future.h @@ -320,7 +320,8 @@ whenN(InputIterator first, InputIterator last, size_t n); * you call this, it will deadlock. */ template -typename F::value_type waitWithSemaphore(F&& f); +typename std::add_lvalue_reference::type +waitWithSemaphore(F&& f); }} // folly::wangle diff --git a/folly/wangle/test/FutureTest.cpp b/folly/wangle/test/FutureTest.cpp index d9256445..0202470c 100644 --- a/folly/wangle/test/FutureTest.cpp +++ b/folly/wangle/test/FutureTest.cpp @@ -627,7 +627,18 @@ TEST(Future, throwIfFailed) { TEST(Future, waitWithSemaphoreImmediate) { waitWithSemaphore(makeFuture()); auto done = waitWithSemaphore(makeFuture(42)); - EXPECT_EQ(done, 42); + EXPECT_EQ(42, done); + + vector v{1,2,3}; + auto done_v = waitWithSemaphore(makeFuture(v)); + EXPECT_EQ(v.size(), done_v.size()); + EXPECT_EQ(v, done_v); + + vector> 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) {