diff --git a/Minecraft.World/Util/AABB.cpp b/Minecraft.World/Util/AABB.cpp index c4a8bff2e..3dff9c23d 100644 --- a/Minecraft.World/Util/AABB.cpp +++ b/Minecraft.World/Util/AABB.cpp @@ -7,8 +7,8 @@ #include "AABB.h" #include "HitResult.h" -unsigned int AABB::tlsIdx = 0; -AABB::ThreadStorage* AABB::tlsDefault = NULL; +thread_local AABB::ThreadStorage* AABB::m_tlsPool = nullptr; +AABB::ThreadStorage* AABB::m_tlsPoolDefault = nullptr; AABB::ThreadStorage::ThreadStorage() { pool = new AABB[POOL_SIZE]; // 4jcraft, needs to be deleted with delete[] @@ -21,21 +21,18 @@ AABB::ThreadStorage::~ThreadStorage() { void AABB::CreateNewThreadStorage() { ThreadStorage* tls = new ThreadStorage(); - if (tlsDefault == NULL) { - tlsIdx = TlsAlloc(); - tlsDefault = tls; + if (m_tlsPoolDefault == nullptr) { + m_tlsPoolDefault = tls; } - - TlsSetValue(tlsIdx, tls); + m_tlsPool = m_tlsPoolDefault; } -void AABB::UseDefaultThreadStorage() { TlsSetValue(tlsIdx, tlsDefault); } +void AABB::UseDefaultThreadStorage() { m_tlsPool = m_tlsPoolDefault; } void AABB::ReleaseThreadStorage() { - ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx); - if (tls == tlsDefault) return; - - delete tls; + if (m_tlsPool != m_tlsPoolDefault) { + delete m_tlsPool; + } } AABB* AABB::newPermanent(double x0, double y0, double z0, double x1, double y1, @@ -49,7 +46,7 @@ void AABB::resetPool() {} AABB* AABB::newTemp(double x0, double y0, double z0, double x1, double y1, double z1) { - ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx); + ThreadStorage* tls = m_tlsPool; AABB* thisAABB = &tls->pool[tls->poolPointer]; thisAABB->set(x0, y0, z0, x1, y1, z1); tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE; diff --git a/Minecraft.World/Util/AABB.h b/Minecraft.World/Util/AABB.h index bcd941f39..205d5ac98 100644 --- a/Minecraft.World/Util/AABB.h +++ b/Minecraft.World/Util/AABB.h @@ -15,8 +15,8 @@ class AABB { ThreadStorage(); ~ThreadStorage(); }; - static unsigned int tlsIdx; - static ThreadStorage* tlsDefault; + static thread_local ThreadStorage* m_tlsPool; + static ThreadStorage* m_tlsPoolDefault; public: // Each new thread that needs to use Vec3 pools will need to call one of the diff --git a/Minecraft.World/Util/Vec3.cpp b/Minecraft.World/Util/Vec3.cpp index 4e42b99a3..7796aba16 100644 --- a/Minecraft.World/Util/Vec3.cpp +++ b/Minecraft.World/Util/Vec3.cpp @@ -2,8 +2,8 @@ #include "Vec3.h" #include "AABB.h" -unsigned int Vec3::tlsIdx = 0; -Vec3::ThreadStorage* Vec3::tlsDefault = NULL; +thread_local Vec3::ThreadStorage* Vec3::m_tlsPool = nullptr; +Vec3::ThreadStorage* Vec3::m_tlsPoolDefault = nullptr; Vec3::ThreadStorage::ThreadStorage() { pool = new Vec3[POOL_SIZE]; @@ -14,20 +14,18 @@ Vec3::ThreadStorage::~ThreadStorage() { delete[] pool; } void Vec3::CreateNewThreadStorage() { ThreadStorage* tls = new ThreadStorage(); - if (tlsDefault == NULL) { - tlsIdx = TlsAlloc(); - tlsDefault = tls; + if (m_tlsPoolDefault == nullptr) { + m_tlsPoolDefault = tls; } - TlsSetValue(tlsIdx, tls); + m_tlsPool = m_tlsPoolDefault; } -void Vec3::UseDefaultThreadStorage() { TlsSetValue(tlsIdx, tlsDefault); } +void Vec3::UseDefaultThreadStorage() { m_tlsPool = m_tlsPoolDefault; } void Vec3::ReleaseThreadStorage() { - ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx); - if (tls == tlsDefault) return; - - delete tls; + if (m_tlsPool != m_tlsPoolDefault) { + delete m_tlsPool; + } } Vec3* Vec3::newPermanent(double x, double y, double z) { @@ -39,7 +37,7 @@ void Vec3::clearPool() {} void Vec3::resetPool() {} Vec3* Vec3::newTemp(double x, double y, double z) { - ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx); + ThreadStorage* tls = m_tlsPool; Vec3* thisVec = &tls->pool[tls->poolPointer]; thisVec->set(x, y, z); tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE; diff --git a/Minecraft.World/Util/Vec3.h b/Minecraft.World/Util/Vec3.h index bc1239d81..da9fcc73b 100644 --- a/Minecraft.World/Util/Vec3.h +++ b/Minecraft.World/Util/Vec3.h @@ -12,8 +12,9 @@ class Vec3 { ThreadStorage(); ~ThreadStorage(); }; - static unsigned int tlsIdx; - static ThreadStorage* tlsDefault; + + static thread_local ThreadStorage* m_tlsPool; + static ThreadStorage* m_tlsPoolDefault; public: // Each new thread that needs to use Vec3 pools will need to call one of the