From 2180aaa4bcc21883e0e00c5d87169d348e7d6a6a Mon Sep 17 00:00:00 2001 From: notmatthewbeshay <92357869+NotMachow@users.noreply.github.com> Date: Sat, 14 Mar 2026 06:28:46 +1100 Subject: [PATCH] Remove DWORD from shared TLS keys --- Minecraft.World/Blocks/PistonBaseTile.cpp | 8 ++++---- Minecraft.World/Blocks/PistonBaseTile.h | 13 ++++++++----- Minecraft.World/Blocks/TheEndPortalTile.cpp | 8 ++++---- Minecraft.World/Blocks/TheEndPortalTile.h | 7 +++++-- Minecraft.World/Blocks/Tile.cpp | 9 ++++----- Minecraft.World/Blocks/Tile.h | 14 +++++++++----- Minecraft.World/Entities/Entity.cpp | 8 ++++---- Minecraft.World/Entities/Entity.h | 13 ++++++++----- Minecraft.World/Level/Level.cpp | 12 ++++++------ Minecraft.World/Level/Level.h | 16 +++++++++------- .../Level/Storage/OldChunkStorage.cpp | 8 ++++---- Minecraft.World/Level/Storage/OldChunkStorage.h | 14 +++++++++----- 12 files changed, 74 insertions(+), 56 deletions(-) diff --git a/Minecraft.World/Blocks/PistonBaseTile.cpp b/Minecraft.World/Blocks/PistonBaseTile.cpp index 1aa424c43..d64bc06ae 100644 --- a/Minecraft.World/Blocks/PistonBaseTile.cpp +++ b/Minecraft.World/Blocks/PistonBaseTile.cpp @@ -23,12 +23,12 @@ const float PistonBaseTile::PLATFORM_THICKNESS = 4.0f; namespace { #if defined(_WIN32) - inline void *PistonTlsGetValue(DWORD key) + inline void *PistonTlsGetValue(PistonBaseTile::TlsKey key) { return TlsGetValue(key); } - inline void PistonTlsSetValue(DWORD key, void *value) + inline void PistonTlsSetValue(PistonBaseTile::TlsKey key, void *value) { TlsSetValue(key, value); } @@ -53,9 +53,9 @@ namespace } #if defined(_WIN32) -DWORD PistonBaseTile::tlsIdx = TlsAlloc(); +PistonBaseTile::TlsKey PistonBaseTile::tlsIdx = TlsAlloc(); #else -pthread_key_t PistonBaseTile::tlsIdx = CreatePistonTlsKey(); +PistonBaseTile::TlsKey PistonBaseTile::tlsIdx = CreatePistonTlsKey(); #endif // 4J - NOTE - this ignoreUpdate stuff has been removed from the java version, but I'm not currently sure how the java version does without it... there must be diff --git a/Minecraft.World/Blocks/PistonBaseTile.h b/Minecraft.World/Blocks/PistonBaseTile.h index 4e844a7e2..37841a5bd 100644 --- a/Minecraft.World/Blocks/PistonBaseTile.h +++ b/Minecraft.World/Blocks/PistonBaseTile.h @@ -1,5 +1,6 @@ #pragma once #include "Tile.h" +#include #if !defined(_WIN32) #include @@ -8,6 +9,12 @@ class PistonBaseTile : public Tile { public: +#if defined(_WIN32) + using TlsKey = std::uint32_t; +#else + using TlsKey = pthread_key_t; +#endif + static const int EXTENDED_BIT = 8; static const int UNDEFINED_FACING = 7; @@ -29,11 +36,7 @@ private: Icon *iconBack; Icon *iconPlatform; -#if defined(_WIN32) - static DWORD tlsIdx; -#else - static pthread_key_t tlsIdx; -#endif + static TlsKey tlsIdx; // 4J - was just a static but implemented with TLS for our version static bool ignoreUpdate(); static void ignoreUpdate(bool set); diff --git a/Minecraft.World/Blocks/TheEndPortalTile.cpp b/Minecraft.World/Blocks/TheEndPortalTile.cpp index a2fbaf308..8d43bb1eb 100644 --- a/Minecraft.World/Blocks/TheEndPortalTile.cpp +++ b/Minecraft.World/Blocks/TheEndPortalTile.cpp @@ -12,12 +12,12 @@ namespace { #if defined(_WIN32) - inline void *TheEndPortalTlsGetValue(DWORD key) + inline void *TheEndPortalTlsGetValue(TheEndPortal::TlsKey key) { return TlsGetValue(key); } - inline void TheEndPortalTlsSetValue(DWORD key, void *value) + inline void TheEndPortalTlsSetValue(TheEndPortal::TlsKey key, void *value) { TlsSetValue(key, value); } @@ -42,9 +42,9 @@ namespace } #if defined(_WIN32) -DWORD TheEndPortal::tlsIdx = TlsAlloc(); +TheEndPortal::TlsKey TheEndPortal::tlsIdx = TlsAlloc(); #else -pthread_key_t TheEndPortal::tlsIdx = CreateTheEndPortalTlsKey(); +TheEndPortal::TlsKey TheEndPortal::tlsIdx = CreateTheEndPortalTlsKey(); #endif // 4J - allowAnywhere is a static in java, implementing as TLS here to make thread safe diff --git a/Minecraft.World/Blocks/TheEndPortalTile.h b/Minecraft.World/Blocks/TheEndPortalTile.h index 130a4232b..762f1299f 100644 --- a/Minecraft.World/Blocks/TheEndPortalTile.h +++ b/Minecraft.World/Blocks/TheEndPortalTile.h @@ -1,5 +1,6 @@ #pragma once #include "TileEntities/EntityTile.h" +#include #if !defined(_WIN32) #include @@ -9,10 +10,12 @@ class TheEndPortal : public EntityTile { public: #if defined(_WIN32) - static DWORD tlsIdx; + using TlsKey = std::uint32_t; #else - static pthread_key_t tlsIdx; + using TlsKey = pthread_key_t; #endif + + static TlsKey tlsIdx; // 4J - was just a static but implemented with TLS for our version static bool allowAnywhere(); static void allowAnywhere(bool set); diff --git a/Minecraft.World/Blocks/Tile.cpp b/Minecraft.World/Blocks/Tile.cpp index 209b76272..77d06c68e 100644 --- a/Minecraft.World/Blocks/Tile.cpp +++ b/Minecraft.World/Blocks/Tile.cpp @@ -19,12 +19,12 @@ namespace { #if defined(_WIN32) - inline void *TileTlsGetValue(DWORD key) + inline void *TileTlsGetValue(Tile::TlsKey key) { return TlsGetValue(key); } - inline void TileTlsSetValue(DWORD key, void *value) + inline void TileTlsSetValue(Tile::TlsKey key, void *value) { TlsSetValue(key, value); } @@ -235,9 +235,9 @@ Tile *Tile::stairs_quartz = NULL; Tile *Tile::woolCarpet = NULL; #if defined(_WIN32) -DWORD Tile::tlsIdxShape = TlsAlloc(); +Tile::TlsKey Tile::tlsIdxShape = TlsAlloc(); #else -pthread_key_t Tile::tlsIdxShape = CreateTileTlsKey(); +Tile::TlsKey Tile::tlsIdxShape = CreateTileTlsKey(); #endif Tile::ThreadStorage::ThreadStorage() @@ -1667,4 +1667,3 @@ const int Tile::quartzBlock_Id; const int Tile::stairs_quartz_Id; const int Tile::woolCarpet_Id; #endif - diff --git a/Minecraft.World/Blocks/Tile.h b/Minecraft.World/Blocks/Tile.h index ea4421fc0..fc757c61c 100644 --- a/Minecraft.World/Blocks/Tile.h +++ b/Minecraft.World/Blocks/Tile.h @@ -3,6 +3,7 @@ #include "../Util/Vec3.h" #include "../Util/Definitions.h" #include "../Util/SoundTypes.h" +#include #if !defined(_WIN32) #include #endif @@ -49,6 +50,13 @@ class Tile friend class ChunkRebuildData; friend class WallTile; +public: +#if defined(_WIN32) + using TlsKey = std::uint32_t; +#else + using TlsKey = pthread_key_t; +#endif + protected: // 4J added so we can have separate shapes for different threads class ThreadStorage @@ -58,11 +66,7 @@ protected: int tileId; ThreadStorage(); }; -#if defined(_WIN32) - static DWORD tlsIdxShape; -#else - static pthread_key_t tlsIdxShape; -#endif + static TlsKey tlsIdxShape; public: // Each new thread that needs to use Vec3 pools will need to call one of the following 2 functions, to either create its own // local storage, or share the default storage already allocated by the main thread diff --git a/Minecraft.World/Entities/Entity.cpp b/Minecraft.World/Entities/Entity.cpp index 1a9a927da..3f4b77f17 100644 --- a/Minecraft.World/Entities/Entity.cpp +++ b/Minecraft.World/Entities/Entity.cpp @@ -26,12 +26,12 @@ namespace { #if defined(_WIN32) - inline void *EntityTlsGetValue(DWORD key) + inline void *EntityTlsGetValue(Entity::TlsKey key) { return TlsGetValue(key); } - inline void EntityTlsSetValue(DWORD key, void *value) + inline void EntityTlsSetValue(Entity::TlsKey key, void *value) { TlsSetValue(key, value); } @@ -59,9 +59,9 @@ namespace int Entity::entityCounter = 2048; // 4J - changed initialiser to 2048, as we are using range 0 - 2047 as special unique smaller ids for things that need network tracked #if defined(_WIN32) -DWORD Entity::tlsIdx = TlsAlloc(); +Entity::TlsKey Entity::tlsIdx = TlsAlloc(); #else -pthread_key_t Entity::tlsIdx = CreateEntityTlsKey(); +Entity::TlsKey Entity::tlsIdx = CreateEntityTlsKey(); #endif // 4J - added getSmallId & freeSmallId methods diff --git a/Minecraft.World/Entities/Entity.h b/Minecraft.World/Entities/Entity.h index 10cc1553c..c62a17391 100644 --- a/Minecraft.World/Entities/Entity.h +++ b/Minecraft.World/Entities/Entity.h @@ -5,6 +5,7 @@ #include "../IO/NBT/FloatTag.h" #include "../Util/Vec3.h" #include "../Util/Definitions.h" +#include #if !defined(_WIN32) #include #endif @@ -38,6 +39,12 @@ class Entity : public std::enable_shared_from_this { friend class Gui; // 4J Stu - Added to be able to access the shared flag functions and constants, without making them publicly available to everything public: +#if defined(_WIN32) + using TlsKey = std::uint32_t; +#else + using TlsKey = pthread_key_t; +#endif + // 4J-PB - added to replace (e instanceof Type), avoiding dynamic casts virtual eINSTANCEOF GetType() = 0; @@ -368,11 +375,7 @@ private: static int extraWanderIds[EXTRA_WANDER_MAX]; static int extraWanderCount; static int extraWanderTicks; -#if defined(_WIN32) - static DWORD tlsIdx; -#else - static pthread_key_t tlsIdx; -#endif + static TlsKey tlsIdx; public: static void tickExtraWandering(); static void countFlagsForPIX(); diff --git a/Minecraft.World/Level/Level.cpp b/Minecraft.World/Level/Level.cpp index b4026d890..2ebe42e00 100644 --- a/Minecraft.World/Level/Level.cpp +++ b/Minecraft.World/Level/Level.cpp @@ -47,12 +47,12 @@ namespace { #if defined(_WIN32) - inline void *LevelTlsGetValue(DWORD key) + inline void *LevelTlsGetValue(Level::TlsKey key) { return TlsGetValue(key); } - inline void LevelTlsSetValue(DWORD key, void *value) + inline void LevelTlsSetValue(Level::TlsKey key, void *value) { TlsSetValue(key, value); } @@ -77,11 +77,11 @@ namespace } #if defined(_WIN32) -DWORD Level::tlsIdx = TlsAlloc(); -DWORD Level::tlsIdxLightCache = TlsAlloc(); +Level::TlsKey Level::tlsIdx = TlsAlloc(); +Level::TlsKey Level::tlsIdxLightCache = TlsAlloc(); #else -pthread_key_t Level::tlsIdx = CreateLevelTlsKey(); -pthread_key_t Level::tlsIdxLightCache = CreateLevelTlsKey(); +Level::TlsKey Level::tlsIdx = CreateLevelTlsKey(); +Level::TlsKey Level::tlsIdxLightCache = CreateLevelTlsKey(); #endif // 4J : WESTY : Added for time played stats. diff --git a/Minecraft.World/Level/Level.h b/Minecraft.World/Level/Level.h index f6b60ec6f..4f77a9ff4 100644 --- a/Minecraft.World/Level/Level.h +++ b/Minecraft.World/Level/Level.h @@ -9,6 +9,7 @@ #include "../Util/ParticleTypes.h" #include "../WorldGen/Biomes/Biome.h" #include "../Util/C4JThread.h" +#include #if !defined(_WIN32) #include #endif @@ -48,6 +49,12 @@ class VillageSiege; class Level : public LevelSource { public: +#if defined(_WIN32) + using TlsKey = std::uint32_t; +#else + using TlsKey = pthread_key_t; +#endif + static const int MAX_TICK_TILES_PER_TICK = 1000; // 4J Added @@ -80,13 +87,8 @@ 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 TlsKey tlsIdx; + static TlsKey tlsIdxLightCache; static void enableLightingCache(); static void destroyLightingCache(); static bool getCacheTestEnabled(); diff --git a/Minecraft.World/Level/Storage/OldChunkStorage.cpp b/Minecraft.World/Level/Storage/OldChunkStorage.cpp index 14523a624..c8eae025a 100644 --- a/Minecraft.World/Level/Storage/OldChunkStorage.cpp +++ b/Minecraft.World/Level/Storage/OldChunkStorage.cpp @@ -11,18 +11,18 @@ #if defined(_WIN32) namespace { - inline void *OldChunkStorageTlsGetValue(DWORD key) + inline void *OldChunkStorageTlsGetValue(OldChunkStorage::TlsKey key) { return TlsGetValue(key); } - inline void OldChunkStorageTlsSetValue(DWORD key, void *value) + inline void OldChunkStorageTlsSetValue(OldChunkStorage::TlsKey key, void *value) { TlsSetValue(key, value); } } -DWORD OldChunkStorage::tlsIdx = TlsAlloc(); +OldChunkStorage::TlsKey OldChunkStorage::tlsIdx = TlsAlloc(); #else namespace { @@ -45,7 +45,7 @@ namespace } } -pthread_key_t OldChunkStorage::tlsIdx = CreateOldChunkStorageTlsKey(); +OldChunkStorage::TlsKey OldChunkStorage::tlsIdx = CreateOldChunkStorageTlsKey(); #endif OldChunkStorage::ThreadStorage *OldChunkStorage::tlsDefault = NULL; diff --git a/Minecraft.World/Level/Storage/OldChunkStorage.h b/Minecraft.World/Level/Storage/OldChunkStorage.h index 34d100720..58b298142 100644 --- a/Minecraft.World/Level/Storage/OldChunkStorage.h +++ b/Minecraft.World/Level/Storage/OldChunkStorage.h @@ -4,6 +4,7 @@ #include "../../IO/Files/File.h" #include "../../IO/NBT/CompoundTag.h" #include "../../Headers/com.mojang.nbt.h" +#include #if !defined(_WIN32) #include #endif @@ -12,6 +13,13 @@ class Level; class OldChunkStorage : public ChunkStorage { +public: +#if defined(_WIN32) + using TlsKey = std::uint32_t; +#else + using TlsKey = pthread_key_t; +#endif + private: // 4J added so we can have separate storage arrays for different threads class ThreadStorage @@ -25,11 +33,7 @@ private: ThreadStorage(); ~ThreadStorage(); }; -#if defined(_WIN32) - static DWORD tlsIdx; -#else - static pthread_key_t tlsIdx; -#endif + static TlsKey tlsIdx; static ThreadStorage *tlsDefault; public: // Each new thread that needs to use Compression will need to call one of the following 2 functions, to either create its own