isle-portable/LEGO1/mxthread.cpp
Mark Langen 889fd886f0
MxSemphore + MxThread + MxThread implementions (#80)
* Add MxSemphore + MxThread and the two implementations I could find
  of MxThread (consumers extend it and override the Run method).

* Implement a function in MxDiskStreamProvider which uses thread and
  semaphore to confirm correct layout / size of those classes.

* All 100% match except two functions with a pair of registers swapped.
2023-07-07 11:00:48 -07:00

99 lines
No EOL
1.9 KiB
C++

#include "mxthread.h"
#include <process.h>
#include "mxomni.h"
// OFFSET: LEGO1 0x100bf690
MxResult MxThread::Run()
{
m_semaphore.Release(1);
return SUCCESS;
}
// OFFSET: LEGO1 0x100bf510
MxThread::MxThread()
{
m_hThread = NULL;
m_running = TRUE;
m_threadId = 0;
}
// OFFSET: LEGO1 0x100bf5a0
MxThread::~MxThread()
{
if (m_hThread)
CloseHandle((HANDLE)m_hThread);
}
typedef unsigned(__stdcall *ThreadFunc)(void *);
// OFFSET: LEGO1 0x100bf610
MxResult MxThread::Start(int p_stack, int 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))
result = SUCCESS;
}
return result;
}
// OFFSET: LEGO1 0x100bf670
void MxThread::Terminate()
{
m_running = FALSE;
m_semaphore.Wait(INFINITE);
}
// OFFSET: LEGO1 0x100bf680
unsigned MxThread::ThreadProc(void *p_thread)
{
return static_cast<MxThread*>(p_thread)->Run();
}
// OFFSET: LEGO1 0x100bf660
void MxThread::Sleep(MxS32 p_milliseconds)
{
::Sleep(p_milliseconds);
}
// OFFSET: LEGO1 0x100b8bb0
MxTickleThread::MxTickleThread(MxCore *p_target, int p_frequencyMS)
{
m_target = p_target;
m_frequencyMS = p_frequencyMS;
}
// OFFSET: LEGO1 0x100d0f50
MxResult MxTickleThread::StartWithTarget(MxCore* p_target)
{
m_target = p_target;
return Start(0x1000, 0);
}
// Match except for register allocation
// OFFSET: LEGO1 0x100b8c90
MxResult MxTickleThread::Run()
{
MxTimer* timer = Timer();
int lastTickled = -m_frequencyMS;
while (IsRunning())
{
int currentTime = timer->GetTime();
if (currentTime < lastTickled) {
lastTickled = -m_frequencyMS;
}
int timeRemainingMS = (m_frequencyMS - currentTime) + lastTickled;
if (timeRemainingMS <= 0) {
m_target->Tickle();
timeRemainingMS = 0;
lastTickled = currentTime;
}
Sleep(timeRemainingMS);
}
return MxThread::Run();
}