project64/Source/Project64/UserInterface/Debugger/ScriptWorker.cpp

69 lines
1.2 KiB
C++
Raw Normal View History

2022-09-21 05:16:07 +00:00
#include "stdafx.h"
#include "ScriptInstance.h"
2022-09-21 05:16:07 +00:00
#include "ScriptWorker.h"
2022-09-21 05:16:07 +00:00
CScriptWorker::CScriptWorker(CScriptInstance * instance, void * dukObjectHeapPtr) :
m_bStopping(false),
m_bRegistered(false),
m_hThread(nullptr),
m_Instance(instance),
m_DukObjectHeapPtr(dukObjectHeapPtr)
{
m_bRegistered = m_Instance->RegisterWorker(this);
}
CScriptWorker::~CScriptWorker()
{
if (m_bRegistered)
{
StopWorkerProc();
m_Instance->UnregisterWorker(this);
}
}
2022-09-21 05:16:07 +00:00
DWORD WINAPI CScriptWorker::ThreadProc(void * _this)
{
((CScriptWorker *)_this)->WorkerProc();
return 0;
}
void CScriptWorker::StartWorkerProc()
{
if (!m_bRegistered)
{
return;
}
if (m_hThread != nullptr)
{
return;
}
2022-09-21 05:16:07 +00:00
m_hThread = CreateThread(0, 0, ThreadProc, (void *)this, 0, nullptr);
}
void CScriptWorker::StopWorkerProc()
{
if (m_hThread == nullptr)
{
return;
}
{
CGuard guard(m_CS);
m_bStopping = true;
}
WaitForSingleObject(m_hThread, INFINITE);
CloseHandle(m_hThread);
m_hThread = nullptr;
m_bStopping = false;
}
bool CScriptWorker::StopRequested()
{
CGuard guard(m_CS);
return m_bStopping;
}