From 065ffff0717283ae06774bc1a2b914f647bb31c2 Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Mon, 2 Mar 2026 19:25:37 -0600 Subject: [PATCH] feat: stub critical sections on top of pthread recursive mutexes --- Minecraft.World/Dimension.cpp | 2 +- Minecraft.World/linux/wlinux.h | 42 +++++++++++++++++++++++-- Minecraft.World/x64headers/extraX64.h | 45 --------------------------- 3 files changed, 41 insertions(+), 48 deletions(-) diff --git a/Minecraft.World/Dimension.cpp b/Minecraft.World/Dimension.cpp index e3eba15df..a2a52aac3 100644 --- a/Minecraft.World/Dimension.cpp +++ b/Minecraft.World/Dimension.cpp @@ -2,7 +2,7 @@ #include "net.minecraft.world.level.levelgen.h" #include "net.minecraft.world.level.h" #include "net.minecraft.world.level.storage.h" -#include "dimension.h" +#include "Dimension.h" #include "BiomeSource.h" #include "FixedBiomeSource.h" #include "OldChunkStorage.h" diff --git a/Minecraft.World/linux/wlinux.h b/Minecraft.World/linux/wlinux.h index e1fc27f0a..09e3bea5f 100644 --- a/Minecraft.World/linux/wlinux.h +++ b/Minecraft.World/linux/wlinux.h @@ -86,6 +86,44 @@ typedef float FLOAT; #define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF) +typedef pthread_mutex_t RTL_CRITICAL_SECTION; +typedef pthread_mutex_t* PRTL_CRITICAL_SECTION; + +typedef RTL_CRITICAL_SECTION CRITICAL_SECTION; +typedef PRTL_CRITICAL_SECTION PCRITICAL_SECTION; +typedef PRTL_CRITICAL_SECTION LPCRITICAL_SECTION; + +void InitializeCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) { + pthread_mutexattr_t attr; + int ret; + + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(CriticalSection, &attr); + pthread_mutexattr_destroy(&attr); +} + +void InitializeCriticalSectionAndSpinCount(PRTL_CRITICAL_SECTION CriticalSection, ULONG SpinCount) { + // no spin count required because we use a recursive mutex + InitializeCriticalSection(CriticalSection); +} + +void DeleteCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) { + pthread_mutex_destroy(CriticalSection); +} + +void EnterCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) { + pthread_mutex_lock(CriticalSection); +} + +void LeaveCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) { + pthread_mutex_unlock(CriticalSection); +} + +ULONG TryEnterCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) { + return pthread_mutex_trylock(CriticalSection) == 0; +} + // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsalloc DWORD TlsAlloc(VOID) { pthread_key_t key; @@ -119,8 +157,8 @@ BOOL TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue) // https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalmemorystatus VOID GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer) { - // TODO: Parse /proc/meminfo and set based on that. Probably will also need another different - // codepath for macOS too. + // TODO: Parse /proc/meminfo and set lpBuffer based on that. Probably will also need another + // different codepath for macOS too. } DWORD GetLastError(VOID) diff --git a/Minecraft.World/x64headers/extraX64.h b/Minecraft.World/x64headers/extraX64.h index b40171f02..17e1479df 100644 --- a/Minecraft.World/x64headers/extraX64.h +++ b/Minecraft.World/x64headers/extraX64.h @@ -22,51 +22,6 @@ const int XUSER_MAX_COUNT = 4; const int MINECRAFT_NET_MAX_PLAYERS = 8; #endif -#if defined(__linux__) - -typedef struct _RTL_CRITICAL_SECTION { - // // - // // The following field is used for blocking when there is contention for - // // the resource - // // - // - union { - ULONG_PTR RawEvent[4]; - } Synchronization; - // - // // - // // The following three fields control entering and exiting the critical - // // section for the resource - // // - // - LONG LockCount; - LONG RecursionCount; - HANDLE OwningThread; -} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION; - -typedef RTL_CRITICAL_SECTION CRITICAL_SECTION; - -inline void InitializeCriticalSection(CRITICAL_SECTION* stubEnterCS) -{ -} - -inline void InitializeCriticalSectionAndSpinCount(CRITICAL_SECTION* CriticalSection, ULONG SpinCount) -{ -} - -inline void DeleteCriticalSection(CRITICAL_SECTION* stubEnterCS) -{ -} - -inline void EnterCriticalSection(CRITICAL_SECTION* stubEnterCS) -{ -} - -inline void LeaveCriticalSection( CRITICAL_SECTION* stubEnterCS) -{ -} -#endif // __linux__ - #ifdef __ORBIS__ #include #include