110 lines
3.6 KiB
C++
110 lines
3.6 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: LockGuardTry.hpp
|
|
Date: 2021-12-25
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
|
|
namespace Aurora::Threading
|
|
{
|
|
template<typename T>
|
|
class TryLockGuard
|
|
{
|
|
private:
|
|
using Internal_t = AuRemovePointer_t<T>;
|
|
bool bLockSuccessful;
|
|
|
|
public:
|
|
|
|
TryLockGuard(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)
|
|
{
|
|
bLockSuccessful = annoying_->get()->TryLock();
|
|
}
|
|
else
|
|
{
|
|
bLockSuccessful = annoying_->TryLock();
|
|
}
|
|
}
|
|
|
|
TryLockGuard(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)
|
|
{
|
|
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:
|
|
AuConditional_t<AuIsPointer_v<T>, T, T*> annoying_;
|
|
};
|
|
|
|
|
|
// Named try-lock guard. Use when you wish to poll ::Locked(), otherwise use the auto-return macros
|
|
// There is no point in using anonymous lock handles w/o checking the lock state or auto-returning
|
|
// Where variable = an iwaitable-like interface reference, pointer, or shared pointer; value = reference to a boolean
|
|
// name = name of handle variable
|
|
#define AU_TRY_LOCK_GUARD_NAMED(variable, name) Aurora::Threading::TryLockGuard<decltype(variable)> name(variable);
|
|
|
|
/// @hideinitializer
|
|
#define AU_TRY_LOCK_GUARD_RET_NAMED(variable, value, name) AU_TRY_LOCK_GUARD_NAMED(variable, name) if (!(name.Locked())) return value;
|
|
|
|
/// @hideinitializer
|
|
#define AU_TRY_LOCK_GUARD_RET_COUNTERVAL(variable, value, counter) AU_TRY_LOCK_GUARD_RET_NAMED(variable, value, AU_CONCAT(__stack_lock, counter))
|
|
|
|
// where variable = an iwaitable-like interface reference, pointer, or shared pointer; value = reference to a boolean
|
|
// value = failure return object
|
|
#define AU_TRY_LOCK_GUARD_RET_VAL(variable, value) AU_TRY_LOCK_GUARD_RET_COUNTERVAL(variable, value, AU_WHAT(__COUNTER__))
|
|
|
|
// where variable = an iwaitable-like interface reference, pointer, or shared pointer
|
|
#define AU_TRY_LOCK_GUARD_RET_DEF(variable) AU_TRY_LOCK_GUARD_RET_VAL(variable, {})
|
|
|
|
// where variable = an iwaitable-like interface reference, pointer
|
|
#define AU_TRY_LOCK_GUARD_RET(variable) AU_TRY_LOCK_GUARD_RET_VAL(variable, )
|
|
} |