refactor: replace winapi TLS functions in Vec3 and AABB

This commit is contained in:
Tropical 2026-03-25 16:22:02 -05:00
parent f4a2db1252
commit 7c6c6d3df7
4 changed files with 25 additions and 29 deletions

View file

@ -7,8 +7,8 @@
#include "AABB.h"
#include "HitResult.h"
unsigned int AABB::tlsIdx = 0;
AABB::ThreadStorage* AABB::tlsDefault = NULL;
thread_local AABB::ThreadStorage* AABB::m_tlsPool = nullptr;
AABB::ThreadStorage* AABB::m_tlsPoolDefault = nullptr;
AABB::ThreadStorage::ThreadStorage() {
pool = new AABB[POOL_SIZE]; // 4jcraft, needs to be deleted with delete[]
@ -21,21 +21,18 @@ AABB::ThreadStorage::~ThreadStorage() {
void AABB::CreateNewThreadStorage() {
ThreadStorage* tls = new ThreadStorage();
if (tlsDefault == NULL) {
tlsIdx = TlsAlloc();
tlsDefault = tls;
if (m_tlsPoolDefault == nullptr) {
m_tlsPoolDefault = tls;
}
TlsSetValue(tlsIdx, tls);
m_tlsPool = m_tlsPoolDefault;
}
void AABB::UseDefaultThreadStorage() { TlsSetValue(tlsIdx, tlsDefault); }
void AABB::UseDefaultThreadStorage() { m_tlsPool = m_tlsPoolDefault; }
void AABB::ReleaseThreadStorage() {
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx);
if (tls == tlsDefault) return;
delete tls;
if (m_tlsPool != m_tlsPoolDefault) {
delete m_tlsPool;
}
}
AABB* AABB::newPermanent(double x0, double y0, double z0, double x1, double y1,
@ -49,7 +46,7 @@ void AABB::resetPool() {}
AABB* AABB::newTemp(double x0, double y0, double z0, double x1, double y1,
double z1) {
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx);
ThreadStorage* tls = m_tlsPool;
AABB* thisAABB = &tls->pool[tls->poolPointer];
thisAABB->set(x0, y0, z0, x1, y1, z1);
tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE;

View file

@ -15,8 +15,8 @@ class AABB {
ThreadStorage();
~ThreadStorage();
};
static unsigned int tlsIdx;
static ThreadStorage* tlsDefault;
static thread_local ThreadStorage* m_tlsPool;
static ThreadStorage* m_tlsPoolDefault;
public:
// Each new thread that needs to use Vec3 pools will need to call one of the

View file

@ -2,8 +2,8 @@
#include "Vec3.h"
#include "AABB.h"
unsigned int Vec3::tlsIdx = 0;
Vec3::ThreadStorage* Vec3::tlsDefault = NULL;
thread_local Vec3::ThreadStorage* Vec3::m_tlsPool = nullptr;
Vec3::ThreadStorage* Vec3::m_tlsPoolDefault = nullptr;
Vec3::ThreadStorage::ThreadStorage() {
pool = new Vec3[POOL_SIZE];
@ -14,20 +14,18 @@ Vec3::ThreadStorage::~ThreadStorage() { delete[] pool; }
void Vec3::CreateNewThreadStorage() {
ThreadStorage* tls = new ThreadStorage();
if (tlsDefault == NULL) {
tlsIdx = TlsAlloc();
tlsDefault = tls;
if (m_tlsPoolDefault == nullptr) {
m_tlsPoolDefault = tls;
}
TlsSetValue(tlsIdx, tls);
m_tlsPool = m_tlsPoolDefault;
}
void Vec3::UseDefaultThreadStorage() { TlsSetValue(tlsIdx, tlsDefault); }
void Vec3::UseDefaultThreadStorage() { m_tlsPool = m_tlsPoolDefault; }
void Vec3::ReleaseThreadStorage() {
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx);
if (tls == tlsDefault) return;
delete tls;
if (m_tlsPool != m_tlsPoolDefault) {
delete m_tlsPool;
}
}
Vec3* Vec3::newPermanent(double x, double y, double z) {
@ -39,7 +37,7 @@ void Vec3::clearPool() {}
void Vec3::resetPool() {}
Vec3* Vec3::newTemp(double x, double y, double z) {
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx);
ThreadStorage* tls = m_tlsPool;
Vec3* thisVec = &tls->pool[tls->poolPointer];
thisVec->set(x, y, z);
tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE;

View file

@ -12,8 +12,9 @@ class Vec3 {
ThreadStorage();
~ThreadStorage();
};
static unsigned int tlsIdx;
static ThreadStorage* tlsDefault;
static thread_local ThreadStorage* m_tlsPool;
static ThreadStorage* m_tlsPoolDefault;
public:
// Each new thread that needs to use Vec3 pools will need to call one of the