Interface for distributed lock strategies.
Example
public:
~MockDistLockStrategy() override { EXPECT_FALSE(IsLocked()); }
void Acquire(std::chrono::milliseconds,
const std::string& locker_id) override {
attempts_++;
auto locked_by = locked_by_var_.Lock();
if (!locked_by->empty() && *locked_by != locker_id)
if (!allowed_) throw std::runtime_error("not allowed");
*locked_by = locker_id;
}
void Release(
const std::string& locker_id)
override {
auto locked_by = locked_by_var_.Lock();
if (*locked_by == locker_id) locked_by->clear();
}
bool IsLocked() {
auto locked_by = locked_by_var_.Lock();
return !locked_by->empty();
}
void Allow(bool allowed) { allowed_ = allowed; }
void SetLockedBy(const std::string& whom) {
auto locked_by = locked_by_var_.Lock();
*locked_by = whom;
}
size_t GetAttemptsCount() const { return attempts_; }
private:
std::atomic<bool> allowed_{false};
std::atomic<size_t> attempts_{0};
};
Definition at line 24 of file dist_lock_strategy.hpp.