mirror of
https://github.com/4jcraft/4jcraft.git
synced 2026-08-20 09:57:10 +00:00
refactor: replace winapi TLS functions in Vec3 and AABB
This commit is contained in:
parent
f4a2db1252
commit
7c6c6d3df7
|
|
@ -7,8 +7,8 @@
|
||||||
#include "AABB.h"
|
#include "AABB.h"
|
||||||
#include "HitResult.h"
|
#include "HitResult.h"
|
||||||
|
|
||||||
unsigned int AABB::tlsIdx = 0;
|
thread_local AABB::ThreadStorage* AABB::m_tlsPool = nullptr;
|
||||||
AABB::ThreadStorage* AABB::tlsDefault = NULL;
|
AABB::ThreadStorage* AABB::m_tlsPoolDefault = nullptr;
|
||||||
|
|
||||||
AABB::ThreadStorage::ThreadStorage() {
|
AABB::ThreadStorage::ThreadStorage() {
|
||||||
pool = new AABB[POOL_SIZE]; // 4jcraft, needs to be deleted with delete[]
|
pool = new AABB[POOL_SIZE]; // 4jcraft, needs to be deleted with delete[]
|
||||||
|
|
@ -21,21 +21,18 @@ AABB::ThreadStorage::~ThreadStorage() {
|
||||||
|
|
||||||
void AABB::CreateNewThreadStorage() {
|
void AABB::CreateNewThreadStorage() {
|
||||||
ThreadStorage* tls = new ThreadStorage();
|
ThreadStorage* tls = new ThreadStorage();
|
||||||
if (tlsDefault == NULL) {
|
if (m_tlsPoolDefault == nullptr) {
|
||||||
tlsIdx = TlsAlloc();
|
m_tlsPoolDefault = tls;
|
||||||
tlsDefault = tls;
|
|
||||||
}
|
}
|
||||||
|
m_tlsPool = m_tlsPoolDefault;
|
||||||
TlsSetValue(tlsIdx, tls);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AABB::UseDefaultThreadStorage() { TlsSetValue(tlsIdx, tlsDefault); }
|
void AABB::UseDefaultThreadStorage() { m_tlsPool = m_tlsPoolDefault; }
|
||||||
|
|
||||||
void AABB::ReleaseThreadStorage() {
|
void AABB::ReleaseThreadStorage() {
|
||||||
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx);
|
if (m_tlsPool != m_tlsPoolDefault) {
|
||||||
if (tls == tlsDefault) return;
|
delete m_tlsPool;
|
||||||
|
}
|
||||||
delete tls;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AABB* AABB::newPermanent(double x0, double y0, double z0, double x1, double y1,
|
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,
|
AABB* AABB::newTemp(double x0, double y0, double z0, double x1, double y1,
|
||||||
double z1) {
|
double z1) {
|
||||||
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx);
|
ThreadStorage* tls = m_tlsPool;
|
||||||
AABB* thisAABB = &tls->pool[tls->poolPointer];
|
AABB* thisAABB = &tls->pool[tls->poolPointer];
|
||||||
thisAABB->set(x0, y0, z0, x1, y1, z1);
|
thisAABB->set(x0, y0, z0, x1, y1, z1);
|
||||||
tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE;
|
tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE;
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,8 @@ class AABB {
|
||||||
ThreadStorage();
|
ThreadStorage();
|
||||||
~ThreadStorage();
|
~ThreadStorage();
|
||||||
};
|
};
|
||||||
static unsigned int tlsIdx;
|
static thread_local ThreadStorage* m_tlsPool;
|
||||||
static ThreadStorage* tlsDefault;
|
static ThreadStorage* m_tlsPoolDefault;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Each new thread that needs to use Vec3 pools will need to call one of the
|
// Each new thread that needs to use Vec3 pools will need to call one of the
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@
|
||||||
#include "Vec3.h"
|
#include "Vec3.h"
|
||||||
#include "AABB.h"
|
#include "AABB.h"
|
||||||
|
|
||||||
unsigned int Vec3::tlsIdx = 0;
|
thread_local Vec3::ThreadStorage* Vec3::m_tlsPool = nullptr;
|
||||||
Vec3::ThreadStorage* Vec3::tlsDefault = NULL;
|
Vec3::ThreadStorage* Vec3::m_tlsPoolDefault = nullptr;
|
||||||
|
|
||||||
Vec3::ThreadStorage::ThreadStorage() {
|
Vec3::ThreadStorage::ThreadStorage() {
|
||||||
pool = new Vec3[POOL_SIZE];
|
pool = new Vec3[POOL_SIZE];
|
||||||
|
|
@ -14,20 +14,18 @@ Vec3::ThreadStorage::~ThreadStorage() { delete[] pool; }
|
||||||
|
|
||||||
void Vec3::CreateNewThreadStorage() {
|
void Vec3::CreateNewThreadStorage() {
|
||||||
ThreadStorage* tls = new ThreadStorage();
|
ThreadStorage* tls = new ThreadStorage();
|
||||||
if (tlsDefault == NULL) {
|
if (m_tlsPoolDefault == nullptr) {
|
||||||
tlsIdx = TlsAlloc();
|
m_tlsPoolDefault = tls;
|
||||||
tlsDefault = tls;
|
|
||||||
}
|
}
|
||||||
TlsSetValue(tlsIdx, tls);
|
m_tlsPool = m_tlsPoolDefault;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Vec3::UseDefaultThreadStorage() { TlsSetValue(tlsIdx, tlsDefault); }
|
void Vec3::UseDefaultThreadStorage() { m_tlsPool = m_tlsPoolDefault; }
|
||||||
|
|
||||||
void Vec3::ReleaseThreadStorage() {
|
void Vec3::ReleaseThreadStorage() {
|
||||||
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx);
|
if (m_tlsPool != m_tlsPoolDefault) {
|
||||||
if (tls == tlsDefault) return;
|
delete m_tlsPool;
|
||||||
|
}
|
||||||
delete tls;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3* Vec3::newPermanent(double x, double y, double z) {
|
Vec3* Vec3::newPermanent(double x, double y, double z) {
|
||||||
|
|
@ -39,7 +37,7 @@ void Vec3::clearPool() {}
|
||||||
void Vec3::resetPool() {}
|
void Vec3::resetPool() {}
|
||||||
|
|
||||||
Vec3* Vec3::newTemp(double x, double y, double z) {
|
Vec3* Vec3::newTemp(double x, double y, double z) {
|
||||||
ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx);
|
ThreadStorage* tls = m_tlsPool;
|
||||||
Vec3* thisVec = &tls->pool[tls->poolPointer];
|
Vec3* thisVec = &tls->pool[tls->poolPointer];
|
||||||
thisVec->set(x, y, z);
|
thisVec->set(x, y, z);
|
||||||
tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE;
|
tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE;
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,9 @@ class Vec3 {
|
||||||
ThreadStorage();
|
ThreadStorage();
|
||||||
~ThreadStorage();
|
~ThreadStorage();
|
||||||
};
|
};
|
||||||
static unsigned int tlsIdx;
|
|
||||||
static ThreadStorage* tlsDefault;
|
static thread_local ThreadStorage* m_tlsPool;
|
||||||
|
static ThreadStorage* m_tlsPoolDefault;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Each new thread that needs to use Vec3 pools will need to call one of the
|
// Each new thread that needs to use Vec3 pools will need to call one of the
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue