From b2942ea9d8b29919fc6a9d420a02888784827a92 Mon Sep 17 00:00:00 2001 From: notmatthewbeshay <92357869+NotMachow@users.noreply.github.com> Date: Wed, 11 Mar 2026 07:36:13 +1100 Subject: [PATCH] Use platform TLS keys in level state --- Minecraft.World/Level/Level.cpp | 48 +++++++++++++++++++++++++++++---- Minecraft.World/Level/Level.h | 8 ++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/Minecraft.World/Level/Level.cpp b/Minecraft.World/Level/Level.cpp index c09680f92..ba550a66d 100644 --- a/Minecraft.World/Level/Level.cpp +++ b/Minecraft.World/Level/Level.cpp @@ -41,10 +41,48 @@ #include "../../Minecraft.Client/Textures/Packs/DLCTexturePack.h" #include "../../Minecraft.Client/Platform/Common/DLC/DLCPack.h" #include "../../Minecraft.Client/Platform/PS3/PS3Extras/ShutdownManager.h" +#include +namespace +{ +#if defined(_WIN32) + inline void *LevelTlsGetValue(DWORD key) + { + return TlsGetValue(key); + } + + inline void LevelTlsSetValue(DWORD key, void *value) + { + TlsSetValue(key, value); + } +#else + pthread_key_t CreateLevelTlsKey() + { + pthread_key_t key; + pthread_key_create(&key, NULL); + return key; + } + + inline void *LevelTlsGetValue(pthread_key_t key) + { + return pthread_getspecific(key); + } + + inline void LevelTlsSetValue(pthread_key_t key, void *value) + { + pthread_setspecific(key, value); + } +#endif +} + +#if defined(_WIN32) DWORD Level::tlsIdx = TlsAlloc(); DWORD Level::tlsIdxLightCache = TlsAlloc(); +#else +pthread_key_t Level::tlsIdx = CreateLevelTlsKey(); +pthread_key_t Level::tlsIdxLightCache = CreateLevelTlsKey(); +#endif // 4J : WESTY : Added for time played stats. #include "../Headers/net.minecraft.stats.h" @@ -94,12 +132,12 @@ void Level::enableLightingCache() { // Allocate 16K (needs 32K for large worlds) for a 16x16x16x4 byte cache of results, plus 128K required for toCheck array. Rounding up to 256 to keep as multiple of alignement - aligning to 128K boundary for possible cache locking. void *cache = (unsigned char *)XPhysicalAlloc(256 * 1024, MAXULONG_PTR, 128 * 1024, PAGE_READWRITE | MEM_LARGE_PAGES); - TlsSetValue(tlsIdxLightCache,cache); + LevelTlsSetValue(tlsIdxLightCache, cache); } void Level::destroyLightingCache() { - lightCache_t *cache = (lightCache_t *)TlsGetValue(tlsIdxLightCache); + lightCache_t *cache = static_cast(LevelTlsGetValue(tlsIdxLightCache)); XPhysicalFree(cache); } @@ -458,14 +496,14 @@ void Level::flushCache(lightCache_t *cache, __uint64 cacheUse, LightLayer::varie // 4J - added following 2 functions to move instaBuild flag from being a class member, to TLS bool Level::getInstaTick() { - return ((size_t)TlsGetValue(tlsIdx)) != 0; + return reinterpret_cast(LevelTlsGetValue(tlsIdx)) != 0; } void Level::setInstaTick(bool enable) { void *value = 0; if( enable ) value = (void *)1; - TlsSetValue(tlsIdx,value); + LevelTlsSetValue(tlsIdx, value); } // 4J - added @@ -3386,7 +3424,7 @@ inline int GetIndex(int x, int y, int z) // 4J - Made changes here so that lighting goes through a cache, if enabled for this thread void Level::checkLight(LightLayer::variety layer, int xc, int yc, int zc, bool force, bool rootOnlyEmissive) { - lightCache_t *cache = (lightCache_t *)TlsGetValue(tlsIdxLightCache); + lightCache_t *cache = static_cast(LevelTlsGetValue(tlsIdxLightCache)); __uint64 cacheUse = 0; if( force ) diff --git a/Minecraft.World/Level/Level.h b/Minecraft.World/Level/Level.h index 945341bfa..f6b60ec6f 100644 --- a/Minecraft.World/Level/Level.h +++ b/Minecraft.World/Level/Level.h @@ -9,6 +9,9 @@ #include "../Util/ParticleTypes.h" #include "../WorldGen/Biomes/Biome.h" #include "../Util/C4JThread.h" +#if !defined(_WIN32) +#include +#endif #ifdef __PSVITA__ #include "../../Minecraft.Client/Platform/PSVita/PSVitaExtras/CustomSet.h" @@ -77,8 +80,13 @@ public: int seaLevel; // 4J - added, making instaTick flag use TLS so we can set it in the chunk rebuilding thread without upsetting the main game thread +#if defined(_WIN32) static DWORD tlsIdx; static DWORD tlsIdxLightCache; +#else + static pthread_key_t tlsIdx; + static pthread_key_t tlsIdxLightCache; +#endif static void enableLightingCache(); static void destroyLightingCache(); static bool getCacheTestEnabled();