AuroraRuntime/Include/Aurora/Threading/LockGuardTry.hpp
Reece 9c8224b931 [+] Added TryLockGuard; AU_TRY_LOCK_GUARD_RET_DEF, AU_TRY_LOCK_GUARD, AU_TRY_LOCK_GUARD_RET_VAL, AU_TRY_LOCK_GUARD_RET_DEF
[+] Added explicit AU_WHAT macro, I think people are familiar with this nomenclatures, not sure.
[+] Added try catch around logger write line. I'm sure something will explode around here sooner or later
2021-12-24 16:25:12 +00:00

93 lines
2.8 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 TryLockGuard
{
private:
using Internal_t = std::remove_pointer_t<T>;
bool bLockSuccessful;
public:
TryLockGuard(T &lock)
{
if constexpr (std::is_pointer_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)
{
bLockSuccessful = annoying_->get()->TryLock();
}
else
{
bLockSuccessful = annoying_->TryLock();
}
}
TryLockGuard(T &&lock)
{
if constexpr (std::is_pointer_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)
{
bLockSuccessful = annoying_->get()->TryLock();
}
else
{
bLockSuccessful = annoying_->TryLock();
}
}
~TryLockGuard()
{
if (!bLockSuccessful)
{
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();
}
}
const bool Locked() const
{
return bLockSuccessful;
}
private:
std::conditional_t<std::is_pointer_v<T>, T, T*> annoying_;
};
#define AU_TRY_LOCK_GUARD(variable) Aurora::Threading::TryLockGuard<decltype(variable)> AU_CONCAT(__stack_lock, __COUNTER__) (variable);
#define AU_TRY_LOCK_GUARD_RET_(variable, value, counter) Aurora::Threading::TryLockGuard<decltype(variable)> AU_CONCAT(__stack_lock, counter) (variable); if (!(AU_CONCAT(__stack_lock, counter).Locked())) return value;
#define AU_TRY_LOCK_GUARD_RET_VAL(variable, value) AU_TRY_LOCK_GUARD_RET_(variable, value, AU_WHAT(__COUNTER__))
#define AU_TRY_LOCK_GUARD_RET_DEF(variable) AU_TRY_LOCK_GUARD_RET_VAL(variable, {})
#define AU_TRY_LOCK_GUARD_RET(variable) AU_TRY_LOCK_GUARD_RET_VAL(variable, )
}