Use platform TLS keys in level state

This commit is contained in:
notmatthewbeshay 2026-03-11 07:36:13 +11:00
parent c2b3537c62
commit b2942ea9d8
2 changed files with 51 additions and 5 deletions

View file

@ -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 <cstdint>
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<lightCache_t *>(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<std::intptr_t>(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<lightCache_t *>(LevelTlsGetValue(tlsIdxLightCache));
__uint64 cacheUse = 0;
if( force )

View file

@ -9,6 +9,9 @@
#include "../Util/ParticleTypes.h"
#include "../WorldGen/Biomes/Biome.h"
#include "../Util/C4JThread.h"
#if !defined(_WIN32)
#include <pthread.h>
#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();