DisplayServer: Avoid deadlocks while issuing input events and recursive main loop iterations

This commit is contained in:
Pedro J. Estébanez 2024-04-15 18:17:09 +02:00
parent 2042420bd7
commit fc08eca524
3 changed files with 13 additions and 11 deletions

View File

@ -4268,6 +4268,8 @@ bool DisplayServerX11::_window_focus_check() {
}
void DisplayServerX11::process_events() {
ERR_FAIL_COND(!Thread::is_main_thread());
_THREAD_SAFE_LOCK_
#ifdef DISPLAY_SERVER_X11_DEBUG_LOGS_ENABLED

View File

@ -2985,7 +2985,7 @@ Key DisplayServerMacOS::keyboard_get_label_from_physical(Key p_keycode) const {
}
void DisplayServerMacOS::process_events() {
_THREAD_SAFE_LOCK_
ERR_FAIL_COND(!Thread::is_main_thread());
while (true) {
NSEvent *event = [NSApp
@ -3018,11 +3018,11 @@ void DisplayServerMacOS::process_events() {
if (!drop_events) {
_process_key_events();
_THREAD_SAFE_UNLOCK_
Input::get_singleton()->flush_buffered_events();
_THREAD_SAFE_LOCK_
}
_THREAD_SAFE_LOCK_
for (KeyValue<WindowID, WindowData> &E : windows) {
WindowData &wd = E.value;
if (wd.mpass) {
@ -3051,7 +3051,7 @@ void DisplayServerMacOS::process_events() {
}
void DisplayServerMacOS::force_process_and_drop_events() {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!Thread::is_main_thread());
drop_events = true;
process_events();

View File

@ -2962,30 +2962,28 @@ String DisplayServerWindows::keyboard_get_layout_name(int p_index) const {
}
void DisplayServerWindows::process_events() {
_THREAD_SAFE_LOCK_
MSG msg;
ERR_FAIL_COND(!Thread::is_main_thread());
if (!drop_events) {
joypad->process_joypads();
}
_THREAD_SAFE_LOCK_
MSG msg = {};
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
_THREAD_SAFE_UNLOCK_
if (!drop_events) {
_process_key_events();
_THREAD_SAFE_UNLOCK_
Input::get_singleton()->flush_buffered_events();
} else {
_THREAD_SAFE_UNLOCK_
}
}
void DisplayServerWindows::force_process_and_drop_events() {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!Thread::is_main_thread());
drop_events = true;
process_events();
@ -4664,10 +4662,12 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
} break;
case WM_TIMER: {
if (wParam == windows[window_id].move_timer_id) {
_THREAD_SAFE_UNLOCK_
_process_key_events();
if (!Main::is_iterating()) {
Main::iteration();
}
_THREAD_SAFE_LOCK_
} else if (wParam == windows[window_id].activate_timer_id) {
_process_activate_event(window_id);
KillTimer(windows[window_id].hWnd, windows[window_id].activate_timer_id);