2022-09-21 05:16:07 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
#include "ScriptInstance.h"
|
2022-09-21 05:16:07 +00:00
|
|
|
#include "ScriptWorker.h"
|
2021-09-03 05:11:04 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptWorker::CScriptWorker(CScriptInstance * instance, void * dukObjectHeapPtr) :
|
2021-09-03 05:11:04 +00:00
|
|
|
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();
|
2021-09-03 05:11:04 +00:00
|
|
|
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);
|
2021-09-03 05:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|