diff --git a/Source/Project64/N64 System/Emulation Thread.cpp b/Source/Project64/N64 System/Emulation Thread.cpp new file mode 100644 index 000000000..4c2742835 --- /dev/null +++ b/Source/Project64/N64 System/Emulation Thread.cpp @@ -0,0 +1,96 @@ +/**************************************************************************** +* * +* Project64 - A Nintendo 64 emulator. * +* http://www.pj64-emu.com/ * +* Copyright (C) 2012 Project64. All rights reserved. * +* * +* License: * +* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html * +* * +****************************************************************************/ +#include "stdafx.h" +#include +#include +#include +#include + +void CN64System::StartEmulationThead() +{ + ThreadInfo * Info = new ThreadInfo; + HANDLE * hThread = new HANDLE; + *hThread = NULL; + + //create the needed info into a structure to pass as one parameter + //for creating a thread + Info->ThreadHandle = hThread; + + *hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StartEmulationThread, Info, 0, (LPDWORD)&Info->ThreadID); +} + +void CN64System::StartEmulationThread(ThreadInfo * Info) +{ + if (g_Settings->LoadBool(Setting_CN64TimeCritical)) + { + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); + } + + CoInitialize(NULL); + + EmulationStarting(Info->ThreadHandle, Info->ThreadID); + delete ((HANDLE *)Info->ThreadHandle); + delete Info; + + CoUninitialize(); +} + +void CN64System::CloseCpu() +{ + if (m_CPU_Handle == NULL) + { + return; + } + + m_EndEmulation = true; + if (g_Settings->LoadBool(GameRunning_CPU_Paused)) + { + m_hPauseEvent.Trigger(); + } + + if (GetCurrentThreadId() == m_CPU_ThreadID) + { + ExternalEvent(SysEvent_CloseCPU); + return; + } + + HANDLE hThread = m_CPU_Handle; + m_CPU_Handle = NULL; + for (int count = 0; count < 200; count++) + { + pjutil::Sleep(100); + if (g_Notify->ProcessGuiMessages()) + { + return; + } + + DWORD ExitCode; + if (GetExitCodeThread(hThread, &ExitCode)) + { + if (ExitCode != STILL_ACTIVE) + { + break; + } + } + } + + if (hThread) + { + DWORD ExitCode; + GetExitCodeThread(hThread, &ExitCode); + if (ExitCode == STILL_ACTIVE) + { + TerminateThread(hThread, 0); + } + } + CloseHandle(hThread); + CpuStopped(); +} \ No newline at end of file diff --git a/Source/Project64/N64 System/N64 Class.cpp b/Source/Project64/N64 System/N64 Class.cpp index 192694e36..e8f166c6b 100644 --- a/Source/Project64/N64 System/N64 Class.cpp +++ b/Source/Project64/N64 System/N64 Class.cpp @@ -325,15 +325,7 @@ void CN64System::StartEmulation2(bool NewThread) } else { - ThreadInfo * Info = new ThreadInfo; - HANDLE * hThread = new HANDLE; - *hThread = NULL; - - //create the needed info into a structure to pass as one parameter - //for creating a thread - Info->ThreadHandle = hThread; - - *hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StartEmulationThread, Info, 0, (LPDWORD)&Info->ThreadID); + StartEmulationThead(); } } else @@ -359,72 +351,6 @@ void CN64System::StartEmulation(bool NewThread) } } -void CN64System::StartEmulationThread(ThreadInfo * Info) -{ - if (g_Settings->LoadBool(Setting_CN64TimeCritical)) - { - SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); - } - - CoInitialize(NULL); - - EmulationStarting(Info->ThreadHandle, Info->ThreadID); - delete Info->ThreadHandle; - delete Info; - - CoUninitialize(); -} - -void CN64System::CloseCpu() -{ - if (m_CPU_Handle == NULL) - { - return; - } - - m_EndEmulation = true; - if (g_Settings->LoadBool(GameRunning_CPU_Paused)) - { - m_hPauseEvent.Trigger(); - } - - if (GetCurrentThreadId() == m_CPU_ThreadID) - { - ExternalEvent(SysEvent_CloseCPU); - return; - } - - HANDLE hThread = m_CPU_Handle; - for (int count = 0; count < 200; count++) - { - Sleep(100); - if (Notify().ProcessGuiMessages()) - { - return; - } - - DWORD ExitCode; - if (GetExitCodeThread(hThread, &ExitCode)) - { - if (ExitCode != STILL_ACTIVE) - { - break; - } - } - } - - if (hThread) - { - DWORD ExitCode; - GetExitCodeThread(hThread, &ExitCode); - if (ExitCode == STILL_ACTIVE) - { - TerminateThread(hThread, 0); - } - } - CpuStopped(); -} - void CN64System::DisplayRomInfo(HWND hParent) { if (!g_Rom) { return; } diff --git a/Source/Project64/N64 System/N64 Class.h b/Source/Project64/N64 System/N64 Class.h index 9baa3b9e8..fff998f72 100644 --- a/Source/Project64/N64 System/N64 Class.h +++ b/Source/Project64/N64 System/N64 Class.h @@ -97,6 +97,7 @@ private: static void StartEmulationThread(ThreadInfo * Info); static bool EmulationStarting(void * hThread, uint32_t ThreadId); + static void StartEmulationThead(); void ExecuteCPU(); void RefreshScreen(); diff --git a/Source/Project64/Project64.vcproj b/Source/Project64/Project64.vcproj index 02e6ec7de..91fa3887d 100644 --- a/Source/Project64/Project64.vcproj +++ b/Source/Project64/Project64.vcproj @@ -440,6 +440,10 @@ RelativePath="N64 System\Cheat Class.cpp" > + + diff --git a/Source/Project64/Project64.vcxproj b/Source/Project64/Project64.vcxproj index 3675d5abc..a01afe91c 100644 --- a/Source/Project64/Project64.vcxproj +++ b/Source/Project64/Project64.vcxproj @@ -48,6 +48,7 @@ + diff --git a/Source/Project64/Project64.vcxproj.filters b/Source/Project64/Project64.vcxproj.filters index 0127d1bc3..e6bc26354 100644 --- a/Source/Project64/Project64.vcxproj.filters +++ b/Source/Project64/Project64.vcxproj.filters @@ -429,6 +429,9 @@ Source Files + + Source Files\N64 System Source +