Merge pull request #310 from 4jcraft/refactor/replace-winapi-stubs

refactor: finish up removing `Tls*` functions from linux code
This commit is contained in:
Tropical 2026-03-27 18:53:22 -05:00 committed by GitHub
commit 9bed5431eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 48 deletions

View file

@ -5,7 +5,6 @@
#include <assert.h>
// #include <system_service.h>
#include <codecvt>
#if defined(__linux__) && defined(__GLIBC__)
#include <signal.h>
#include <execinfo.h>
@ -710,15 +709,6 @@ void CleanupDevice() {
}
#endif
int StartMinecraftThreadProc(void* lpParameter) {
AABB::UseDefaultThreadStorage();
Tesselator::CreateNewThreadStorage(1024 * 1024);
RenderManager.InitialiseContext();
Minecraft::start(std::wstring(), std::wstring());
delete Tesselator::getInstance();
return 0;
}
int main(int argc, const char* argv[]) {
#if defined(__linux__) && defined(__GLIBC__)
struct sigaction sa;
@ -1197,7 +1187,7 @@ void FreeRichPresenceStrings() {
vRichPresenceStrings.clear();
}
#ifdef MEMORY_TRACKING
#if 0 // #ifdef MEMORY_TRACKING
int totalAllocGen = 0;
std::unordered_map<int, int> allocCounts;

View file

@ -306,28 +306,6 @@ static inline ULONG TryEnterCriticalSection(
return pthread_mutex_trylock(CriticalSection) == 0;
}
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsalloc
static inline DWORD TlsAlloc(VOID) {
pthread_key_t key;
if (pthread_key_create(&key, NULL) == 0) return key;
return TLS_OUT_OF_INDEXES;
}
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsfree
static inline BOOL TlsFree(DWORD dwTlsIndex) {
return pthread_key_delete(dwTlsIndex) == 0;
}
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsgetvalue
static inline LPVOID TlsGetValue(DWORD dwTlsIndex) {
return pthread_getspecific(dwTlsIndex);
}
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlssetvalue
static inline BOOL TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue) {
return pthread_setspecific(dwTlsIndex, lpTlsValue) == 0;
}
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalmemorystatus
static inline VOID GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer) {
// TODO: Parse /proc/meminfo and set lpBuffer based on that. Probably will

View file

@ -9,8 +9,8 @@
#include "HitResult.h"
#include "Util/Vec3.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[]
@ -23,21 +23,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 = tls;
}
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,
@ -51,7 +48,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