Commit 3e5cdc05 authored by Lee Howes's avatar Lee Howes Committed by Facebook Github Bot

Add runOnExecutor helper for then(executor, callback)

Summary: Helper function to wrap a nested dispatch. Returns a function object that launches the passed function onto the executor and returns a future representing the return value of the function.

Reviewed By: andriigrynenko

Differential Revision: D15300575

fbshipit-source-id: eca4b4139c6405f216a9f1cbc87a8ec2ff437780
parent f59c30e7
...@@ -1937,6 +1937,19 @@ std::pair<Promise<T>, Future<T>> makePromiseContract(Executor::KeepAlive<> e) { ...@@ -1937,6 +1937,19 @@ std::pair<Promise<T>, Future<T>> makePromiseContract(Executor::KeepAlive<> e) {
return std::make_pair(std::move(p), std::move(f)); return std::make_pair(std::move(p), std::move(f));
} }
template <class F>
auto makeAsyncTask(folly::Executor::KeepAlive<> ka, F&& func) {
return
[func = std::forward<F>(func), ka = std::move(ka)](auto&& param) mutable {
return via(
ka,
[func = std::move(func),
param = std::forward<decltype(param)>(param)]() mutable {
return func(std::forward<decltype(param)>(param));
});
};
}
} // namespace folly } // namespace folly
#if FOLLY_HAS_COROUTINES #if FOLLY_HAS_COROUTINES
......
...@@ -219,11 +219,12 @@ TEST(Via, priority) { ...@@ -219,11 +219,12 @@ TEST(Via, priority) {
TEST(Via, then2) { TEST(Via, then2) {
ManualExecutor x1, x2; ManualExecutor x1, x2;
bool a = false, b = false, c = false; bool a = false, b = false, c = false, d = false;
via(&x1) via(&x1)
.thenValue([&](auto&&) { a = true; }) .thenValue([&](auto&&) { a = true; })
.then(&x2, [&](auto&&) { b = true; }) .then(&x2, [&](auto&&) { b = true; })
.thenValue([&](auto&&) { c = true; }); .thenValue([&](auto&&) { c = true; })
.thenValueInline(folly::makeAsyncTask(&x2, [&](auto&&) { d = true; }));
EXPECT_FALSE(a); EXPECT_FALSE(a);
EXPECT_FALSE(b); EXPECT_FALSE(b);
...@@ -239,6 +240,10 @@ TEST(Via, then2) { ...@@ -239,6 +240,10 @@ TEST(Via, then2) {
x1.run(); x1.run();
EXPECT_TRUE(c); EXPECT_TRUE(c);
EXPECT_FALSE(d);
x2.run();
EXPECT_TRUE(d);
} }
TEST(Via, allowInline) { TEST(Via, allowInline) {
......
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