skullc-peripherals/Utility/Inc/utility_atomicscopeguard.hpp
Erki ce4f8eb8f5
All checks were successful
continuous-integration/drone/push Build is passing
New logging interfaces
2021-04-02 00:50:59 +03:00

50 lines
931 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;
}
#endif /* UTILITY_INC_UTILITY_ATOMICSCOPEGUARD_HPP_ */