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