Commit 0c27031c authored by Matthieu Martin's avatar Matthieu Martin Committed by Facebook GitHub Bot

Move addFiberFuture from detail::

Summary:
After pursuing adoption and more discussion, it seems clear that this was probably trying to push "hiding fiber details" one step too far.
See comment in code for more details

Reviewed By: yfeldblum, pranavtbhat

Differential Revision: D22259585

fbshipit-source-id: 798cada6527332faf7e4db9942a964d6b7cca1e4
parent 10dd9f4b
......@@ -77,7 +77,7 @@ template <typename... Ts>
Async<
std::tuple<folly::lift_unit_t<async_inner_type_t<invoke_result_t<Ts>>>...>>
collectAll(Ts&&... tasks) {
auto future = folly::collectAllUnsafe(detail::addFiberFuture(
auto future = folly::collectAllUnsafe(addFiberFuture(
std::forward<Ts>(tasks), FiberManager::getFiberManager())...);
auto tuple = await(futureWait(std::move(future)));
return Async(folly::unwrapTryTuple(std::move(tuple)));
......@@ -92,8 +92,8 @@ collectAll(Ts&&... tasks) {
template <typename F>
auto executeOnNewFiber(F&& func) {
DCHECK(detail::onFiber());
return futureWait(detail::addFiberFuture(
std::forward<F>(func), FiberManager::getFiberManager()));
return futureWait(
addFiberFuture(std::forward<F>(func), FiberManager::getFiberManager()));
}
} // namespace async
......
......@@ -23,28 +23,45 @@ namespace folly {
namespace fibers {
namespace async {
namespace detail {
// These functions wrap the corresponding FiberManager members.
// The difference is that these functions accept callables which
// return Async results.
//
// Implementation notes:
// These functions don't respect some design principles of the library:
// - Strict separation of functions running on main context and fiber context
// - Avoid using the Awaitable of the other async frameworks in the public
// interface, under long-term goal of decoupling the frameworks
// - Keep FiberManager as an implementation detail, in favor of Executor in
// the public interface
//
// These functions should ultimately live in detail::, but are public to
// enable early adoption of the library.
/**
* Schedule an async-annotated functor to run on a fiber manager. Returns a
* future for the result.
*
* In most cases, you probably want to use executeOnFiberAndWait instead
* Schedule an async-annotated functor to run on a fiber manager.
*/
template <typename F>
auto addFiberFuture(F&& func, FiberManager& fm) {
return fm.addTaskFuture(
void addFiber(F&& func, FiberManager& fm) {
fm.addTask(
[func = std::forward<F>(func)]() mutable { return init_await(func()); });
}
} // namespace detail
/**
* Schedule an async-annotated functor to run on a fiber manager.
* Returns a Future for the result.
*
* In most cases, prefer those options instead:
* - executeOnNewFiber: reset fiber stack usage from fiber context
* - executeOnFiberAndWait: initialize fiber context from main context
* then block thread
*/
template <typename F>
void addFiber(F&& func, FiberManager& fm) {
fm.addTask(
auto addFiberFuture(F&& func, FiberManager& fm) {
return fm.addTaskFuture(
[func = std::forward<F>(func)]() mutable { return init_await(func()); });
}
} // namespace async
} // namespace fibers
} // namespace folly
......@@ -248,7 +248,7 @@ TEST(FiberManager, asyncFiberManager) {
{
folly::EventBase evb;
bool completed = false;
async::detail::addFiberFuture(
async::addFiberFuture(
[&]() -> async::Async<void> {
completed = true;
return {};
......@@ -260,7 +260,7 @@ TEST(FiberManager, asyncFiberManager) {
size_t count = 0;
EXPECT_EQ(
1,
async::detail::addFiberFuture(
async::addFiberFuture(
[count]() mutable -> async::Async<int> { return ++count; },
getFiberManager(evb))
.getVia(&evb));
......
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