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