fix waitWithSemaphore return type
authorMatt Dordal <mnd@fb.com>
Mon, 19 May 2014 20:03:28 +0000 (13:03 -0700)
committerDave Watson <davejwatson@fb.com>
Tue, 20 May 2014 19:59:05 +0000 (12:59 -0700)
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

folly/wangle/Future-inl.h
folly/wangle/Future.h
folly/wangle/test/FutureTest.cpp

index 10d844c2e514e014a5b8681e6ff14bf643d0111f..940d869685a61ecc3c120c4c5e67449aa19cdc99 100644 (file)
@@ -420,14 +420,15 @@ whenN(InputIterator first, InputIterator last, size_t n) {
 }
 
 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;
-  Try<typename F::value_type> done;
-  f.then([&](Try<typename F::value_type> &&t) {
-    done = std::move(t);
+  auto done = f.then([&](Try<typename F::value_type> &&t) {
     sem.post();
+    return t.value();
   });
   sem.wait();
+  while (!done.isReady()) {}
   return done.value();
 }
 
index a0030f84b3e669556be55c0eafac126dcb86a287..20ecce1699b2da5e5911a6f43537a543764e9f10 100644 (file)
@@ -320,7 +320,8 @@ whenN(InputIterator first, InputIterator last, size_t n);
  * you call this, it will deadlock.
  */
 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
 
index d925644589ea254c2b15a5dd45bbb1331ccffea7..0202470cef045b35a7d23150ba30ed6fa5b504c9 100644 (file)
@@ -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<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) {