diff --git a/GLCriticalSection.h b/GLCriticalSection.h deleted file mode 100644 index b994c5ee..00000000 --- a/GLCriticalSection.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef GLCRITICALSECTION_H -#define GLCRITICALSECTION_H - -#include // std::mutex - -class GLCriticalSection -{ -public: - void lock() - { - m_mtx.lock(); - m_locked = true; - } - - void unlock() - { - m_locked = false; - m_mtx.unlock(); - } - - bool isLocked() const {return m_locked;} - -protected: - class Lock { - public: - Lock(GLCriticalSection * _pCS) : m_pCS(_pCS) {m_pCS->lock();} - ~Lock() {m_pCS->unlock();} - private: - GLCriticalSection * m_pCS; - }; - -private: - std::mutex m_mtx; - bool m_locked; -}; - -#endif // GLCRITICALSECTION_H diff --git a/GLideN64.vcxproj b/GLideN64.vcxproj index c7adc3ec..7aead5f2 100644 --- a/GLideN64.vcxproj +++ b/GLideN64.vcxproj @@ -345,7 +345,6 @@ - diff --git a/GLideN64.vcxproj.filters b/GLideN64.vcxproj.filters index e4b4b5cd..c1a4df66 100644 --- a/GLideN64.vcxproj.filters +++ b/GLideN64.vcxproj.filters @@ -305,9 +305,6 @@ Header Files - - Header Files - diff --git a/PluginAPI.h b/PluginAPI.h index b672fef8..38665aad 100644 --- a/PluginAPI.h +++ b/PluginAPI.h @@ -1,17 +1,34 @@ #ifndef COMMONPLUGINAPI_H #define COMMONPLUGINAPI_H +#include +#include + #ifdef MUPENPLUSAPI #include "m64p_plugin.h" #else #include "ZilmarGFX_1_3.h" +#define RSPTHREAD #endif -#include "GLCriticalSection.h" +enum API_COMMAND { + acNone = 0, + acProcessDList, + acUpdateScreen, + acRomClosed +}; -class PluginAPI : public GLCriticalSection +class PluginAPI { public: +#ifdef RSPTHREAD + ~PluginAPI() + { + delete m_pRspThread; + m_pRspThread = NULL; + } +#endif + // Common void MoveScreen(int /*_xpos*/, int /*_ypos*/) {} void ProcessRDPList() {} @@ -70,10 +87,24 @@ public: } private: - PluginAPI() {} + PluginAPI() +#ifdef RSPTHREAD + : m_pRspThread(NULL), m_command(acNone) + #endif + {} PluginAPI(const PluginAPI &); - void _initiateGFX(const GFX_INFO & _gfxInfo); + void _initiateGFX(const GFX_INFO & _gfxInfo) const; + +#ifdef RSPTHREAD + void _callAPICommand(API_COMMAND _command); + std::mutex m_rspThreadMtx; + std::mutex m_pluginThreadMtx; + std::condition_variable_any m_rspThreadCv; + std::condition_variable_any m_pluginThreadCv; + std::thread * m_pRspThread; + API_COMMAND m_command; +#endif }; inline PluginAPI & api() diff --git a/common/CommonAPIImpl_common.cpp b/common/CommonAPIImpl_common.cpp index a0218e89..f5d4e8fd 100644 --- a/common/CommonAPIImpl_common.cpp +++ b/common/CommonAPIImpl_common.cpp @@ -3,6 +3,7 @@ #else # include "../winlnxdefs.h" #endif // _WINDOWS +#include #include "../PluginAPI.h" @@ -12,16 +13,75 @@ #include "../RSP.h" #include "../VI.h" #include "../Debug.h" +#include "../Log.h" + +#ifdef RSPTHREAD +void RSP_ThreadProc(std::mutex * _pRspThreadMtx, std::mutex * _pPluginThreadMtx, std::condition_variable_any * _pRspThreadCv, std::condition_variable_any * _pPluginThreadCv, API_COMMAND * _pCommand) +{ + _pRspThreadMtx->lock(); + RSP_Init(); + OGL_ResizeWindow(); + assert(!isGLError()); + + while (true) { + _pPluginThreadMtx->lock(); + _pPluginThreadCv->notify_one(); + _pPluginThreadMtx->unlock(); + _pRspThreadCv->wait(*_pRspThreadMtx); + switch (*_pCommand) { + case acProcessDList: + RSP_ProcessDList(); + break; + case acUpdateScreen: + VI_UpdateScreen(); + break; + case acRomClosed: + OGL_Stop(); + GBI_Destroy(); + *_pCommand = acNone; + _pRspThreadMtx->unlock(); + _pPluginThreadMtx->lock(); + _pPluginThreadCv->notify_one(); + _pPluginThreadMtx->unlock(); + return; + } + assert(!isGLError()); + *_pCommand = acNone; + } +} + +void PluginAPI::_callAPICommand(API_COMMAND _command) +{ + m_command = _command; + m_pluginThreadMtx.lock(); + m_rspThreadMtx.lock(); + m_rspThreadCv.notify_one(); + m_rspThreadMtx.unlock(); + m_pluginThreadCv.wait(m_pluginThreadMtx); + m_pluginThreadMtx.unlock(); +} +#endif void PluginAPI::ProcessDList() { - Lock lock(this); + LOG(LOG_APIFUNC, "ProcessDList\n"); +#ifdef RSPTHREAD + _callAPICommand(acProcessDList); +#else RSP_ProcessDList(); +#endif } void PluginAPI::RomClosed() { + LOG(LOG_APIFUNC, "RomClosed\n"); +#ifdef RSPTHREAD + _callAPICommand(acRomClosed); + delete m_pRspThread; + m_pRspThread = NULL; +#else OGL_Stop(); +#endif #ifdef DEBUG CloseDebugDlg(); @@ -30,9 +90,17 @@ void PluginAPI::RomClosed() void PluginAPI::RomOpen() { + LOG(LOG_APIFUNC, "RomOpen\n"); +#ifdef RSPTHREAD + m_pluginThreadMtx.lock(); + m_pRspThread = new std::thread(RSP_ThreadProc, &m_rspThreadMtx, &m_pluginThreadMtx, &m_rspThreadCv, &m_pluginThreadCv, &m_command); + m_pRspThread->detach(); + m_pluginThreadCv.wait(m_pluginThreadMtx); + m_pluginThreadMtx.unlock(); +#else RSP_Init(); - OGL_ResizeWindow(); +#endif #ifdef DEBUG OpenDebugDlg(); @@ -46,11 +114,15 @@ void PluginAPI::ShowCFB() void PluginAPI::UpdateScreen() { - Lock lock(this); + LOG(LOG_APIFUNC, "UpdateScreen\n"); +#ifdef RSPTHREAD + _callAPICommand(acUpdateScreen); +#else VI_UpdateScreen(); +#endif } -void PluginAPI::_initiateGFX(const GFX_INFO & _gfxInfo) { +void PluginAPI::_initiateGFX(const GFX_INFO & _gfxInfo) const { DMEM = _gfxInfo.DMEM; IMEM = _gfxInfo.IMEM; RDRAM = _gfxInfo.RDRAM; diff --git a/mupenplus/CommonAPIImpl_mupenplus.cpp b/mupenplus/CommonAPIImpl_mupenplus.cpp index cb081cb8..8bd71e1a 100644 --- a/mupenplus/CommonAPIImpl_mupenplus.cpp +++ b/mupenplus/CommonAPIImpl_mupenplus.cpp @@ -4,7 +4,6 @@ int PluginAPI::InitiateGFX(const GFX_INFO & _gfxInfo) { - Lock lock(this); _initiateGFX(_gfxInfo); Config_LoadConfig(); diff --git a/windows/CommonAPIImpl_windows.cpp b/windows/CommonAPIImpl_windows.cpp index 40489b6c..2872d294 100644 --- a/windows/CommonAPIImpl_windows.cpp +++ b/windows/CommonAPIImpl_windows.cpp @@ -14,7 +14,6 @@ BOOL CALLBACK FindToolBarProc( HWND hWnd, LPARAM lParam ) int PluginAPI::InitiateGFX(const GFX_INFO & _gfxInfo) { - Lock lock(this); _initiateGFX(_gfxInfo); hWnd = _gfxInfo.hWnd;