project64/Source/Project64-core/N64System/EmulationThread.cpp

79 lines
2.1 KiB
C++
Raw Normal View History

#include "stdafx.h"
2021-04-14 05:34:15 +00:00
#include <Project64-core/N64System/N64System.h>
2015-12-06 09:59:58 +00:00
#include <Project64-core/Notification.h>
2015-12-19 23:57:27 +00:00
#include <Common/Util.h>
void CN64System::StartEmulationThead()
{
WriteTrace(TraceN64System, TraceDebug, "Start");
CThread * thread = new CThread((CThread::CTHREAD_START_ROUTINE)StartEmulationThread);
thread->Start(thread);
WriteTrace(TraceN64System, TraceDebug, "Done");
}
void CN64System::StartEmulationThread(CThread * thread)
{
WriteTrace(TraceN64System, TraceDebug, "Start");
#ifdef _WIN32
if (g_Settings->LoadBool(Setting_CN64TimeCritical))
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
}
2021-04-12 11:35:39 +00:00
CoInitialize(nullptr);
EmulationStarting(thread);
CoUninitialize();
#else
EmulationStarting(thread);
#endif
WriteTrace(TraceN64System, TraceDebug, "Done");
}
void CN64System::CloseCpu()
{
WriteTrace(TraceN64System, TraceDebug, "Start");
2021-04-12 11:35:39 +00:00
if (m_thread == nullptr)
{
return;
}
WriteTrace(TraceN64System, TraceDebug, "Setting end emulation");
m_EndEmulation = true;
if (g_Settings->LoadBool(GameRunning_CPU_Paused))
{
WriteTrace(TraceN64System, TraceDebug, "Resume CPU");
m_hPauseEvent.Trigger();
}
if (CThread::GetCurrentThreadId() == m_thread->ThreadID())
{
WriteTrace(TraceN64System, TraceDebug, "CloseCpu called on emulation thread");
ExternalEvent(SysEvent_CloseCPU);
return;
}
CThread * hThread = m_thread;
2021-04-12 11:35:39 +00:00
m_thread = nullptr;
for (int count = 0; count < 200; count++)
{
2021-04-12 11:35:39 +00:00
if (hThread == nullptr || !hThread->isRunning())
{
WriteTrace(TraceN64System, TraceDebug, "Thread no longer running");
break;
}
WriteTrace(TraceN64System, TraceDebug, "%d - waiting", count);
pjutil::Sleep(100);
WriteTrace(TraceN64System, TraceDebug, "%d - Finished wait", count);
if (g_Notify->ProcessGuiMessages())
{
return;
}
}
CpuStopped();
WriteTrace(TraceN64System, TraceDebug, "Deleting thread object");
delete hThread;
WriteTrace(TraceN64System, TraceDebug, "Done");
}