diff --git a/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp b/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp index 968bbe9be..290b4626e 100644 --- a/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp +++ b/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp @@ -5,7 +5,6 @@ #include // #include -#include #if defined(__linux__) && defined(__GLIBC__) #include #include @@ -710,15 +709,6 @@ void CleanupDevice() { } #endif -int StartMinecraftThreadProc(void* lpParameter) { - AABB::UseDefaultThreadStorage(); - Tesselator::CreateNewThreadStorage(1024 * 1024); - RenderManager.InitialiseContext(); - Minecraft::start(std::wstring(), std::wstring()); - delete Tesselator::getInstance(); - return 0; -} - int main(int argc, const char* argv[]) { #if defined(__linux__) && defined(__GLIBC__) struct sigaction sa; @@ -1197,7 +1187,7 @@ void FreeRichPresenceStrings() { vRichPresenceStrings.clear(); } -#ifdef MEMORY_TRACKING +#if 0 // #ifdef MEMORY_TRACKING int totalAllocGen = 0; std::unordered_map allocCounts; diff --git a/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h b/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h index 5aea5dc23..ca2741f9c 100644 --- a/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h +++ b/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h @@ -306,28 +306,6 @@ static inline ULONG TryEnterCriticalSection( return pthread_mutex_trylock(CriticalSection) == 0; } -// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsalloc -static inline DWORD TlsAlloc(VOID) { - pthread_key_t key; - if (pthread_key_create(&key, NULL) == 0) return key; - return TLS_OUT_OF_INDEXES; -} - -// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsfree -static inline BOOL TlsFree(DWORD dwTlsIndex) { - return pthread_key_delete(dwTlsIndex) == 0; -} - -// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsgetvalue -static inline LPVOID TlsGetValue(DWORD dwTlsIndex) { - return pthread_getspecific(dwTlsIndex); -} - -// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlssetvalue -static inline BOOL TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue) { - return pthread_setspecific(dwTlsIndex, lpTlsValue) == 0; -} - // https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalmemorystatus static inline VOID GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer) { // TODO: Parse /proc/meminfo and set lpBuffer based on that. Probably will diff --git a/Minecraft.World/Util/AABB.cpp b/Minecraft.World/Util/AABB.cpp index e9252864c..2cb797e38 100644 --- a/Minecraft.World/Util/AABB.cpp +++ b/Minecraft.World/Util/AABB.cpp @@ -9,8 +9,8 @@ #include "HitResult.h" #include "Util/Vec3.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[] @@ -23,21 +23,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 = tls; } -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, @@ -51,7 +48,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