Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Windows CriticalSection with SDL Mutex #7

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ add_library(omni STATIC
register_lego1_target(omni)
set_property(TARGET omni PROPERTY ARCHIVE_OUTPUT_NAME "omni$<$<CONFIG:Debug>: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
Expand Down Expand Up @@ -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 $<$<BOOL:${ISLE_USE_DX5}>:DirectX5::DirectX5>)
target_link_libraries(${tgt} PRIVATE $<$<BOOL:${ISLE_USE_DX5}>:DirectX5::DirectX5> SDL3::SDL3)
endforeach()

# Make sure filenames are ALL CAPS
Expand All @@ -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 $<$<BOOL:${ISLE_USE_DX5}>:DirectX5::DirectX5>)
target_link_libraries(isle PRIVATE $<$<BOOL:${ISLE_USE_DX5}>:DirectX5::DirectX5> SDL3::SDL3)

# Link DSOUND, WINMM, and LEGO1
target_link_libraries(isle PRIVATE dsound winmm lego1)
Expand Down
9 changes: 6 additions & 3 deletions LEGO1/omni/include/mxcriticalsection.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MXCRITICALSECTION_H
#define MXCRITICALSECTION_H

#include <windows.h>
#include <SDL3/SDL_mutex.h>

// SIZE 0x1c
class MxCriticalSection {
Expand All @@ -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
2 changes: 2 additions & 0 deletions LEGO1/omni/include/mxomni.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "mxcriticalsection.h"
#include "mxstring.h"

#include <windows.h>

class MxAtomSet;
class MxDSAction;
class MxEntity;
Expand Down
2 changes: 1 addition & 1 deletion LEGO1/omni/include/mxsemaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MxSemaphore {
void Release();

private:
SDL_Semaphore* m_semaphore; // 0x04
SDL_Semaphore* m_semaphore;
};

#endif // MXSEMAPHORE_H
45 changes: 5 additions & 40 deletions LEGO1/omni/src/system/mxcriticalsection.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "mxcriticalsection.h"

#include "decomp.h"

#include <stdio.h>
#include "platform.h"

DECOMP_SIZE_ASSERT(MxCriticalSection, 0x1c)

Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions util/platform.h
Original file line number Diff line number Diff line change
@@ -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
Loading