#include "ledger.hpp" namespace scoped { bool Gate::acquire(int owner) { if (owner < 1 || owner > owner_limit) { ++state_.denied; return false; } if (state_.depth != 0) { ++state_.denied; return false; } state_.owner = owner; ++state_.depth; ++state_.acquisitions; if (state_.depth > state_.maximum_depth) { state_.maximum_depth = state_.depth; } return true; } bool Gate::release(int owner) { if (owner < 1 || owner > owner_limit || state_.depth == 0 || state_.owner != owner) { ++state_.invalid_releases; return false; } --state_.depth; ++state_.releases; if (state_.depth == 0) { state_.owner = 0; } return true; } void Gate::reset() { state_ = GateView{}; } GateView Gate::view() const { return state_; } Scope::Scope(Gate& gate, int owner) : gate_(gate), owner_(owner), acquired_(gate.acquire(owner)) { } Scope::~Scope() { if (acquired_) { gate_.release(owner_); } } bool Scope::acquired() const { return acquired_; } }