49 lines
935 B
C++
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_ */
|