AuroraRuntime/Include/Aurora/Threading/LockGuard.hpp

89 lines
2.1 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: LockGuard.hpp
Date: 2021-6-10
Author: Reece
***/
#pragma once
namespace Aurora::Threading
{
template<typename T>
class LockGuard
{
private:
using Internal_t = AuRemovePointer_t<T>;
public:
LockGuard(T &lock)
{
if constexpr (AuIsPointer_v<T>)
{
annoying_ = lock;
}
else
{
annoying_ = &lock;
}
if constexpr (AuIsBaseOfTemplate<AURORA_RUNTIME_AU_SHARED_PTR, Internal_t>::value || AuIsBaseOfTemplate<AURORA_RUNTIME_AU_UNIQUE_PTR, Internal_t>::value)
{
annoying_->get()->Lock();
}
else
{
annoying_->Lock();
}
constructed_ = true;
}
LockGuard(T &&lock)
{
if constexpr (AuIsPointer_v<T>)
{
annoying_ = lock;
}
else
{
annoying_ = &lock;
}
if constexpr (AuIsBaseOfTemplate<AURORA_RUNTIME_AU_SHARED_PTR, Internal_t>::value || AuIsBaseOfTemplate<AURORA_RUNTIME_AU_UNIQUE_PTR, Internal_t>::value)
{
annoying_->get()->Lock();
}
else
{
annoying_->Lock();
}
constructed_ = true;
}
~LockGuard()
{
if (!constructed_)
{
return;
}
if constexpr (AuIsBaseOfTemplate<AURORA_RUNTIME_AU_SHARED_PTR, Internal_t>::value || AuIsBaseOfTemplate<AURORA_RUNTIME_AU_UNIQUE_PTR, Internal_t>::value)
{
annoying_->get()->Unlock();
}
else
{
annoying_->Unlock();
}
}
private:
AuConditional_t<AuIsPointer_v<T>, T, T*> annoying_;
bool constructed_ {};
};
#define AU_LOCK_GUARD(variable) Aurora::Threading::LockGuard<decltype(variable)> AU_CONCAT(__stack_lock, __COUNTER__) (variable);
}