mirror of
https://github.com/4jcraft/4jcraft.git
synced 2026-08-20 09:57:10 +00:00
Merge pull request #310 from 4jcraft/refactor/replace-winapi-stubs
refactor: finish up removing `Tls*` functions from linux code
This commit is contained in:
commit
9bed5431eb
|
|
@ -5,7 +5,6 @@
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
// #include <system_service.h>
|
// #include <system_service.h>
|
||||||
#include <codecvt>
|
|
||||||
#if defined(__linux__) && defined(__GLIBC__)
|
#if defined(__linux__) && defined(__GLIBC__)
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <execinfo.h>
|
#include <execinfo.h>
|
||||||
|
|
@ -710,15 +709,6 @@ void CleanupDevice() {
|
||||||
}
|
}
|
||||||
#endif
|
#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[]) {
|
int main(int argc, const char* argv[]) {
|
||||||
#if defined(__linux__) && defined(__GLIBC__)
|
#if defined(__linux__) && defined(__GLIBC__)
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
|
|
@ -1197,7 +1187,7 @@ void FreeRichPresenceStrings() {
|
||||||
vRichPresenceStrings.clear();
|
vRichPresenceStrings.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MEMORY_TRACKING
|
#if 0 // #ifdef MEMORY_TRACKING
|
||||||
|
|
||||||
int totalAllocGen = 0;
|
int totalAllocGen = 0;
|
||||||
std::unordered_map<int, int> allocCounts;
|
std::unordered_map<int, int> allocCounts;
|
||||||
|
|
|
||||||
|
|
@ -306,28 +306,6 @@ static inline ULONG TryEnterCriticalSection(
|
||||||
return pthread_mutex_trylock(CriticalSection) == 0;
|
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
|
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalmemorystatus
|
||||||
static inline VOID GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer) {
|
static inline VOID GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer) {
|
||||||
// TODO: Parse /proc/meminfo and set lpBuffer based on that. Probably will
|
// TODO: Parse /proc/meminfo and set lpBuffer based on that. Probably will
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@
|
||||||
#include "HitResult.h"
|
#include "HitResult.h"
|
||||||
#include "Util/Vec3.h"
|
#include "Util/Vec3.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[]
|
||||||
|
|
@ -23,21 +23,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 = tls;
|
||||||
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,
|
||||||
|
|
@ -51,7 +48,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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue