skullc-peripherals/Utility/Inc/utility_atomicscopeguard.hpp
Erki 60bad24319
Some checks failed
continuous-integration/drone/push Build is failing
The great renaming, part 1
2021-06-08 23:18:56 +03:00

49 lines
935 B
C++

/*
* utility_atomicscopeguard.hpp
*
* Created on: Mar 21, 2021
* Author: erki
*/
#ifndef SKULLC_UTILITY_ATOMICSCOPEGUARD_HPP_
#define SKULLC_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 /* SKULLC_UTILITY_ATOMICSCOPEGUARD_HPP_ */