50 lines
931 B
C++
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_ */
|