From: Martin Martin Date: Fri, 29 Apr 2016 18:54:34 +0000 (-0700) Subject: Make addTask[Remote]Future() work for functions returning void. X-Git-Tag: 2016.07.26~299 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=b31eb722e444ab0293a73fe9de3f94e657ca6de9;p=folly.git Make addTask[Remote]Future() work for functions returning void. Summary: There's no Future, you're supposed to use Future instead. Unit has a "Lift" template to do the conversion. Reviewed By: andriigrynenko Differential Revision: D3241498 fb-gh-sync-id: db12d4f343685bc613b701e023c860c2c903ed4c fbshipit-source-id: db12d4f343685bc613b701e023c860c2c903ed4c --- diff --git a/folly/experimental/fibers/FiberManager-inl.h b/folly/experimental/fibers/FiberManager-inl.h index 6bad3618..8f31ede1 100644 --- a/folly/experimental/fibers/FiberManager-inl.h +++ b/folly/experimental/fibers/FiberManager-inl.h @@ -281,9 +281,10 @@ void FiberManager::addTask(F&& func) { } template -auto FiberManager::addTaskFuture(F&& func) - -> folly::Future::type> { - using T = typename std::result_of::type; +auto FiberManager::addTaskFuture(F&& func) -> folly::Future< + typename folly::Unit::Lift::type>::type> { + using T = + typename folly::Unit::Lift::type>::type; folly::Promise p; auto f = p.getFuture(); addTaskFinally( @@ -312,9 +313,11 @@ void FiberManager::addTaskRemote(F&& func) { } template -auto FiberManager::addTaskRemoteFuture(F&& func) - -> folly::Future::type> { - folly::Promise::type> p; +auto FiberManager::addTaskRemoteFuture(F&& func) -> folly::Future< + typename folly::Unit::Lift::type>::type> { + folly::Promise< + typename folly::Unit::Lift::type>::type> + p; auto f = p.getFuture(); addTaskRemote( [ p = std::move(p), func = std::forward(func), this ]() mutable { @@ -395,7 +398,8 @@ struct FiberManager::AddTaskFinallyHelper { template void FiberManager::addTaskFinally(F&& func, G&& finally) { - typedef typename std::result_of::type Result; + typedef typename folly::Unit::Lift::type>::type + Result; static_assert( IsRvalueRefTry::type>::value, diff --git a/folly/experimental/fibers/FiberManager.h b/folly/experimental/fibers/FiberManager.h index 73a243ce..559f70e7 100644 --- a/folly/experimental/fibers/FiberManager.h +++ b/folly/experimental/fibers/FiberManager.h @@ -190,8 +190,8 @@ class FiberManager : public ::folly::Executor { * The object will be destroyed once task execution is complete. */ template - auto addTaskFuture(F&& func) - -> folly::Future::type>; + auto addTaskFuture(F&& func) -> folly::Future< + typename folly::Unit::Lift::type>::type>; /** * Add a new task to be executed. Safe to call from other threads. * @@ -209,8 +209,8 @@ class FiberManager : public ::folly::Executor { * The object will be destroyed once task execution is complete. */ template - auto addTaskRemoteFuture(F&& func) - -> folly::Future::type>; + auto addTaskRemoteFuture(F&& func) -> folly::Future< + typename folly::Unit::Lift::type>::type>; // Executor interface calls addTaskRemote void add(folly::Func f) override { diff --git a/folly/experimental/fibers/test/FibersTest.cpp b/folly/experimental/fibers/test/FibersTest.cpp index ac66df6e..08d182ff 100644 --- a/folly/experimental/fibers/test/FibersTest.cpp +++ b/folly/experimental/fibers/test/FibersTest.cpp @@ -1406,6 +1406,29 @@ TEST(FiberManager, remoteFutureTest) { EXPECT_EQ(v2, testValue2); } +// Test that a void function produes a Future. +TEST(FiberManager, remoteFutureVoidUnitTest) { + FiberManager fiberManager(folly::make_unique()); + auto& loopController = + dynamic_cast(fiberManager.loopController()); + + bool ranLocal = false; + folly::Future futureLocal = + fiberManager.addTaskFuture([&]() { ranLocal = true; }); + + bool ranRemote = false; + folly::Future futureRemote = + fiberManager.addTaskRemoteFuture([&]() { ranRemote = true; }); + + loopController.loop([&]() { loopController.stop(); }); + + futureLocal.wait(); + ASSERT_TRUE(ranLocal); + + futureRemote.wait(); + ASSERT_TRUE(ranRemote); +} + TEST(FiberManager, nestedFiberManagers) { folly::EventBase outerEvb; folly::EventBase innerEvb;