2023-07-07 14:00:48 -04:00
|
|
|
|
|
|
|
#include "mxthread.h"
|
|
|
|
|
|
|
|
#include "mxomni.h"
|
2023-10-07 11:30:04 -04:00
|
|
|
#include "mxtimer.h"
|
|
|
|
|
|
|
|
#include <process.h>
|
2023-07-07 14:00:48 -04:00
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100b8bb0
|
2023-12-13 05:48:14 -05:00
|
|
|
MxTickleThread::MxTickleThread(MxCore* p_target, MxS32 p_frequencyMS)
|
2023-07-07 14:00:48 -04:00
|
|
|
{
|
2023-11-21 03:44:45 -05:00
|
|
|
m_target = p_target;
|
|
|
|
m_frequencyMS = p_frequencyMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match except for register allocation
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100b8c90
|
2023-11-21 03:44:45 -05:00
|
|
|
MxResult MxTickleThread::Run()
|
|
|
|
{
|
|
|
|
MxTimer* timer = Timer();
|
2023-12-13 05:48:14 -05:00
|
|
|
MxS32 lastTickled = -m_frequencyMS;
|
2023-11-21 03:44:45 -05:00
|
|
|
while (IsRunning()) {
|
2023-12-13 05:48:14 -05:00
|
|
|
MxLong currentTime = timer->GetTime();
|
2023-11-21 03:44:45 -05:00
|
|
|
|
|
|
|
if (currentTime < lastTickled) {
|
|
|
|
lastTickled = -m_frequencyMS;
|
|
|
|
}
|
2023-12-13 05:48:14 -05:00
|
|
|
|
|
|
|
MxS32 timeRemainingMS = (m_frequencyMS - currentTime) + lastTickled;
|
2023-11-21 03:44:45 -05:00
|
|
|
if (timeRemainingMS <= 0) {
|
|
|
|
m_target->Tickle();
|
|
|
|
timeRemainingMS = 0;
|
|
|
|
lastTickled = currentTime;
|
|
|
|
}
|
|
|
|
Sleep(timeRemainingMS);
|
|
|
|
}
|
|
|
|
return MxThread::Run();
|
2023-07-07 14:00:48 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100bf510
|
2023-07-07 14:00:48 -04:00
|
|
|
MxThread::MxThread()
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
m_hThread = NULL;
|
|
|
|
m_running = TRUE;
|
|
|
|
m_threadId = 0;
|
2023-07-07 14:00:48 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100bf5a0
|
2023-07-07 14:00:48 -04:00
|
|
|
MxThread::~MxThread()
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
if (m_hThread)
|
|
|
|
CloseHandle((HANDLE) m_hThread);
|
2023-07-07 14:00:48 -04:00
|
|
|
}
|
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
typedef unsigned(__stdcall* ThreadFunc)(void*);
|
2023-07-07 14:00:48 -04:00
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100bf610
|
2023-12-13 05:48:14 -05:00
|
|
|
MxResult MxThread::Start(MxS32 p_stack, MxS32 p_flag)
|
2023-07-07 14:00:48 -04:00
|
|
|
{
|
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;
|
2023-07-07 14:00:48 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100bf660
|
2023-11-21 03:44:45 -05:00
|
|
|
void MxThread::Sleep(MxS32 p_milliseconds)
|
|
|
|
{
|
|
|
|
::Sleep(p_milliseconds);
|
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100bf670
|
2023-07-07 14:00:48 -04:00
|
|
|
void MxThread::Terminate()
|
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
m_running = FALSE;
|
|
|
|
m_semaphore.Wait(INFINITE);
|
2023-07-07 14:00:48 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100bf680
|
2023-10-24 19:38:27 -04:00
|
|
|
unsigned MxThread::ThreadProc(void* p_thread)
|
2023-07-07 14:00:48 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
return static_cast<MxThread*>(p_thread)->Run();
|
2023-07-07 14:00:48 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:10:45 -05:00
|
|
|
// FUNCTION: LEGO1 0x100bf690
|
2023-11-21 03:44:45 -05:00
|
|
|
MxResult MxThread::Run()
|
2023-07-07 14:00:48 -04:00
|
|
|
{
|
2023-11-21 03:44:45 -05:00
|
|
|
m_semaphore.Release(1);
|
|
|
|
return SUCCESS;
|
2023-10-25 14:51:59 -04:00
|
|
|
}
|