From c40c31fbb7bef62adcbdb39964906447cba23c57 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sat, 19 Apr 2025 21:12:38 +0300 Subject: [PATCH] Reduce lock usage during window events --- .../Editor/Managed/ManagedEditor.Internal.cpp | 4 ++-- Source/Engine/Input/Input.cpp | 4 +--- .../Platform/SDL/SDLPlatform.Windows.cpp | 20 +------------------ Source/Engine/Platform/SDL/SDLWindow.cpp | 2 +- 4 files changed, 5 insertions(+), 25 deletions(-) diff --git a/Source/Editor/Managed/ManagedEditor.Internal.cpp b/Source/Editor/Managed/ManagedEditor.Internal.cpp index 9ae0b71e9..2220fb0f3 100644 --- a/Source/Editor/Managed/ManagedEditor.Internal.cpp +++ b/Source/Editor/Managed/ManagedEditor.Internal.cpp @@ -402,10 +402,11 @@ DEFINE_INTERNAL_CALL(void) EditorInternal_RunVisualScriptBreakpointLoopTick(floa break; } } + WindowsManager::WindowsLocker.Unlock(); for (const auto& e : inputEvents) { auto window = e.Target ? e.Target : defaultWindow; - if (!window) + if (!window || window->IsClosed()) continue; switch (e.Type) { @@ -443,7 +444,6 @@ DEFINE_INTERNAL_CALL(void) EditorInternal_RunVisualScriptBreakpointLoopTick(floa break; } } - WindowsManager::WindowsLocker.Unlock(); } WindowsManager::WindowsLocker.Lock(); Array> windows; diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index 3cd69e490..b2850b2d5 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -913,11 +913,10 @@ void InputService::Update() WindowsManager::WindowsLocker.Unlock(); // Send input events for the focused window - WindowsManager::WindowsLocker.Lock(); for (const auto& e : InputEvents) { auto window = e.Target ? e.Target : defaultWindow; - if (!window || !WindowsManager::Windows.Contains(window)) + if (!window || window->IsClosed()) continue; switch (e.Type) { @@ -965,7 +964,6 @@ void InputService::Update() break; } } - WindowsManager::WindowsLocker.Unlock(); // Skip if game has no focus to handle the input if (!Engine::HasGameViewportFocus()) diff --git a/Source/Engine/Platform/SDL/SDLPlatform.Windows.cpp b/Source/Engine/Platform/SDL/SDLPlatform.Windows.cpp index dfe6642f9..a805d51f5 100644 --- a/Source/Engine/Platform/SDL/SDLPlatform.Windows.cpp +++ b/Source/Engine/Platform/SDL/SDLPlatform.Windows.cpp @@ -32,26 +32,9 @@ namespace WinImpl // The events for releasing the mouse during window dragging are missing, handle the mouse release event here bool SDLCALL SDLPlatform::EventMessageHook(void* userdata, MSG* msg) { -#define GET_WINDOW_WITH_HWND(window, hwnd) \ - do { \ - (window) = nullptr; \ - WindowsManager::WindowsLocker.Lock(); \ - for (int32 i = 0; i < WindowsManager::Windows.Count(); i++) \ - { \ - if (WindowsManager::Windows[i]->GetNativePtr() == (hwnd)) \ - { \ - (window) = WindowsManager::Windows[i]; \ - break; \ - } \ - } \ - WindowsManager::WindowsLocker.Unlock(); \ - ASSERT((window) != nullptr); \ - } while (false) - if (msg->message == WM_NCLBUTTONDOWN) { - Window* window; - GET_WINDOW_WITH_HWND(window, msg->hwnd); + Window* window = WindowsManager::GetByNativePtr(msg->hwnd); Float2 mousePosition(static_cast(static_cast(WINDOWS_GET_X_LPARAM(msg->lParam))), static_cast(static_cast(WINDOWS_GET_Y_LPARAM(msg->lParam)))); WinImpl::DraggedWindow = window; @@ -81,7 +64,6 @@ bool SDLCALL SDLPlatform::EventMessageHook(void* userdata, MSG* msg) } } return true; -#undef GET_WINDOW_WITH_HWND } bool SDLPlatform::InitInternal() diff --git a/Source/Engine/Platform/SDL/SDLWindow.cpp b/Source/Engine/Platform/SDL/SDLWindow.cpp index 3f34f030a..0d801c063 100644 --- a/Source/Engine/Platform/SDL/SDLWindow.cpp +++ b/Source/Engine/Platform/SDL/SDLWindow.cpp @@ -589,7 +589,7 @@ void SDLWindow::Restore() bool SDLWindow::IsClosed() const { - return _handle == nullptr; + return WindowBase::IsClosed() || _handle == nullptr; } bool SDLWindow::IsForegroundWindow() const