From d82763a62158b77e6504535907537e1a66545264 Mon Sep 17 00:00:00 2001 From: Matt Dordal Date: Fri, 23 May 2014 09:19:20 -0700 Subject: [PATCH] add support for whenAll to waitWithSemaphore Summary: waitWithSemaphore currently doesn't support vector>, unless T is void. Fix that, and also add a now-required void specialization. Test Plan: Add a test that uses vector>, ensure that the tests compile (and pass). Reviewed By: hans@fb.com Subscribers: folly@lists, fugalh FB internal diff: D1338528 Tasks: 4389473 --- folly/wangle/Future-inl.h | 20 +++++++++++++++----- folly/wangle/Future.h | 2 +- folly/wangle/test/FutureTest.cpp | 7 +++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/folly/wangle/Future-inl.h b/folly/wangle/Future-inl.h index 940d8696..df5831db 100644 --- a/folly/wangle/Future-inl.h +++ b/folly/wangle/Future-inl.h @@ -420,18 +420,28 @@ whenN(InputIterator first, InputIterator last, size_t n) { } template -typename std::add_lvalue_reference::type +typename F::value_type waitWithSemaphore(F&& f) { LifoSem sem; - auto done = f.then([&](Try &&t) { + Try done; + f.then([&](Try &&t) { + done = std::move(t); sem.post(); - return t.value(); }); sem.wait(); - while (!done.isReady()) {} - return done.value(); + return std::move(done.value()); } +inline void waitWithSemaphore(Future&& f) { + LifoSem sem; + Try done; + f.then([&](Try &&t) { + done = std::move(t); + sem.post(); + }); + sem.wait(); + return done.value(); +} }} // I haven't included a Future specialization because I don't forsee us diff --git a/folly/wangle/Future.h b/folly/wangle/Future.h index 20ecce16..e7d0f415 100644 --- a/folly/wangle/Future.h +++ b/folly/wangle/Future.h @@ -320,7 +320,7 @@ whenN(InputIterator first, InputIterator last, size_t n); * you call this, it will deadlock. */ template -typename std::add_lvalue_reference::type +typename F::value_type waitWithSemaphore(F&& f); }} // folly::wangle diff --git a/folly/wangle/test/FutureTest.cpp b/folly/wangle/test/FutureTest.cpp index a7a2fa1c..84977c74 100644 --- a/folly/wangle/test/FutureTest.cpp +++ b/folly/wangle/test/FutureTest.cpp @@ -645,6 +645,13 @@ TEST(Future, waitWithSemaphoreImmediate) { v_f.push_back(makeFuture()); auto done_v_f = waitWithSemaphore(whenAll(v_f.begin(), v_f.end())); EXPECT_EQ(2, done_v_f.size()); + + vector> 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) { -- 2.34.1