diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c index 4cfd0f1cd..799e5fb9b 100644 --- a/src/video/wayland/SDL_waylandevents.c +++ b/src/video/wayland/SDL_waylandevents.c @@ -231,11 +231,21 @@ Wayland_PumpEvents(_THIS) keyboard_repeat_handle(&input->keyboard_repeat, now); } + /* If we're trying to dispatch the display in another thread, + * we could trigger a race condition and end up blocking + * in wl_display_dispatch() */ + if (SDL_TryLockMutex(d->display_dispatch_lock)) { + return; + } + if (SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_FALSE, 0)) { err = WAYLAND_wl_display_dispatch(d->display); } else { err = WAYLAND_wl_display_dispatch_pending(d->display); } + + SDL_UnlockMutex(d->display_dispatch_lock); + if (err == -1 && !d->display_disconnected) { /* Something has failed with the Wayland connection -- for example, * the compositor may have shut down and closed its end of the socket, diff --git a/src/video/wayland/SDL_waylandopengles.c b/src/video/wayland/SDL_waylandopengles.c index 23429dbbd..a6cb5027f 100644 --- a/src/video/wayland/SDL_waylandopengles.c +++ b/src/video/wayland/SDL_waylandopengles.c @@ -127,7 +127,8 @@ Wayland_GLES_SwapWindow(_THIS, SDL_Window *window) /* Control swap interval ourselves. See comments on Wayland_GLES_SetSwapInterval */ if (swap_interval != 0) { - struct wl_display *display = ((SDL_VideoData *)_this->driverdata)->display; + SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata; + struct wl_display *display = videodata->display; SDL_VideoDisplay *sdldisplay = SDL_GetDisplayForWindow(window); /* ~10 frames (or 1 sec), so we'll progress even if throttled to zero. */ const Uint32 max_wait = SDL_GetTicks() + (sdldisplay->current_mode.refresh_rate ? @@ -148,12 +149,20 @@ Wayland_GLES_SwapWindow(_THIS, SDL_Window *window) break; } + /* Make sure we're not competing with SDL_PumpEvents() for any new + * events, or one of us may end up blocking in wl_display_dispatch */ + if (SDL_TryLockMutex(videodata->display_dispatch_lock)) { + continue; + } + if (SDL_IOReady(WAYLAND_wl_display_get_fd(display), SDL_FALSE, max_wait - now) <= 0) { /* Error or timeout expired without any events for us */ + SDL_UnlockMutex(videodata->display_dispatch_lock); break; } WAYLAND_wl_display_dispatch(display); + SDL_UnlockMutex(videodata->display_dispatch_lock); } SDL_AtomicSet(&data->swap_interval_ready, 0); } diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c index 03464d193..df915eea6 100644 --- a/src/video/wayland/SDL_waylandvideo.c +++ b/src/video/wayland/SDL_waylandvideo.c @@ -169,6 +169,7 @@ Wayland_DeleteDevice(SDL_VideoDevice *device) WAYLAND_wl_display_flush(data->display); WAYLAND_wl_display_disconnect(data->display); } + SDL_DestroyMutex(data->display_dispatch_lock); SDL_free(data); SDL_free(device); SDL_WAYLAND_UnloadSymbols(); @@ -201,6 +202,8 @@ Wayland_CreateDevice(int devindex) data->display = display; + data->display_dispatch_lock = SDL_CreateMutex(); + /* Initialize all variables that we clean on shutdown */ device = SDL_calloc(1, sizeof(SDL_VideoDevice)); if (!device) { diff --git a/src/video/wayland/SDL_waylandvideo.h b/src/video/wayland/SDL_waylandvideo.h index 2d8b6323a..42da52e0e 100644 --- a/src/video/wayland/SDL_waylandvideo.h +++ b/src/video/wayland/SDL_waylandvideo.h @@ -86,6 +86,7 @@ typedef struct { char *classname; int relative_mouse_mode; + SDL_mutex *display_dispatch_lock; } SDL_VideoData; typedef struct {