Remove DWORD from shared TLS keys

This commit is contained in:
notmatthewbeshay 2026-03-14 06:28:46 +11:00
parent f4d8815285
commit 2180aaa4bc
12 changed files with 74 additions and 56 deletions

View file

@ -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

View file

@ -1,5 +1,6 @@
#pragma once
#include "Tile.h"
#include <cstdint>
#if !defined(_WIN32)
#include <pthread.h>
@ -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);

View file

@ -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

View file

@ -1,5 +1,6 @@
#pragma once
#include "TileEntities/EntityTile.h"
#include <cstdint>
#if !defined(_WIN32)
#include <pthread.h>
@ -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);

View file

@ -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

View file

@ -3,6 +3,7 @@
#include "../Util/Vec3.h"
#include "../Util/Definitions.h"
#include "../Util/SoundTypes.h"
#include <cstdint>
#if !defined(_WIN32)
#include <pthread.h>
#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

View file

@ -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

View file

@ -5,6 +5,7 @@
#include "../IO/NBT/FloatTag.h"
#include "../Util/Vec3.h"
#include "../Util/Definitions.h"
#include <cstdint>
#if !defined(_WIN32)
#include <pthread.h>
#endif
@ -38,6 +39,12 @@ class Entity : public std::enable_shared_from_this<Entity>
{
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();

View file

@ -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.

View file

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

View file

@ -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;

View file

@ -4,6 +4,7 @@
#include "../../IO/Files/File.h"
#include "../../IO/NBT/CompoundTag.h"
#include "../../Headers/com.mojang.nbt.h"
#include <cstdint>
#if !defined(_WIN32)
#include <pthread.h>
#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