aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/cancellation
diff options
context:
space:
mode:
authorqrort <qrort@yandex-team.com>2022-12-02 11:31:25 +0300
committerqrort <qrort@yandex-team.com>2022-12-02 11:31:25 +0300
commitb1f4ffc9c8abff3ba58dc1ec9a9f92d2f0de6806 (patch)
tree2a23209faf0fea5586a6d4b9cee60d1b318d29fe /library/cpp/threading/cancellation
parent559174a9144de40d6bb3997ea4073c82289b4974 (diff)
downloadydb-b1f4ffc9c8abff3ba58dc1ec9a9f92d2f0de6806.tar.gz
remove kikimr/driver DEPENDS
Diffstat (limited to 'library/cpp/threading/cancellation')
-rw-r--r--library/cpp/threading/cancellation/README.md112
-rw-r--r--library/cpp/threading/cancellation/cancellation_token.cpp1
-rw-r--r--library/cpp/threading/cancellation/cancellation_token.h105
3 files changed, 0 insertions, 218 deletions
diff --git a/library/cpp/threading/cancellation/README.md b/library/cpp/threading/cancellation/README.md
deleted file mode 100644
index 98e0e9b299f..00000000000
--- a/library/cpp/threading/cancellation/README.md
+++ /dev/null
@@ -1,112 +0,0 @@
-The Cancellation library
-========================
-
-Intro
------
-
-This small library provides primitives for implementation of a cooperative cancellation of long running or asynchronous operations.
-The design has been copied from the well-known CancellationTokenSource/CancellationToken classes of the .NET Framework
-
-To use the library include `cancellation_token.h`.
-
-Examples
---------
-
-1. Simple check for cancellation
-
- ```c++
- void LongRunningOperation(TCancellationToken token) {
- ...
- if (token.IsCancellationRequested()) {
- return;
- }
- ...
- }
-
- TCancellationTokenSource source;
- TThread thread([token = source.Token()]() { LongRunningOperation(std::move(token)); });
- thread.Start();
- ...
- source.Cancel();
- thread.Join();
- ```
-
-2. Exit via an exception
-
- ```c++
- void LongRunningOperation(TCancellationToken token) {
- try {
- for (;;) {
- ...
- token.ThrowIfCancellationRequested();
- ...
- }
- } catch (TOperationCancelledException const&) {
- return;
- } catch (...) {
- Y_FAIL("Never should be there")
- }
- }
-
- TCancellationTokenSource source;
- TThread thread([token = source.Token()]() { LongRunningOperation(std::move(token)); });
- thread.Start();
- ...
- source.Cancel();
- thread.Join();
- ```
-
-3. Periodic poll with cancellation
-
- ```c++
- void LongRunningOperation(TCancellationToken token) {
- while (!token.Wait(PollInterval)) {
- ...
- }
- }
-
- TCancellationTokenSource source;
- TThread thread([token = source.Token()]() { LongRunningOperation(std::move(token)); });
- thread.Start();
- ...
- source.Cancel();
- thread.Join();
- ```
-
-4. Waiting on the future
-
- ```c++
- TFuture<void> InnerOperation();
- TFuture<void> OuterOperation(TCancellationToken token) {
- return WaitAny(FirstOperation(), token.Future())
- .Apply([token = std::move(token)](auto&&) {
- token.ThrowIfCancellationRequested();
- });
- }
-
- TCancellationTokenSource source;
- auto future = OuterOperation();
- ...
- source.Cancel()
- ...
- try {
- auto value = future.ExtractValueSync();
- } catch (TOperationCancelledException const&) {
- // cancelled
- }
- ```
-
-5. Using default token when no cancellation needed
-
- ```c++
- void LongRunningOperation(TCancellationToken token) {
- ...
- if (token.IsCancellationRequested()) {
- return;
- }
- ...
- }
-
- // We do not want to cancel the operation. So, there is no need to create a cancellation token source
- LongRunningOperation(TCancellationToken::Default);
- ```
diff --git a/library/cpp/threading/cancellation/cancellation_token.cpp b/library/cpp/threading/cancellation/cancellation_token.cpp
deleted file mode 100644
index 1a0a19f690f..00000000000
--- a/library/cpp/threading/cancellation/cancellation_token.cpp
+++ /dev/null
@@ -1 +0,0 @@
-#include "cancellation_token.h"
diff --git a/library/cpp/threading/cancellation/cancellation_token.h b/library/cpp/threading/cancellation/cancellation_token.h
deleted file mode 100644
index 337e2cfda0a..00000000000
--- a/library/cpp/threading/cancellation/cancellation_token.h
+++ /dev/null
@@ -1,105 +0,0 @@
-#pragma once
-
-#include "operation_cancelled_exception.h"
-
-#include <library/cpp/threading/future/future.h>
-
-#include <util/generic/ptr.h>
-#include <util/generic/singleton.h>
-
-namespace NThreading {
-
-class TCancellationTokenSource;
-
-//! A cancellation token could be passed to an async or long running operation to perform a cooperative operation cancel
-class TCancellationToken {
-private:
- TFuture<void> Future_;
-
-public:
- TCancellationToken() = delete;
- TCancellationToken(const TCancellationToken&) noexcept = default;
- TCancellationToken(TCancellationToken&&) noexcept = default;
- TCancellationToken& operator = (const TCancellationToken&) noexcept = default;
- TCancellationToken& operator = (TCancellationToken&&) noexcept = default;
-
- //! Shows whether a cancellation has been requested
- bool IsCancellationRequested() const {
- return Future_.HasValue();
- }
-
- //! Throws the TOperationCancelledException if a cancellation has been requested
- void ThrowIfCancellationRequested() const {
- if (IsCancellationRequested()) {
- ythrow TOperationCancelledException();
- }
- }
-
- //! Waits for a cancellation
- bool Wait(TDuration duration) const {
- return Future_.Wait(duration);
- }
-
- bool Wait(TInstant deadline) const {
- return Future_.Wait(deadline);
- }
-
- void Wait() const {
- return Future_.Wait();
- }
-
- //! Returns a future that could be used for waiting for a cancellation
- TFuture<void> const& Future() const noexcept {
- return Future_;
- }
-
- //! The default cancellation token that cannot be cancelled
- static TCancellationToken const& Default() {
- return *SingletonWithPriority<TCancellationToken, 0>(NewPromise());
- }
-
-private:
- TCancellationToken(TFuture<void> future)
- : Future_(std::move(future))
- {
- }
-
-private:
- friend class TCancellationTokenSource;
-
- Y_DECLARE_SINGLETON_FRIEND();
-};
-
-//! A cancellation token source produces cancellation tokens to be passed to cancellable operations
-class TCancellationTokenSource {
-private:
- TPromise<void> Promise;
-
-public:
- TCancellationTokenSource()
- : Promise(NewPromise())
- {
- }
-
- TCancellationTokenSource(TCancellationTokenSource const&) = delete;
- TCancellationTokenSource(TCancellationTokenSource&&) = delete;
- TCancellationTokenSource& operator=(TCancellationTokenSource const&) = delete;
- TCancellationTokenSource& operator=(TCancellationTokenSource&&) = delete;
-
- //! Shows whether a cancellation has been requested
- bool IsCancellationRequested() const noexcept {
- return Promise.HasValue();
- }
-
- //! Produces a cancellation token
- TCancellationToken Token() const {
- return TCancellationToken(Promise.GetFuture());
- }
-
- //! Propagates a cancel request to all produced tokens
- void Cancel() noexcept {
- Promise.TrySetValue();
- }
-};
-
-}