AuroraRuntime/Include/Aurora/Threading/Threads/TLSVariable.hpp

96 lines
2.2 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
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
{
2022-04-02 00:48:29 +00:00
AUKN_SYM bool DeadTest();
2021-06-30 09:28:52 +00:00
template<typename T, bool isStatic = false>
2021-06-27 21:25:29 +00:00
class TLSVariable
{
private:
int _;
public:
TLSVariable() {}
~TLSVariable()
{
2021-06-30 09:28:52 +00:00
if constexpr (!isStatic)
{
2022-04-02 00:48:29 +00:00
if (DeadTest())
{
return;
}
2021-06-30 09:28:52 +00:00
GetThread()->GetTlsView()->Remove(GetHandle());
}
}
T& operator ->()
{
return Get();
}
operator T&()
{
return Get();
}
T operator *()
{
return Get();
}
2021-06-30 09:28:52 +00:00
TLSVariable& operator =(const T & val)
{
Get() = val;
return *this;
2021-06-27 21:25:29 +00:00
}
2021-06-30 09:28:52 +00:00
private:
2021-06-27 21:25:29 +00:00
AuUInt64 GetHandle()
{
2021-06-30 09:28:52 +00:00
if constexpr (isStatic)
{
return (AuUInt64(reinterpret_cast<AuUInt>(&_)) & (~kTlsKeyMask)) | kTlsKeyFollowsConvention | kTlsKeyStaticPointerHandle;
}
else
{
return (AuUInt64(reinterpret_cast<AuUInt>(&_)) & (~kTlsKeyMask)) | kTlsKeyFollowsConvention | kTlsKeyResettablePointerHandle;
}
2021-06-27 21:25:29 +00:00
}
T &Get()
{
auto view = GetThread()->GetTlsView();
2021-06-30 09:28:52 +00:00
auto ptr = view->GetOrSetup(GetHandle(),
sizeof(T),
2021-06-27 21:25:29 +00:00
[](void *buffer) -> void
{
2022-01-19 17:08:13 +00:00
if constexpr (AuIsClass_v<T>)
2021-06-27 21:25:29 +00:00
{
new (buffer) T();
}
else
{
//std::memset(buffer, 0, sizeof(T));
}
},
[](void *buffer) -> void
{
2022-01-19 17:08:13 +00:00
if constexpr (AuIsClass_v<T>)
2021-06-27 21:25:29 +00:00
{
reinterpret_cast<T *>(buffer)->~T();
}
});
return *reinterpret_cast<T *>(ptr);
}
2021-06-30 09:28:52 +00:00
2021-06-27 21:25:29 +00:00
};
}