| 1 | #pragma once | 
|---|
| 2 | #include <atomic> | 
|---|
| 3 | #include <memory> | 
|---|
| 4 | #include <Common/ActionLock.h> | 
|---|
| 5 |  | 
|---|
| 6 |  | 
|---|
| 7 | namespace DB | 
|---|
| 8 | { | 
|---|
| 9 |  | 
|---|
| 10 | /// An atomic variable that is used to block and interrupt certain actions. | 
|---|
| 11 | /// If it is not zero then actions related with it should be considered as interrupted. | 
|---|
| 12 | /// Uses shared_ptr and the lock uses weak_ptr to be able to "hold" a lock when an object with blocker has already died. | 
|---|
| 13 | class ActionBlocker | 
|---|
| 14 | { | 
|---|
| 15 | public: | 
|---|
| 16 | ActionBlocker() : counter(std::make_shared<Counter>(0)) {} | 
|---|
| 17 |  | 
|---|
| 18 | bool isCancelled() const { return *counter > 0; } | 
|---|
| 19 |  | 
|---|
| 20 | /// Temporarily blocks corresponding actions (while the returned object is alive) | 
|---|
| 21 | friend class ActionLock; | 
|---|
| 22 | ActionLock cancel() { return ActionLock(*this); } | 
|---|
| 23 |  | 
|---|
| 24 | /// Cancel the actions forever. | 
|---|
| 25 | void cancelForever() { ++(*counter); } | 
|---|
| 26 |  | 
|---|
| 27 | /// Returns reference to counter to allow to watch on it directly. | 
|---|
| 28 | const std::atomic<int> & getCounter() const { return *counter; } | 
|---|
| 29 |  | 
|---|
| 30 | private: | 
|---|
| 31 | using Counter = std::atomic<int>; | 
|---|
| 32 | using CounterPtr = std::shared_ptr<Counter>; | 
|---|
| 33 |  | 
|---|
| 34 | CounterPtr counter; | 
|---|
| 35 | }; | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|