Commit 0c47744a authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

add co_cancelled

Summary: [folly] Add `co_cancelled`, effectively a shorthand for `co_error(OperationCancelled{})`.

Reviewed By: Mizuchi, lewissbaker

Differential Revision: D25627993

fbshipit-source-id: 91f80772a8b179a4de0fe3b29dfe7eecaafb8e1b
parent 590d4349
...@@ -206,7 +206,7 @@ auto collectAllImpl( ...@@ -206,7 +206,7 @@ auto collectAllImpl(
// Parent task was cancelled before any child tasks failed. // Parent task was cancelled before any child tasks failed.
// Complete with the OperationCancelled error instead of the // Complete with the OperationCancelled error instead of the
// child task's errors. // child task's errors.
co_yield co_error(OperationCancelled{}); co_yield co_cancelled;
} }
co_return std::tuple<collect_all_component_t<SemiAwaitables>...>{ co_return std::tuple<collect_all_component_t<SemiAwaitables>...>{
...@@ -320,7 +320,7 @@ auto collectAllRange(InputRange awaitables) ...@@ -320,7 +320,7 @@ auto collectAllRange(InputRange awaitables)
// Cancellation was requested of the parent Task before any of the // Cancellation was requested of the parent Task before any of the
// child tasks failed. // child tasks failed.
co_yield co_error(OperationCancelled{}); co_yield co_cancelled;
} }
std::vector<detail::collect_all_range_component_t< std::vector<detail::collect_all_range_component_t<
...@@ -617,7 +617,7 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency) ...@@ -617,7 +617,7 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency)
co_yield co_error(std::move(firstException)); co_yield co_error(std::move(firstException));
} }
co_yield co_error(OperationCancelled{}); co_yield co_cancelled;
} }
} }
...@@ -778,7 +778,7 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency) ...@@ -778,7 +778,7 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency)
// Otherwise, cancellation was requested before any of the child tasks // Otherwise, cancellation was requested before any of the child tasks
// failed so complete with the OperationCancelled error. // failed so complete with the OperationCancelled error.
co_yield co_error(OperationCancelled{}); co_yield co_cancelled;
} }
std::vector<detail::collect_all_range_component_t< std::vector<detail::collect_all_range_component_t<
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <cassert> #include <cassert>
#include <type_traits> #include <type_traits>
#include <folly/CancellationToken.h>
#include <folly/ExceptionWrapper.h> #include <folly/ExceptionWrapper.h>
#include <folly/Try.h> #include <folly/Try.h>
...@@ -61,5 +62,14 @@ class co_result final { ...@@ -61,5 +62,14 @@ class co_result final {
Try<T> result_; Try<T> result_;
}; };
class co_cancelled_t final {
public:
/* implicit */ operator co_error() const {
return co_error(OperationCancelled{});
}
};
FOLLY_INLINE_VARIABLE constexpr co_cancelled_t co_cancelled{};
} // namespace coro } // namespace coro
} // namespace folly } // namespace folly
...@@ -187,7 +187,7 @@ class ExponentialBackoffWithJitter { ...@@ -187,7 +187,7 @@ class ExponentialBackoffWithJitter {
// Check to see if we were cancelled during the sleep. // Check to see if we were cancelled during the sleep.
const auto& cancelToken = co_await co_current_cancellation_token; const auto& cancelToken = co_await co_current_cancellation_token;
if (cancelToken.isCancellationRequested()) { if (cancelToken.isCancellationRequested()) {
co_yield folly::coro::co_error(OperationCancelled{}); co_yield folly::coro::co_cancelled;
} }
} }
......
...@@ -43,7 +43,7 @@ inline Task<void> sleep(Duration d, Timekeeper* tk) { ...@@ -43,7 +43,7 @@ inline Task<void> sleep(Duration d, Timekeeper* tk) {
co_await baton; co_await baton;
} }
if (cancelled) { if (cancelled) {
co_yield co_error(OperationCancelled()); co_yield co_cancelled;
} }
co_yield co_result(std::move(result)); co_yield co_result(std::move(result));
} }
......
...@@ -171,7 +171,7 @@ coro::Task<void> Semaphore::co_wait() { ...@@ -171,7 +171,7 @@ coro::Task<void> Semaphore::co_wait() {
// sure that we aren't reading it concurrently with a potential write // sure that we aren't reading it concurrently with a potential write
// from a thread requesting cancellation. // from a thread requesting cancellation.
if (cancelled) { if (cancelled) {
co_yield folly::coro::co_error(folly::OperationCancelled{}); co_yield folly::coro::co_cancelled;
} }
co_return; co_return;
......
...@@ -147,7 +147,7 @@ coro::Task<void> SemaphoreBase::co_wait_common(int64_t tokens) { ...@@ -147,7 +147,7 @@ coro::Task<void> SemaphoreBase::co_wait_common(int64_t tokens) {
// sure that we aren't reading it concurrently with a potential write // sure that we aren't reading it concurrently with a potential write
// from a thread requesting cancellation. // from a thread requesting cancellation.
if (cancelled) { if (cancelled) {
co_yield folly::coro::co_error(folly::OperationCancelled{}); co_yield folly::coro::co_cancelled;
} }
co_return; co_return;
......
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