Commit d1f59dd5 authored by Pranav Thulasiram Bhat's avatar Pranav Thulasiram Bhat Committed by Facebook GitHub Bot

Awaiting Coro

Summary:
This diff implements `coro_wait`, an annotated helper to block on the execution of a coroutine.

The goal here is to provide an easy way to migrate from fibers to coroutines.

Reviewed By: A5he

Differential Revision: D21978610

fbshipit-source-id: d59a222978d9db2963656312e71c7dcb2b9a11e9
parent a7d768ee
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/experimental/coro/BlockingWait.h>
#include <folly/experimental/coro/Task.h>
#include <folly/fibers/async/Async.h>
namespace folly {
namespace fibers {
namespace async {
/**
* Block on a task's execution. Should be called from an Async annotated
* function. The fiber executing task_wait will block while the task is
* suspended, and the task's work will be executed inline on the fiber main
* context.
*/
template <typename T>
Async<T> taskWait(folly::coro::Task<T>&& task) {
return folly::coro::blockingWait(std::move(task));
}
} // namespace async
} // namespace fibers
} // namespace folly
...@@ -42,6 +42,10 @@ ...@@ -42,6 +42,10 @@
#include <folly/fibers/async/Future.h> #include <folly/fibers/async/Future.h>
#include <folly/io/async/ScopedEventBaseThread.h> #include <folly/io/async/ScopedEventBaseThread.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
#if FOLLY_HAS_COROUTINES
#include <folly/experimental/coro/Sleep.h>
#include <folly/fibers/async/Task.h>
#endif
using namespace folly::fibers; using namespace folly::fibers;
...@@ -2763,3 +2767,27 @@ TEST(FiberManager, asyncFuture) { ...@@ -2763,3 +2767,27 @@ TEST(FiberManager, asyncFuture) {
}) })
.getVia(&evb); .getVia(&evb);
} }
#if FOLLY_HAS_COROUTINES
TEST(FiberManager, asyncTask) {
auto coroFn =
[]() -> folly::coro::Task<std::tuple<std::thread::id, bool, bool>> {
co_await folly::coro::sleep(std::chrono::milliseconds(1));
co_return std::make_tuple(
std::this_thread::get_id(),
FiberManager::getFiberManagerUnsafe() != nullptr,
onFiber());
};
folly::EventBase evb;
auto& fm = getFiberManager(evb);
fm.addTaskFuture([&]() {
// Coroutine should run to completion on fiber main context
EXPECT_EQ(
std::make_tuple(std::this_thread::get_id(), true, false),
async::init_await(async::taskWait(coroFn())));
})
.getVia(&evb);
}
#endif
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