96 lines
2.2 KiB
C++
96 lines
2.2 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: TLSVariable.hpp
|
|
Date: 2021-6-11
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
|
|
namespace Aurora::Threading::Threads
|
|
{
|
|
AUKN_SYM bool DeadTest();
|
|
|
|
template<typename T, bool isStatic = false>
|
|
class TLSVariable
|
|
{
|
|
private:
|
|
int _;
|
|
|
|
public:
|
|
TLSVariable() {}
|
|
~TLSVariable()
|
|
{
|
|
if constexpr (!isStatic)
|
|
{
|
|
if (DeadTest())
|
|
{
|
|
return;
|
|
}
|
|
GetThread()->GetTlsView()->Remove(GetHandle());
|
|
}
|
|
}
|
|
|
|
T& operator ->()
|
|
{
|
|
return Get();
|
|
}
|
|
|
|
operator T&()
|
|
{
|
|
return Get();
|
|
}
|
|
|
|
T operator *()
|
|
{
|
|
return Get();
|
|
}
|
|
|
|
TLSVariable& operator =(const T & val)
|
|
{
|
|
Get() = val;
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
|
|
AuUInt64 GetHandle()
|
|
{
|
|
if constexpr (isStatic)
|
|
{
|
|
return (AuUInt64(reinterpret_cast<AuUInt>(&_)) & (~kTlsKeyMask)) | kTlsKeyFollowsConvention | kTlsKeyStaticPointerHandle;
|
|
}
|
|
else
|
|
{
|
|
return (AuUInt64(reinterpret_cast<AuUInt>(&_)) & (~kTlsKeyMask)) | kTlsKeyFollowsConvention | kTlsKeyResettablePointerHandle;
|
|
}
|
|
}
|
|
|
|
T &Get()
|
|
{
|
|
auto view = GetThread()->GetTlsView();
|
|
auto ptr = view->GetOrSetup(GetHandle(),
|
|
sizeof(T),
|
|
[](void *buffer) -> void
|
|
{
|
|
if constexpr (AuIsClass_v<T>)
|
|
{
|
|
new (buffer) T();
|
|
}
|
|
else
|
|
{
|
|
//std::memset(buffer, 0, sizeof(T));
|
|
}
|
|
},
|
|
[](void *buffer) -> void
|
|
{
|
|
if constexpr (AuIsClass_v<T>)
|
|
{
|
|
reinterpret_cast<T *>(buffer)->~T();
|
|
}
|
|
});
|
|
return *reinterpret_cast<T *>(ptr);
|
|
}
|
|
|
|
};
|
|
} |