isle/LEGO1/mxthread.cpp

93 lines
1.8 KiB
C++
Raw Normal View History

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