Work-around for a hang when USB devices are unplugged, contributed by James Zipperer
authorSam Lantinga <slouken@libsdl.org>
Wed, 12 Oct 2016 22:25:19 -0700
changeset 10523a45d3b96cf85
parent 10522 1cb2f14d0701
child 10524 8ede8e4e5b6f
Work-around for a hang when USB devices are unplugged, contributed by James Zipperer
src/audio/alsa/SDL_alsa_audio.c
     1.1 --- a/src/audio/alsa/SDL_alsa_audio.c	Wed Oct 12 19:50:16 2016 -0700
     1.2 +++ b/src/audio/alsa/SDL_alsa_audio.c	Wed Oct 12 22:25:19 2016 -0700
     1.3 @@ -314,9 +314,20 @@
     1.4      swizzle_alsa_channels(this, this->hidden->mixbuf, frames_left);
     1.5  
     1.6      while ( frames_left > 0 && SDL_AtomicGet(&this->enabled) ) {
     1.7 -        /* !!! FIXME: This works, but needs more testing before going live */
     1.8 -        /* ALSA_snd_pcm_wait(this->hidden->pcm_handle, -1); */
     1.9 -        int status = ALSA_snd_pcm_writei(this->hidden->pcm_handle,
    1.10 +        int status;
    1.11 +
    1.12 +        /* This wait is a work-around for a hang when USB devices are
    1.13 +           unplugged.  Normally it should not result in any waiting,
    1.14 +           but in the case of a USB unplug, it serves as a way to
    1.15 +           join the playback thread after the timeout occurs */
    1.16 +        status = ALSA_snd_pcm_wait(this->hidden->pcm_handle, 1000);
    1.17 +        if (status == 0) {
    1.18 +            /*fprintf(stderr, "ALSA timeout waiting for available buffer space\n");*/
    1.19 +            SDL_OpenedAudioDeviceDisconnected(this);
    1.20 +            return;
    1.21 +        }
    1.22 +
    1.23 +        status = ALSA_snd_pcm_writei(this->hidden->pcm_handle,
    1.24                                           sample_buf, frames_left);
    1.25  
    1.26          if (status < 0) {