[+] 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
This commit is contained in:
parent
4bd5c4604c
commit
9c8224b931
93
Include/Aurora/Threading/LockGuardTry.hpp
Normal file
93
Include/Aurora/Threading/LockGuardTry.hpp
Normal file
@ -0,0 +1,93 @@
|
||||
/***
|
||||
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, )
|
||||
}
|
@ -16,6 +16,7 @@
|
||||
#include "Sleep.hpp"
|
||||
|
||||
#include "LockGuard.hpp"
|
||||
#include "LockGuardTry.hpp"
|
||||
|
||||
#include "Threads/Threads.hpp"
|
||||
|
||||
|
@ -25,6 +25,8 @@
|
||||
#define AU_TEMPLATE_ENABLE_WHEN(...) typename std::enable_if<__VA_ARGS__>::type* = nullptr
|
||||
#endif
|
||||
|
||||
#define AU_WHAT(n) n
|
||||
|
||||
/// @hideinitializer
|
||||
#define AU_STRINGIFY_(in) #in
|
||||
#define AU_STRINGIFY(in) AU_STRINGIFY_(in)
|
||||
|
@ -37,21 +37,52 @@ namespace Aurora::Console::Hooks
|
||||
gExternalLineProcessor = subscriber;
|
||||
}
|
||||
|
||||
void WriteLine(const ConsoleMessage &msg)
|
||||
static void WriteLoggerFailedWarning()
|
||||
{
|
||||
AU_LOCK_GUARD(gMutex);
|
||||
static std::string kLoggerError = "Something went from while dispatching a log line\n";
|
||||
Console::WriteStdOut(kLoggerError.data(), kLoggerError.size());
|
||||
|
||||
if (msg.line.find('\n') == AuString::npos) [[likely]]
|
||||
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
||||
OutputDebugStringA(kLoggerError.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
static void TryWrite(const ConsoleMessage &msg)
|
||||
{
|
||||
for (const auto &callback : gLineFunctionalCallbacks)
|
||||
{
|
||||
try
|
||||
{
|
||||
callback(msg);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
WriteLoggerFailedWarning();
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &sub : gLineSubscribers)
|
||||
{
|
||||
try
|
||||
{
|
||||
sub->OnMessage(msg);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
WriteLoggerFailedWarning();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WriteLine(const ConsoleMessage &msg)
|
||||
{
|
||||
AU_LOCK_GUARD(gMutex);
|
||||
|
||||
try
|
||||
{
|
||||
if (msg.line.find('\n') == AuString::npos) [[likely]]
|
||||
{
|
||||
TryWrite(msg);
|
||||
}
|
||||
else [[unlikely]]
|
||||
{
|
||||
@ -61,18 +92,16 @@ namespace Aurora::Console::Hooks
|
||||
ConsoleMessage dup = msg;
|
||||
dup.line = line;
|
||||
|
||||
for (const auto &callback : gLineFunctionalCallbacks)
|
||||
{
|
||||
callback(dup);
|
||||
}
|
||||
|
||||
for (const auto &sub : gLineSubscribers)
|
||||
{
|
||||
sub->OnMessage(dup);
|
||||
}
|
||||
TryWrite(msg);
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// Loggers experiencing something fucky is a liablity
|
||||
WriteLoggerFailedWarning();
|
||||
}
|
||||
}
|
||||
|
||||
void Init()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user