2021-12-24 16:25:12 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2021-12-26 15:48:14 +00:00
|
|
|
File: LockGuardTry.hpp
|
|
|
|
Date: 2021-12-25
|
2021-12-24 16:25:12 +00:00
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace Aurora::Threading
|
|
|
|
{
|
|
|
|
template<typename T>
|
|
|
|
class TryLockGuard
|
|
|
|
{
|
|
|
|
private:
|
2022-01-19 18:18:13 +00:00
|
|
|
using Internal_t = AuRemovePointer_t<T>;
|
2021-12-24 16:25:12 +00:00
|
|
|
bool bLockSuccessful;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
TryLockGuard(T &lock)
|
|
|
|
{
|
2022-01-19 17:08:13 +00:00
|
|
|
if constexpr (AuIsPointer_v<T>)
|
2021-12-24 16:25:12 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2022-01-19 17:08:13 +00:00
|
|
|
if constexpr (AuIsPointer_v<T>)
|
2021-12-24 16:25:12 +00:00
|
|
|
{
|
|
|
|
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:
|
2022-01-19 18:37:00 +00:00
|
|
|
AuConditional_t<AuIsPointer_v<T>, T, T*> annoying_;
|
2021-12-24 16:25:12 +00:00
|
|
|
};
|
|
|
|
|
2022-01-17 15:21:36 +00:00
|
|
|
|
|
|
|
// 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
|
2021-12-24 16:25:12 +00:00
|
|
|
#define AU_TRY_LOCK_GUARD_RET_DEF(variable) AU_TRY_LOCK_GUARD_RET_VAL(variable, {})
|
2022-01-17 15:21:36 +00:00
|
|
|
|
|
|
|
// where variable = an iwaitable-like interface reference, pointer
|
2021-12-24 16:25:12 +00:00
|
|
|
#define AU_TRY_LOCK_GUARD_RET(variable) AU_TRY_LOCK_GUARD_RET_VAL(variable, )
|
2021-12-26 15:48:14 +00:00
|
|
|
}
|