/* * utility_atomicscopeguard.hpp * * Created on: Mar 21, 2021 * Author: erki */ #ifndef UTILITY_INC_UTILITY_ATOMICSCOPEGUARD_HPP_ #define UTILITY_INC_UTILITY_ATOMICSCOPEGUARD_HPP_ #include namespace Utility { template struct AtomicScopeGuard { using hal = H; AtomicScopeGuard() { hal::disableInterrupts(); _reentrancy_counter++; } AtomicScopeGuard(const AtomicScopeGuard&) = delete; AtomicScopeGuard(AtomicScopeGuard&&) = delete; AtomicScopeGuard& operator=(const AtomicScopeGuard&) = delete; AtomicScopeGuard& operator=(AtomicScopeGuard&&) = delete; ~AtomicScopeGuard() { _reentrancy_counter--; if (!_reentrancy_counter) hal::enableInterrupts(); } private: static std::int32_t _reentrancy_counter; }; template std::int32_t AtomicScopeGuard::_reentrancy_counter = 0; }// namespace Utility #endif /* UTILITY_INC_UTILITY_ATOMICSCOPEGUARD_HPP_ */