Skip to content

Commit

Permalink
Replace Windows threads with SDL threads
Browse files Browse the repository at this point in the history
  • Loading branch information
foxtacles committed May 30, 2024
1 parent c24221a commit 50460aa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
9 changes: 5 additions & 4 deletions LEGO1/omni/include/mxthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "mxsemaphore.h"
#include "mxtypes.h"

#include <SDL3/SDL_thread.h>

class MxCore;

// VTABLE: LEGO1 0x100dc860
Expand Down Expand Up @@ -33,10 +35,9 @@ class MxThread {
private:
static unsigned ThreadProc(void* p_thread);

MxULong m_hThread; // 0x04
MxU32 m_threadId; // 0x08
MxBool m_running; // 0x0c
MxSemaphore m_semaphore; // 0x10
SDL_Thread* m_thread;
MxBool m_running;
MxSemaphore m_semaphore;

protected:
MxCore* m_target; // 0x18
Expand Down
2 changes: 2 additions & 0 deletions LEGO1/omni/src/system/mxsemaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ MxResult MxSemaphore::Init(MxU32 p_initialCount, MxU32 p_maxCount)
{
// [library:synchronization] No support for max count, but shouldn't be necessary
MxResult result = FAILURE;

if ((m_semaphore = SDL_CreateSemaphore(p_initialCount))) {
result = SUCCESS;
}

return result;
}

Expand Down
27 changes: 15 additions & 12 deletions LEGO1/omni/src/system/mxthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,42 @@

#include "decomp.h"

#include <process.h>
#include <windows.h>
#include <SDL3/SDL_timer.h>

DECOMP_SIZE_ASSERT(MxThread, 0x1c)

// FUNCTION: LEGO1 0x100bf510
MxThread::MxThread()
{
m_hThread = NULL;
m_thread = NULL;
m_running = TRUE;
m_threadId = 0;
}

// FUNCTION: LEGO1 0x100bf5a0
MxThread::~MxThread()
{
if (m_hThread) {
CloseHandle((HANDLE) m_hThread);
}
}

typedef unsigned(__stdcall* ThreadFunc)(void*);

// FUNCTION: LEGO1 0x100bf610
MxResult MxThread::Start(MxS32 p_stack, MxS32 p_flag)
{
MxResult result = FAILURE;

if (m_semaphore.Init(0, 1) == SUCCESS) {
if ((m_hThread =
_beginthreadex(NULL, p_stack << 2, (ThreadFunc) &MxThread::ThreadProc, this, p_flag, &m_threadId))) {
const SDL_PropertiesID props = SDL_CreateProperties();
SDL_SetProperty(
props,
SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER,
(SDL_FunctionPointer) &MxThread::ThreadProc
);
SDL_SetProperty(props, SDL_PROP_THREAD_CREATE_USERDATA_POINTER, this);
SDL_SetNumberProperty(props, SDL_PROP_THREAD_CREATE_STACKSIZE_NUMBER, p_stack << 2);

if ((m_thread = SDL_CreateThreadWithProperties(props))) {
result = SUCCESS;
}

SDL_DestroyProperties(props);
}

return result;
Expand All @@ -43,7 +46,7 @@ MxResult MxThread::Start(MxS32 p_stack, MxS32 p_flag)
// FUNCTION: LEGO1 0x100bf660
void MxThread::Sleep(MxS32 p_milliseconds)
{
::Sleep(p_milliseconds);
SDL_Delay(p_milliseconds);
}

// FUNCTION: LEGO1 0x100bf670
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To achieve our goal of platform independence, we need to replace any Windows-onl
| - | - | - | - |
| [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) |
| 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) |
Expand Down

0 comments on commit 50460aa

Please sign in to comment.