Commit 6284a28e authored by Matthieu Martin's avatar Matthieu Martin Committed by Facebook GitHub Bot

Specify return type of async:: functions

Summary: Simplify API understanding for code reader

Reviewed By: pranavtbhat

Differential Revision: D22272546

fbshipit-source-id: 341625ed9079f3bc4aee09851eadc02fd05b2434
parent 06786016
......@@ -17,6 +17,8 @@
#pragma once
#include <folly/Traits.h>
#include <folly/Unit.h>
#include <folly/functional/Invoke.h>
#include <glog/logging.h>
#include <utility>
......@@ -101,6 +103,9 @@ class [[nodiscard]] Async<void> {
typedef void inner_type;
/* implicit */ Async() {}
/* implicit */ Async(Unit) {}
/* implicit */ Async(Async<Unit> &&) {}
Async(const Async&) = delete;
Async(Async && other) = default;
Async& operator=(const Async&) = delete;
......@@ -161,6 +166,14 @@ struct async_inner_type<Async<T>> {
template <typename T>
using async_inner_type_t = typename async_inner_type<T>::type;
// async_invocable_inner_type
template <typename F>
using async_invocable_inner_type = async_inner_type<invoke_result_t<F>>;
template <typename F>
using async_invocable_inner_type_t =
typename async_invocable_inner_type<F>::type;
} // namespace async
} // namespace fibers
} // namespace folly
......@@ -74,9 +74,8 @@ auto collectAll(Collection&& c) -> decltype(collectAll(c.begin(), c.end())) {
* container or iterators
*/
template <typename... Ts>
Async<
std::tuple<folly::lift_unit_t<async_inner_type_t<invoke_result_t<Ts>>>...>>
collectAll(Ts&&... tasks) {
Async<std::tuple<lift_unit_t<async_invocable_inner_type_t<Ts>>...>> collectAll(
Ts&&... tasks) {
auto future = folly::collectAllUnsafe(addFiberFuture(
std::forward<Ts>(tasks), FiberManager::getFiberManager())...);
auto tuple = await(futureWait(std::move(future)));
......@@ -90,7 +89,7 @@ collectAll(Ts&&... tasks) {
* overflows
*/
template <typename F>
auto executeOnNewFiber(F&& func) {
Async<async_invocable_inner_type_t<F>> executeOnNewFiber(F&& func) {
DCHECK(detail::onFiber());
return futureWait(
addFiberFuture(std::forward<F>(func), FiberManager::getFiberManager()));
......@@ -101,7 +100,9 @@ auto executeOnNewFiber(F&& func) {
* blocking the current fiber.
*/
template <typename F>
auto executeOnRemoteFiber(F&& func, FiberManager& fm) {
Async<async_invocable_inner_type_t<F>> executeOnRemoteFiber(
F&& func,
FiberManager& fm) {
DCHECK(detail::onFiber());
return futureWait(addFiberRemoteFuture(std::forward<F>(func), fm));
}
......
......@@ -57,7 +57,9 @@ void addFiber(F&& func, FiberManager& fm) {
* then block thread
*/
template <typename F>
auto addFiberFuture(F&& func, FiberManager& fm) {
Future<lift_unit_t<async_invocable_inner_type_t<F>>> addFiberFuture(
F&& func,
FiberManager& fm) {
return fm.addTaskFuture(
[func = std::forward<F>(func)]() mutable { return init_await(func()); });
}
......@@ -72,7 +74,9 @@ auto addFiberFuture(F&& func, FiberManager& fm) {
* - executeOnRemoteFiberAndWait: wait on remote fiber from (local) main context
*/
template <typename F>
auto addFiberRemoteFuture(F&& func, FiberManager& fm) {
Future<lift_unit_t<async_invocable_inner_type_t<F>>> addFiberRemoteFuture(
F&& func,
FiberManager& fm) {
return fm.addTaskRemoteFuture(
[func = std::forward<F>(func)]() mutable { return init_await(func()); });
}
......
......@@ -26,9 +26,9 @@ namespace async {
* Async annotated wrapper around fibers::await
*/
template <typename F>
auto promiseWait(F&& func) {
Async<typename FirstArgOf<F>::type::value_type> promiseWait(F&& func) {
// Call into blocking API
return Async{folly::fibers::await(std::forward<F>(func))};
return fibers::await(std::forward<F>(func));
}
} // namespace async
......
......@@ -27,7 +27,8 @@ namespace async {
namespace detail {
template <typename F>
auto executeOnFiberAndWait(F&& func, folly::EventBase& evb, FiberManager& fm) {
lift_unit_t<async_invocable_inner_type_t<F>>
executeOnFiberAndWait(F&& func, folly::EventBase& evb, FiberManager& fm) {
DCHECK(!detail::onFiber());
return addFiberFuture(std::forward<F>(func), fm).getVia(&evb);
}
......@@ -43,7 +44,7 @@ auto executeOnFiberAndWait(F&& func, folly::EventBase& evb, FiberManager& fm) {
* options
*/
template <typename F>
auto executeOnFiberAndWait(
lift_unit_t<async_invocable_inner_type_t<F>> executeOnFiberAndWait(
F&& func,
const FiberManager::Options& opts = FiberManager::Options()) {
folly::EventBase evb;
......@@ -52,14 +53,16 @@ auto executeOnFiberAndWait(
}
template <typename F>
auto executeOnFiberAndWait(F&& func, const FiberManager::FrozenOptions& opts) {
lift_unit_t<async_invocable_inner_type_t<F>> executeOnFiberAndWait(
F&& func,
const FiberManager::FrozenOptions& opts) {
folly::EventBase evb;
return detail::executeOnFiberAndWait(
std::forward<F>(func), evb, getFiberManager(evb, opts));
}
template <typename F>
auto executeOnFiberAndWait(
lift_unit_t<async_invocable_inner_type_t<F>> executeOnFiberAndWait(
F&& func,
folly::EventBase& evb,
const FiberManager::Options& opts = FiberManager::Options()) {
......@@ -68,7 +71,7 @@ auto executeOnFiberAndWait(
}
template <typename F>
auto executeOnFiberAndWait(
lift_unit_t<async_invocable_inner_type_t<F>> executeOnFiberAndWait(
F&& func,
folly::EventBase& evb,
const FiberManager::FrozenOptions& opts) {
......@@ -87,7 +90,9 @@ auto executeOnFiberAndWait(
* library uses a dedicated thread pool to run fibers.
*/
template <typename F>
auto executeOnRemoteFiberAndWait(F&& func, FiberManager& fm) {
lift_unit_t<async_invocable_inner_type_t<F>> executeOnRemoteFiberAndWait(
F&& func,
FiberManager& fm) {
DCHECK(!detail::onFiber());
return addFiberRemoteFuture(std::forward<F>(func), fm).get();
}
......
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