Add RunOnWndMsgThread to run arbitrary code on the window message thread

This commit is contained in:
Silent 2020-12-04 20:48:36 +01:00
parent 7902c12738
commit 5a8cfaf7f9
No known key found for this signature in database
GPG Key ID: AE53149BB0C45AF1
1 changed files with 18 additions and 0 deletions

View File

@ -71,6 +71,7 @@
#include <assert.h> #include <assert.h>
#include <process.h> #include <process.h>
#include <clocale> #include <clocale>
#include <functional>
#include <unordered_map> #include <unordered_map>
#include <thread> #include <thread>
@ -557,6 +558,16 @@ const char *CxbxGetErrorDescription(HRESULT hResult)
return nullptr; return nullptr;
} }
static constexpr UINT WM_CXBXR_RUN_ON_MESSAGE_THREAD = WM_USER+0;
// A helper function to run any code on a window message thread
// Used for those D3D9 function which *must* be ran on this particular thread
static void RunOnWndMsgThread(const std::function<void()>& func)
{
const void* param = &func;
SendMessage(g_hEmuWindow, WM_CXBXR_RUN_ON_MESSAGE_THREAD, reinterpret_cast<WPARAM>(param), 0);
}
const char *D3DErrorString(HRESULT hResult) const char *D3DErrorString(HRESULT hResult)
{ {
@ -2109,6 +2120,13 @@ static LRESULT WINAPI EmuMsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPar
} }
break; break;
case WM_CXBXR_RUN_ON_MESSAGE_THREAD:
{
auto func = reinterpret_cast<const std::function<void()>*>(wParam);
(*func)();
return 0;
}
default: default:
return DefWindowProc(hWnd, msg, wParam, lParam); return DefWindowProc(hWnd, msg, wParam, lParam);
} }