From e37939276f78a5b0c11214a0ff185ef9f4f1c4dd Mon Sep 17 00:00:00 2001 From: Christian Semmler Date: Thu, 30 May 2024 16:06:46 -0400 Subject: [PATCH 1/2] Replace Windows CriticalSection with SDL Mutex --- CMakeLists.txt | 6 +-- LEGO1/omni/include/mxcriticalsection.h | 9 +++-- LEGO1/omni/include/mxomni.h | 2 + LEGO1/omni/include/mxsemaphore.h | 2 +- LEGO1/omni/src/system/mxcriticalsection.cpp | 45 +++------------------ util/platform.h | 20 +++++++++ 6 files changed, 37 insertions(+), 47 deletions(-) create mode 100644 util/platform.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b92b28b2..80a51768 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -299,7 +299,7 @@ add_library(omni STATIC register_lego1_target(omni) set_property(TARGET omni PROPERTY ARCHIVE_OUTPUT_NAME "omni$<$:d>") target_include_directories(omni PRIVATE "${CMAKE_SOURCE_DIR}/LEGO1/omni/include" "${CMAKE_SOURCE_DIR}/LEGO1" "${CMAKE_SOURCE_DIR}/util") -target_link_libraries(omni PRIVATE dsound winmm libsmacker SDL3::SDL3) +target_link_libraries(omni PRIVATE dsound winmm libsmacker) add_library(lego1 SHARED LEGO1/define.cpp @@ -462,7 +462,7 @@ target_include_directories(lego1 PUBLIC "${CMAKE_SOURCE_DIR}/LEGO1/lego/legoomni target_link_libraries(lego1 PRIVATE tglrl viewmanager realtime mxdirectx roi geom anim Vec::Vec dinput dxguid misc 3dmanager omni) foreach(tgt IN LISTS lego1_targets) - target_link_libraries(${tgt} PRIVATE $<$:DirectX5::DirectX5>) + target_link_libraries(${tgt} PRIVATE $<$:DirectX5::DirectX5> SDL3::SDL3) endforeach() # Make sure filenames are ALL CAPS @@ -479,7 +479,7 @@ if (ISLE_BUILD_APP) target_compile_definitions(isle PRIVATE ISLE_APP) # Use internal DirectX 5 if required - target_link_libraries(isle PRIVATE $<$:DirectX5::DirectX5>) + target_link_libraries(isle PRIVATE $<$:DirectX5::DirectX5> SDL3::SDL3) # Link DSOUND, WINMM, and LEGO1 target_link_libraries(isle PRIVATE dsound winmm lego1) diff --git a/LEGO1/omni/include/mxcriticalsection.h b/LEGO1/omni/include/mxcriticalsection.h index 23c3fb76..9a5c85b6 100644 --- a/LEGO1/omni/include/mxcriticalsection.h +++ b/LEGO1/omni/include/mxcriticalsection.h @@ -1,7 +1,7 @@ #ifndef MXCRITICALSECTION_H #define MXCRITICALSECTION_H -#include +#include // SIZE 0x1c class MxCriticalSection { @@ -15,8 +15,11 @@ class MxCriticalSection { void Leave(); private: - CRITICAL_SECTION m_criticalSection; // 0x00 - HANDLE m_mutex; // 0x18 + // [library:synchronization] + // SDL uses the most efficient mutex implementation available on the target platform. + // Originally this class allowed working with either a Win32 CriticalSection or Mutex, + // but only CriticalSection was ever used and we don't need both anyway. + SDL_Mutex* m_mutex; }; #endif // MXCRITICALSECTION_H diff --git a/LEGO1/omni/include/mxomni.h b/LEGO1/omni/include/mxomni.h index cb32e8c6..1b8b4573 100644 --- a/LEGO1/omni/include/mxomni.h +++ b/LEGO1/omni/include/mxomni.h @@ -5,6 +5,8 @@ #include "mxcriticalsection.h" #include "mxstring.h" +#include + class MxAtomSet; class MxDSAction; class MxEntity; diff --git a/LEGO1/omni/include/mxsemaphore.h b/LEGO1/omni/include/mxsemaphore.h index c2eba380..1d43009a 100644 --- a/LEGO1/omni/include/mxsemaphore.h +++ b/LEGO1/omni/include/mxsemaphore.h @@ -20,7 +20,7 @@ class MxSemaphore { void Release(); private: - SDL_Semaphore* m_semaphore; // 0x04 + SDL_Semaphore* m_semaphore; }; #endif // MXSEMAPHORE_H diff --git a/LEGO1/omni/src/system/mxcriticalsection.cpp b/LEGO1/omni/src/system/mxcriticalsection.cpp index f0adc551..e78bcfcd 100644 --- a/LEGO1/omni/src/system/mxcriticalsection.cpp +++ b/LEGO1/omni/src/system/mxcriticalsection.cpp @@ -1,8 +1,7 @@ #include "mxcriticalsection.h" #include "decomp.h" - -#include +#include "platform.h" DECOMP_SIZE_ASSERT(MxCriticalSection, 0x1c) @@ -12,61 +11,27 @@ BOOL g_useMutex = FALSE; // FUNCTION: LEGO1 0x100b6d20 MxCriticalSection::MxCriticalSection() { - HANDLE mutex; - - if (g_useMutex) { - mutex = CreateMutexA(NULL, FALSE, NULL); - m_mutex = mutex; - } - else { - InitializeCriticalSection(&m_criticalSection); - m_mutex = NULL; - } + m_mutex = SDL_CreateMutex(); } // FUNCTION: LEGO1 0x100b6d60 MxCriticalSection::~MxCriticalSection() { if (m_mutex != NULL) { - CloseHandle(m_mutex); - } - else { - DeleteCriticalSection(&m_criticalSection); + SDL_DestroyMutex(m_mutex); } } // FUNCTION: LEGO1 0x100b6d80 void MxCriticalSection::Enter() { - DWORD result; - FILE* file; - - if (m_mutex != NULL) { - result = WaitForSingleObject(m_mutex, 5000); - if (result == WAIT_FAILED) { - file = fopen("C:\\DEADLOCK.TXT", "a"); - if (file != NULL) { - fprintf(file, "mutex timeout occurred!\n"); - fclose(file); - } - - abort(); - } - } - else { - EnterCriticalSection(&m_criticalSection); - } + SDL_LockMutex(m_mutex); } // FUNCTION: LEGO1 0x100b6de0 void MxCriticalSection::Leave() { - if (m_mutex != NULL) { - ReleaseMutex(m_mutex); - } - else { - LeaveCriticalSection(&m_criticalSection); - } + SDL_UnlockMutex(m_mutex); } // FUNCTION: LEGO1 0x100b6e00 diff --git a/util/platform.h b/util/platform.h new file mode 100644 index 00000000..8ea91871 --- /dev/null +++ b/util/platform.h @@ -0,0 +1,20 @@ +#ifndef TYPES_H +#define TYPES_H + +// Defining substitutions for definitions usually found in Windows headers + +#define BOOL int32_t + +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +#ifndef NULL +#define NULL 0 +#endif + +#endif // TYPES_H From a751187aff0e2557584071cbf00d94f9c1c5f253 Mon Sep 17 00:00:00 2001 From: Christian Semmler Date: Thu, 30 May 2024 16:08:50 -0400 Subject: [PATCH 2/2] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e23e2796..694f7ac4 100644 --- a/README.md +++ b/README.md @@ -22,14 +22,14 @@ To achieve our goal of platform independence, we need to replace any Windows-onl | Library | Substitution | Implementation status | | | - | - | - | - | -| [Smacker](https://github.com/isledecomp/isle/tree/master/3rdparty/smacker) | [libsmacker](https://github.com/foxtacles/libsmacker) | ✅ | [Open issues](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable%20%22%2F%2F%20%5Blibrary%3Alibsmacker%5D%22&type=code) | -| Filesystem | C standard library | ❌ | [Open issues](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3Afilesystem%5D%22&type=code) | -| Threads, Mutexes (Synchronization) | [SDL3](https://www.libsdl.org/) | ❌ | [Open issues](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3Asynchronization%5D%22&type=code) | -| Keyboard, Mouse, Joystick, DirectInput (Input) | [SDL3](https://www.libsdl.org/) | ❌ | [Open issues](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3Ainput%5D%22&type=code) | -| WinMM, DirectSound (Audio) | [SDL3](https://www.libsdl.org/) | ❌ | [Open issues](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3Aaudio%5D%22&type=code) | -| DirectDraw (2D video) | [SDL3](https://www.libsdl.org/) | ❌ | [Open issues](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3A2d%5D%22&type=code) | -| Direct3D (3D video) | [SDL3](https://www.libsdl.org/), OpenGL ES (**TBD**) | ❌ | [Open issues](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3A3d%5D%22&type=code) | -| Direct3D Retained Mode | Custom re-implementation (**TBD**) | ❌ | [Open issues](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3Aretained%5D%22&type=code) | +| [Smacker](https://github.com/isledecomp/isle/tree/master/3rdparty/smacker) | [libsmacker](https://github.com/foxtacles/libsmacker) | ✅ | [Remarks](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable%20%22%2F%2F%20%5Blibrary%3Alibsmacker%5D%22&type=code) | +| Filesystem | C standard library | ❌ | [Remarks](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3Afilesystem%5D%22&type=code) | +| Threads, Mutexes (Synchronization) | [SDL3](https://www.libsdl.org/) | ❌ | [Remarks](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3Asynchronization%5D%22&type=code) | +| Keyboard, Mouse, Joystick, DirectInput (Input) | [SDL3](https://www.libsdl.org/) | ❌ | [Remarks](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3Ainput%5D%22&type=code) | +| WinMM, DirectSound (Audio) | [SDL3](https://www.libsdl.org/) | ❌ | [Remarks](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3Aaudio%5D%22&type=code) | +| DirectDraw (2D video) | [SDL3](https://www.libsdl.org/) | ❌ | [Remarks](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3A2d%5D%22&type=code) | +| Direct3D (3D video) | [SDL3](https://www.libsdl.org/), OpenGL ES (**TBD**) | ❌ | [Remarks](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3A3d%5D%22&type=code) | +| Direct3D Retained Mode | Custom re-implementation (**TBD**) | ❌ | [Remarks](https://github.com/search?q=repo%3Aisledecomp%2Fisle-portable+%22%2F%2F+%5Blibrary%3Aretained%5D%22&type=code) | | [SmartHeap](https://github.com/isledecomp/isle/tree/master/3rdparty/smartheap) | Default memory allocator | - | - | ## Building