Skip to content

Commit

Permalink
Fixed bug 4391 - hid_enumerate() sometimes causes game to freeze for …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
slouken committed Nov 20, 2018
1 parent d3fa42b commit b73703b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/SDL_bits.h
Expand Up @@ -101,6 +101,15 @@ SDL_MostSignificantBitIndex32(Uint32 x)
#endif
}

SDL_FORCE_INLINE SDL_bool
SDL_HasExactlyOneBitSet32(Uint32 x)
{
if (x && !(x & (x - 1))) {
return SDL_TRUE;
}
return SDL_FALSE;
}

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
Expand Down
6 changes: 6 additions & 0 deletions src/SDL.c
Expand Up @@ -348,6 +348,12 @@ SDL_WasInit(Uint32 flags)
int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
Uint32 initialized = 0;

/* Fast path for checking one flag */
if (SDL_HasExactlyOneBitSet32(flags)) {
int subsystem_index = SDL_MostSignificantBitIndex32(flags);
return SDL_SubsystemRefCount[subsystem_index] ? flags : 0;
}

if (!flags) {
flags = SDL_INIT_EVERYTHING;
}
Expand Down
4 changes: 4 additions & 0 deletions src/joystick/SDL_joystick.c
Expand Up @@ -1016,6 +1016,10 @@ SDL_JoystickUpdate(void)
int i;
SDL_Joystick *joystick;

if (!SDL_WasInit(SDL_INIT_JOYSTICK)) {
return;
}

SDL_LockJoysticks();

if (SDL_updating_joystick) {
Expand Down
4 changes: 4 additions & 0 deletions src/sensor/SDL_sensor.c
Expand Up @@ -505,6 +505,10 @@ SDL_SensorUpdate(void)
int i;
SDL_Sensor *sensor;

if (!SDL_WasInit(SDL_INIT_SENSOR)) {
return;
}

SDL_LockSensors();

if (SDL_updating_sensor) {
Expand Down

0 comments on commit b73703b

Please sign in to comment.