Fixed bug 4391 - hid_enumerate() sometimes causes game to freeze for a few seconds
Daniel Gibson
Even though my game (dhewm3) doesn't use SDL_INIT_JOYSTICK, SDL_PumpEvent() calls SDL_JoystickUpdate() which ends up calling hid_enumerate() every three seconds, and sometimes on my Win7 box hid_enumerate() takes about 5 seconds, which causes the whole game to freeze for that time.
1.1 --- a/include/SDL_bits.h Sun Nov 18 19:28:20 2018 +0300
1.2 +++ b/include/SDL_bits.h Mon Nov 19 21:17:00 2018 -0800
1.3 @@ -101,6 +101,15 @@
1.4 #endif
1.5 }
1.6
1.7 +SDL_FORCE_INLINE SDL_bool
1.8 +SDL_HasExactlyOneBitSet32(Uint32 x)
1.9 +{
1.10 + if (x && !(x & (x - 1))) {
1.11 + return SDL_TRUE;
1.12 + }
1.13 + return SDL_FALSE;
1.14 +}
1.15 +
1.16 /* Ends C function definitions when using C++ */
1.17 #ifdef __cplusplus
1.18 }
2.1 --- a/src/SDL.c Sun Nov 18 19:28:20 2018 +0300
2.2 +++ b/src/SDL.c Mon Nov 19 21:17:00 2018 -0800
2.3 @@ -348,6 +348,12 @@
2.4 int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
2.5 Uint32 initialized = 0;
2.6
2.7 + /* Fast path for checking one flag */
2.8 + if (SDL_HasExactlyOneBitSet32(flags)) {
2.9 + int subsystem_index = SDL_MostSignificantBitIndex32(flags);
2.10 + return SDL_SubsystemRefCount[subsystem_index] ? flags : 0;
2.11 + }
2.12 +
2.13 if (!flags) {
2.14 flags = SDL_INIT_EVERYTHING;
2.15 }
3.1 --- a/src/joystick/SDL_joystick.c Sun Nov 18 19:28:20 2018 +0300
3.2 +++ b/src/joystick/SDL_joystick.c Mon Nov 19 21:17:00 2018 -0800
3.3 @@ -1016,6 +1016,10 @@
3.4 int i;
3.5 SDL_Joystick *joystick;
3.6
3.7 + if (!SDL_WasInit(SDL_INIT_JOYSTICK)) {
3.8 + return;
3.9 + }
3.10 +
3.11 SDL_LockJoysticks();
3.12
3.13 if (SDL_updating_joystick) {
4.1 --- a/src/sensor/SDL_sensor.c Sun Nov 18 19:28:20 2018 +0300
4.2 +++ b/src/sensor/SDL_sensor.c Mon Nov 19 21:17:00 2018 -0800
4.3 @@ -505,6 +505,10 @@
4.4 int i;
4.5 SDL_Sensor *sensor;
4.6
4.7 + if (!SDL_WasInit(SDL_INIT_SENSOR)) {
4.8 + return;
4.9 + }
4.10 +
4.11 SDL_LockSensors();
4.12
4.13 if (SDL_updating_sensor) {