From a618ea0746608603e106de7842088de06078a314 Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Tue, 20 Oct 2020 18:55:02 +0000 Subject: [PATCH] sdlwindow: work around SDL_Init race and std::mutex abuse $ gamescope vkcube-wayland [...] [../src/wlserver.cpp:502] Running compositor on wayland display 'gamescope-0' [backend/headless/backend.c:21] Starting headless backend [xwayland/server.c:94] WAYLAND_SOCKET=10 Xwayland :1 -rootless -terminate -listen 8 -listen 9 -wm -1 Process 78718 stopped * thread #1, name = 'gamescope', stop reason = signal SIGABRT frame #0: 0x0000000800a2667a libc.so.7`__sys_thr_kill at thr_kill.S:4 (lldb) bt * thread #1, name = 'gamescope', stop reason = signal SIGABRT * frame #0: 0x0000000800a2667a libc.so.7`__sys_thr_kill at thr_kill.S:4 frame #1: 0x0000000800992cc4 libc.so.7`__raise(s=6) at raise.c:52:10 frame #2: 0x0000000800a51f59 libc.so.7`abort at abort.c:67:8 frame #3: 0x000000080088b939 libcxxrt.so.1`report_failure(err=, thrown_exception=0x00000008048f51c8) at exception.cc:719:5 frame #4: 0x0000000800860c8c libc++.so.1`std::__1::__throw_system_error(ev=11, what_arg="mutex lock failed") at system_error.cpp:287:5 frame #5: 0x00000008008541cd libc++.so.1`std::__1::mutex::lock(this=) at mutex.cpp:35:9 frame #6: 0x0000000000242ffa gamescope`sdlwindow_init() at sdlwindow.cpp:220:16 frame #7: 0x0000000000236dc2 gamescope`initOutput() at main.cpp:208:10 frame #8: 0x0000000000236d35 gamescope`main(argc=2, argv=0x00007fffffffe0f0) at main.cpp:182:7 frame #9: 0x000000000022227f gamescope`_start(ap=, cleanup=) at crt1_c.c:75:7 (lldb) f 6 frame #6: 0x0000000000242ffa gamescope`sdlwindow_init() at sdlwindow.cpp:220:16 217 inputSDLThread.detach(); 218 219 // When this returns SDL_Init should be over -> 220 g_SDLInitLock.lock(); 221 222 return g_bSDLInitOK; 223 } --- src/sdlwindow.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/sdlwindow.cpp b/src/sdlwindow.cpp index 5a47ff8403..bba9ec8492 100644 --- a/src/sdlwindow.cpp +++ b/src/sdlwindow.cpp @@ -206,8 +206,12 @@ bool sdlwindow_init( void ) std::thread inputSDLThread( inputSDLThreadRun ); inputSDLThread.detach(); + // Other threads cannot unlock std::mutex, so wait for SDL_Init + using namespace std::chrono_literals; + std::this_thread::sleep_for(1s); + // When this returns SDL_Init should be over - g_SDLInitLock.lock(); + g_SDLInitLock.unlock(); return g_bSDLInitOK; }