Commit 7c6897aa authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

Use Executor::KeepAlive in coro::Task

Reviewed By: lewissbaker

Differential Revision: D13657174

fbshipit-source-id: afdda6abf02cb9e10b8696e1d81e9c2881521788
parent 25374a6a
...@@ -73,7 +73,7 @@ class TaskPromiseBase { ...@@ -73,7 +73,7 @@ class TaskPromiseBase {
friend class FinalAwaiter; friend class FinalAwaiter;
protected: protected:
TaskPromiseBase() noexcept : executor_(nullptr) {} TaskPromiseBase() noexcept {}
public: public:
std::experimental::suspend_always initial_suspend() noexcept { std::experimental::suspend_always initial_suspend() noexcept {
...@@ -90,11 +90,11 @@ class TaskPromiseBase { ...@@ -90,11 +90,11 @@ class TaskPromiseBase {
template <typename Awaitable> template <typename Awaitable>
auto await_transform(Awaitable&& awaitable) noexcept { auto await_transform(Awaitable&& awaitable) noexcept {
using folly::coro::co_viaIfAsync; using folly::coro::co_viaIfAsync;
return co_viaIfAsync(executor_, static_cast<Awaitable&&>(awaitable)); return co_viaIfAsync(executor_.get(), static_cast<Awaitable&&>(awaitable));
} }
auto await_transform(co_current_executor_t) noexcept { auto await_transform(co_current_executor_t) noexcept {
return AwaitableReady<folly::Executor*>{executor_}; return AwaitableReady<folly::Executor*>{executor_.get()};
} }
private: private:
...@@ -105,7 +105,7 @@ class TaskPromiseBase { ...@@ -105,7 +105,7 @@ class TaskPromiseBase {
friend class folly::coro::Task; friend class folly::coro::Task;
std::experimental::coroutine_handle<> continuation_; std::experimental::coroutine_handle<> continuation_;
folly::Executor* executor_; folly::Executor::KeepAlive<> executor_;
}; };
template <typename T> template <typename T>
...@@ -191,7 +191,7 @@ class FOLLY_NODISCARD TaskWithExecutor { ...@@ -191,7 +191,7 @@ class FOLLY_NODISCARD TaskWithExecutor {
} }
folly::Executor* executor() const noexcept { folly::Executor* executor() const noexcept {
return coro_.promise().executor_; return coro_.promise().executor_.get();
} }
void swap(TaskWithExecutor& t) noexcept { void swap(TaskWithExecutor& t) noexcept {
...@@ -237,7 +237,7 @@ class FOLLY_NODISCARD TaskWithExecutor { ...@@ -237,7 +237,7 @@ class FOLLY_NODISCARD TaskWithExecutor {
std::experimental::coroutine_handle<> continuation) noexcept { std::experimental::coroutine_handle<> continuation) noexcept {
auto& promise = coro_.promise(); auto& promise = coro_.promise();
DCHECK(!promise.continuation_); DCHECK(!promise.continuation_);
DCHECK(promise.executor_ != nullptr); DCHECK(promise.executor_);
promise.continuation_ = continuation; promise.continuation_ = continuation;
promise.executor_->add( promise.executor_->add(
...@@ -344,7 +344,7 @@ class FOLLY_NODISCARD Task { ...@@ -344,7 +344,7 @@ class FOLLY_NODISCARD Task {
/// task on the specified executor. /// task on the specified executor.
FOLLY_NODISCARD FOLLY_NODISCARD
TaskWithExecutor<T> scheduleOn(Executor* executor) && noexcept { TaskWithExecutor<T> scheduleOn(Executor* executor) && noexcept {
coro_.promise().executor_ = executor; coro_.promise().executor_ = getKeepAliveToken(executor);
return TaskWithExecutor<T>{std::exchange(coro_, {})}; return TaskWithExecutor<T>{std::exchange(coro_, {})};
} }
...@@ -404,7 +404,7 @@ auto detail::TaskPromiseBase::await_transform(Task<T>&& t) noexcept { ...@@ -404,7 +404,7 @@ auto detail::TaskPromiseBase::await_transform(Task<T>&& t) noexcept {
}; };
// Child task inherits the awaiting task's executor // Child task inherits the awaiting task's executor
t.coro_.promise().executor_ = executor_; t.coro_.promise().executor_ = executor_.copy();
return Awaiter{std::exchange(t.coro_, {})}; return Awaiter{std::exchange(t.coro_, {})};
} }
......
...@@ -119,6 +119,15 @@ TEST(Coro, Sleep) { ...@@ -119,6 +119,15 @@ TEST(Coro, Sleep) {
chrono::round<std::chrono::seconds>(totalTime), std::chrono::seconds{1}); chrono::round<std::chrono::seconds>(totalTime), std::chrono::seconds{1});
} }
TEST(Coro, ExecutorKeepAlive) {
auto future = [] {
ScopedEventBaseThread evbThread;
return taskSleep().scheduleOn(evbThread.getEventBase()).start();
}();
EXPECT_TRUE(future.isReady());
}
coro::Task<int> taskException() { coro::Task<int> taskException() {
throw std::runtime_error("Test exception"); throw std::runtime_error("Test exception");
co_return 42; co_return 42;
......
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