refactor: convert Tile::tlsIdxShape to thread_local

This commit is contained in:
Tropical 2026-03-25 14:20:08 -05:00
parent 625ce97385
commit 4a1fb94600
11 changed files with 30 additions and 80 deletions

View file

@ -260,8 +260,7 @@ void ButtonTile::checkPressed(Level* level, int x, int y, int z) {
bool shouldBePressed;
updateShape(data);
Tile::ThreadStorage* tls =
(Tile::ThreadStorage*)TlsGetValue(Tile::tlsIdxShape);
Tile::ThreadStorage* tls = m_threadShape;
std::vector<std::shared_ptr<Entity> >* entities = level->getEntitiesOfClass(
typeid(Arrow), AABB::newTemp(x + tls->xx0, y + tls->yy0, z + tls->zz0,
x + tls->xx1, y + tls->yy1, z + tls->zz1));

View file

@ -13,8 +13,7 @@ const std::wstring LiquidTile::TEXTURE_WATER_STILL = L"water";
const std::wstring LiquidTile::TEXTURE_WATER_FLOW = L"water_flow";
const std::wstring LiquidTile::TEXTURE_LAVA_FLOW = L"lava_flow";
LiquidTile::LiquidTile(int id, Material* material)
: Tile(id, material, false) {
LiquidTile::LiquidTile(int id, Material* material) : Tile(id, material, false) {
float yo = 0;
float e = 0;
@ -267,8 +266,7 @@ void LiquidTile::animateTick(Level* level, int x, int y, int z,
if (level->getMaterial(x, y + 1, z) == Material::air &&
!level->isSolidRenderTile(x, y + 1, z)) {
if (random->nextInt(100) == 0) {
ThreadStorage* tls =
(ThreadStorage*)TlsGetValue(Tile::tlsIdxShape);
ThreadStorage* tls = m_threadShape;
double xx = x + random->nextFloat();
double yy = y + tls->yy1;
double zz = z + random->nextFloat();

View file

@ -103,7 +103,7 @@ Icon* PistonBaseTile::getTexture(int face, int data) {
// when the piston is extended, either normally
// or because a piston arm animation, the top
// texture is the furnace bottom
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(Tile::tlsIdxShape);
ThreadStorage* tls = m_threadShape;
if (isExtended(data) || tls->xx0 > 0 || tls->yy0 > 0 || tls->zz0 > 0 ||
tls->xx1 < 1 || tls->yy1 < 1 || tls->zz1 < 1) {
return iconInside;

View file

@ -126,7 +126,7 @@ void PistonMovingPiece::updateShape(
progress = 1.0f - progress;
}
int facing = entity->getFacing();
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(Tile::tlsIdxShape);
ThreadStorage* tls = m_threadShape;
tls->xx0 = tile->getShapeX0() - Facing::STEP_X[facing] * progress;
tls->yy0 = tile->getShapeY0() - Facing::STEP_Y[facing] * progress;
tls->zz0 = tile->getShapeZ0() - Facing::STEP_Z[facing] * progress;

View file

@ -145,7 +145,7 @@ void StemTile::updateShape(
std::shared_ptr<TileEntity>
forceEntity) // 4J added forceData, forceEntity param
{
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(Tile::tlsIdxShape);
ThreadStorage* tls = m_threadShape;
tls->yy1 = (level->getData(x, y, z) * 2 + 2) / 16.0f;
float ss = 0.125f;
setShape(0.5f - ss, 0, 0.5f - ss, 0.5f + ss, (float)tls->yy1, 0.5f + ss);

View file

@ -17,31 +17,6 @@
#include "../Headers/net.minecraft.h"
#include "Tile.h"
namespace {
#if defined(_WIN32)
inline void* TileTlsGetValue(Tile::TlsKey key) { return TlsGetValue(key); }
inline void TileTlsSetValue(Tile::TlsKey key, void* value) {
TlsSetValue(key, value);
}
#else
pthread_key_t CreateTileTlsKey() {
pthread_key_t key;
const int result = pthread_key_create(&key, nullptr);
assert(result == 0);
return key;
}
inline void* TileTlsGetValue(pthread_key_t key) {
return pthread_getspecific(key);
}
inline void TileTlsSetValue(pthread_key_t key, void* value) {
pthread_setspecific(key, value);
}
#endif
} // namespace
std::wstring Tile::TILE_DESCRIPTION_PREFIX = L"Tile.";
const float Tile::INDESTRUCTIBLE_DESTROY_TIME = -1.0f;
@ -247,11 +222,7 @@ Tile* Tile::woolCarpet = NULL;
Tile* Tile::clayHardened = NULL;
Tile* Tile::coalBlock = NULL;
#if defined(_WIN32)
Tile::TlsKey Tile::tlsIdxShape = TlsAlloc();
#else
Tile::TlsKey Tile::tlsIdxShape = CreateTileTlsKey();
#endif
thread_local Tile::ThreadStorage* Tile::m_threadShape = nullptr;
Tile::ThreadStorage::ThreadStorage() {
xx0 = yy0 = zz0 = xx1 = yy1 = zz1 = 0.0;
@ -259,14 +230,11 @@ Tile::ThreadStorage::ThreadStorage() {
}
void Tile::CreateNewThreadStorage() {
ThreadStorage* tls = new ThreadStorage();
TileTlsSetValue(Tile::tlsIdxShape, tls);
m_threadShape = new ThreadStorage();
}
void Tile::ReleaseThreadStorage() {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
delete tls;
delete m_threadShape;
}
void Tile::staticCtor() {
@ -1900,8 +1868,7 @@ Tile* Tile::disableMipmap() {
void Tile::setShape(float x0, float y0, float z0, float x1, float y1,
float z1) {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
tls->xx0 = x0;
tls->yy0 = y0;
tls->zz0 = z0;
@ -1952,7 +1919,7 @@ bool Tile::isFaceVisible(Level* level, int x, int y, int z, int f) {
bool Tile::shouldRenderFace(LevelSource* level, int x, int y, int z, int face) {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
if (face == 0 && tls->yy0 > 0) return true;
@ -1969,8 +1936,7 @@ bool Tile::shouldRenderFace(LevelSource* level, int x, int y, int z, int face) {
int Tile::getFaceFlags(LevelSource* level, int x, int y, int z) {
int faceFlags = 0;
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
@ -2043,8 +2009,7 @@ Icon* Tile::getTexture(int face, int data) { return icon; }
Icon* Tile::getTexture(int face) { return getTexture(face, 0); }
AABB* Tile::getTileAABB(Level* level, int x, int y, int z) {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return AABB::newTemp(x + tls->xx0, y + tls->yy0, z + tls->zz0, x + tls->xx1,
@ -2058,8 +2023,7 @@ void Tile::addAABBs(Level* level, int x, int y, int z, AABB* box,
}
AABB* Tile::getAABB(Level* level, int x, int y, int z) {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return AABB::newTemp(x + tls->xx0, y + tls->yy0, z + tls->zz0, x + tls->xx1,
@ -2164,8 +2128,7 @@ HitResult* Tile::clip(Level* level, int xt, int yt, int zt, Vec3* a, Vec3* b) {
a = a->add(-xt, -yt, -zt);
b = b->add(-xt, -yt, -zt);
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
Vec3* xh0 = a->clipX(b, tls->xx0);
Vec3* xh1 = a->clipX(b, tls->xx1);
@ -2213,8 +2176,7 @@ HitResult* Tile::clip(Level* level, int xt, int yt, int zt, Vec3* a, Vec3* b) {
bool Tile::containsX(Vec3* v) {
if (v == NULL) return false;
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return v->y >= tls->yy0 && v->y <= tls->yy1 && v->z >= tls->zz0 &&
@ -2224,8 +2186,7 @@ bool Tile::containsX(Vec3* v) {
bool Tile::containsY(Vec3* v) {
if (v == NULL) return false;
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return v->x >= tls->xx0 && v->x <= tls->xx1 && v->z >= tls->zz0 &&
@ -2235,8 +2196,7 @@ bool Tile::containsY(Vec3* v) {
bool Tile::containsZ(Vec3* v) {
if (v == NULL) return false;
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return v->x >= tls->xx0 && v->x <= tls->xx1 && v->y >= tls->yy0 &&
@ -2300,55 +2260,48 @@ void Tile::updateShape(
std::shared_ptr<TileEntity>
forceEntity) // 4J added forceData, forceEntity param
{
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
}
double Tile::getShapeX0() {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return tls->xx0;
}
double Tile::getShapeX1() {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return tls->xx1;
}
double Tile::getShapeY0() {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return tls->yy0;
}
double Tile::getShapeY1() {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return tls->yy1;
}
double Tile::getShapeZ0() {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return tls->zz0;
}
double Tile::getShapeZ1() {
ThreadStorage* tls =
static_cast<ThreadStorage*>(TileTlsGetValue(Tile::tlsIdxShape));
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return tls->zz1;

View file

@ -66,7 +66,7 @@ protected:
int tileId;
ThreadStorage();
};
static TlsKey tlsIdxShape;
static thread_local ThreadStorage* m_threadShape;
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

View file

@ -25,7 +25,7 @@ void TopSnowTile::registerIcons(IconRegister* iconRegister) {
AABB* TopSnowTile::getAABB(Level* level, int x, int y, int z) {
int height = level->getData(x, y, z) & HEIGHT_MASK;
float offset = 2.0f / SharedConstants::WORLD_RESOLUTION;
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(Tile::tlsIdxShape);
ThreadStorage* tls = m_threadShape;
return AABB::newTemp(x + tls->xx0, y + tls->yy0, z + tls->zz0, x + tls->xx1,
y + (height * offset), z + tls->zz1);
}

View file

@ -132,7 +132,7 @@ void TripWireTile::checkPressed(Level* level, int x, int y, int z) {
bool wasPressed = (data & MASK_POWERED) == MASK_POWERED;
bool shouldBePressed = false;
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(Tile::tlsIdxShape);
ThreadStorage* tls = m_threadShape;
std::vector<std::shared_ptr<Entity> >* entities = level->getEntities(
nullptr, AABB::newTemp(x + tls->xx0, y + tls->yy0, z + tls->zz0,
x + tls->xx1, y + tls->yy1, z + tls->zz1));

View file

@ -24,7 +24,7 @@ void WaterlilyTile::addAABBs(Level* level, int x, int y, int z, AABB* box,
}
AABB* WaterlilyTile::getAABB(Level* level, int x, int y, int z) {
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(Tile::tlsIdxShape);
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return AABB::newTemp(x + tls->xx0, y + tls->yy0, z + tls->zz0, x + tls->xx1,

View file

@ -18,7 +18,7 @@ Icon* WoolCarpetTile::getTexture(int face, int data) {
AABB* WoolCarpetTile::getAABB(Level* level, int x, int y, int z) {
int height = 0;
float offset = 1.0f / SharedConstants::WORLD_RESOLUTION;
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(Tile::tlsIdxShape);
ThreadStorage* tls = m_threadShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape();
return AABB::newTemp(x + tls->xx0, y + tls->yy0, z + tls->zz0, x + tls->xx1,