Skip to content

Commit 870e567

Browse files
committed
Move SDL joystick scanning off the main thread
Setting SDL_HINT_JOYSTICK_THREAD=1 before SDL_InitSubSystem(SDL_INIT_JOYSTICK) tells SDL2 to run joystick polling and HID device-detection on a dedicated background thread, rather than processing them inline inside SDL_PollEvent on the main thread. Without this hint, every WM_DEVICECHANGE broadcast (which Windows sends when *any* HID device wakes, sleeps, or pulses the USB bus) causes SDL to walk every HID device class via SetupAPI / cfgmgr32 from inside SDL_PollEvent, stalling the frame loop for multiple seconds on machines with many HID peripherals - especially wireless Logitech mice and keyboards that periodically pulse the bus. Diagnosed via ProcMon stack capture on one affected user's machine, with the slow frame's call chain ending in SDL2.dll -> SETUPAPI.dll -> cfgmgr32.dll -> kernel registry queries against HKLM\System\CurrentControlSet\Enum\HID\*. With the hint enabled, that work happens on SDL's own thread and the main thread just dequeues already-collected events.
1 parent f831251 commit 870e567

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

code/io/joy-sdl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,13 @@ namespace joystick
10361036

10371037
mprintf(("Initializing Joystick...\n"));
10381038

1039+
// Run joystick polling and HID device-detection on a dedicated SDL thread
1040+
// instead of the main thread. Without this, WM_DEVICECHANGE broadcasts
1041+
// (caused by wireless HID peripherals waking/sleeping on the USB bus)
1042+
// trigger SDL to re-walk every HID device class via SetupAPI / cfgmgr32
1043+
// from inside SDL_PollEvent, stalling the frame loop for multiple seconds.
1044+
SDL_SetHint(SDL_HINT_JOYSTICK_THREAD, "1");
1045+
10391046
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
10401047
{
10411048
mprintf((" Could not initialize joystick: %s\n", SDL_GetError()));

0 commit comments

Comments
 (0)