| 1 | #pragma once |
| 2 | #include <memory> |
| 3 | #include <atomic> |
| 4 | |
| 5 | |
| 6 | namespace DB |
| 7 | { |
| 8 | |
| 9 | class ActionBlocker; |
| 10 | using StorageActionBlockType = size_t; |
| 11 | |
| 12 | /// Blocks related action while a ActionLock instance exists |
| 13 | /// ActionBlocker could be destroyed before the lock, in this case ActionLock will safely do nothing in its destructor |
| 14 | class ActionLock |
| 15 | { |
| 16 | public: |
| 17 | |
| 18 | ActionLock() = default; |
| 19 | |
| 20 | explicit ActionLock(const ActionBlocker & blocker); |
| 21 | |
| 22 | ActionLock(ActionLock && other); |
| 23 | ActionLock & operator=(ActionLock && other); |
| 24 | |
| 25 | ActionLock(const ActionLock & other) = delete; |
| 26 | ActionLock & operator=(const ActionLock & other) = delete; |
| 27 | |
| 28 | bool expired() const |
| 29 | { |
| 30 | return counter_ptr.expired(); |
| 31 | } |
| 32 | |
| 33 | ~ActionLock() |
| 34 | { |
| 35 | if (auto counter = counter_ptr.lock()) |
| 36 | --(*counter); |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | using Counter = std::atomic<int>; |
| 41 | using CounterWeakPtr = std::weak_ptr<Counter>; |
| 42 | |
| 43 | CounterWeakPtr counter_ptr; |
| 44 | }; |
| 45 | |
| 46 | } |
| 47 | |