#pragma once #include #include #include namespace DB { /// An atomic variable that is used to block and interrupt certain actions /// If it is not zero then actions related with it should be considered as interrupted class ActionBlocker { public: ActionBlocker() : counter(std::make_shared(0)) {} bool isCancelled() const { return *counter > 0; } /// Temporarily blocks corresponding actions (while the returned object is alive) friend class ActionLock; ActionLock cancel() { return ActionLock(*this); } /// Cancel the actions forever. void cancelForever() { ++(*counter); } /// Returns reference to counter to allow to watch on it directly. const std::atomic & getCounter() const { return *counter; } private: using Counter = std::atomic; using CounterPtr = std::shared_ptr; CounterPtr counter; }; }