Skip to content

x11: Use XCheckIfEvent() instead of XNextEvent() for thread-safety#4934

Merged
slouken merged 1 commit into
libsdl-org:mainfrom
cgutman:x11_checkif
Nov 9, 2021
Merged

x11: Use XCheckIfEvent() instead of XNextEvent() for thread-safety#4934
slouken merged 1 commit into
libsdl-org:mainfrom
cgutman:x11_checkif

Conversation

@cgutman
Copy link
Copy Markdown
Collaborator

@cgutman cgutman commented Nov 9, 2021

Description

A racing reader could read from our fd between SDL_IOReady()/X11_Pending() and our call to XNextEvent() which will cause XNextEvent() to block for more data. Avoid this by using XCheckIfEvent() which will never block.

This approach is based on that from rust-windowing/winit#782 and glfw/glfw@3d6221c

This also fixes a bug where we could poll() for data, even when events were already read and pending in the queue. Unlike the Wayland implementation, this isn't totally thread-safe because nothing prevents a racing reader from reading events into the queue between our XCheckIfEvent() and SDL_IOReady() calls, but I think this is the best we can do with Xlib.

Existing Issue(s)

#4928

A racing reader could read from our fd between SDL_IOReady()/X11_Pending()
and our call to XNextEvent() which will cause XNextEvent() to block for
more data. Avoid this by using XCheckIfEvent() which will never block.

This also fixes a bug where we could poll() for data, even when events were
already read and pending in the queue. Unlike the Wayland implementation,
this isn't totally thread-safe because nothing prevents a racing reader
from reading events into the queue between our XCheckIfEvent() and
SDL_IOReady() calls, but I think this is the best we can do with Xlib.
@slouken slouken merged commit 9c95c24 into libsdl-org:main Nov 9, 2021
@cgutman cgutman deleted the x11_checkif branch November 24, 2021 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants