skullc-peripherals/Utility/Inc/utility_atomicscopeguard.hpp
Erki 55a8efa579
All checks were successful
continuous-integration/drone/push Build is passing
Clang format pass
2021-04-03 17:49:25 +03:00

49 lines
950 B
C++

/*
* utility_atomicscopeguard.hpp
*
* Created on: Mar 21, 2021
* Author: erki
*/
#ifndef UTILITY_INC_UTILITY_ATOMICSCOPEGUARD_HPP_
#define UTILITY_INC_UTILITY_ATOMICSCOPEGUARD_HPP_
#include <cstdint>
namespace Utility
{
template<typename H>
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<typename H>
std::int32_t AtomicScopeGuard<H>::_reentrancy_counter = 0;
}// namespace Utility
#endif /* UTILITY_INC_UTILITY_ATOMICSCOPEGUARD_HPP_ */