Clarifying hard-to-understand piece of code.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 22 Jul 2014 23:12:21 -0400
changeset 90106d059ed9b6ca
parent 9009 ddbca09f8f9d
child 9011 c3591f14ab7e
Clarifying hard-to-understand piece of code.
src/audio/SDL_audio.c
     1.1 --- a/src/audio/SDL_audio.c	Fri Jul 18 14:10:45 2014 -0700
     1.2 +++ b/src/audio/SDL_audio.c	Tue Jul 22 23:12:21 2014 -0400
     1.3 @@ -205,22 +205,34 @@
     1.4      return -1;
     1.5  }
     1.6  
     1.7 +static SDL_INLINE SDL_bool
     1.8 +is_in_audio_device_thread(SDL_AudioDevice * device)
     1.9 +{
    1.10 +    /* The device thread locks the same mutex, but not through the public API.
    1.11 +       This check is in case the application, in the audio callback,
    1.12 +       tries to lock the thread that we've already locked from the
    1.13 +       device thread...just in case we only have non-recursive mutexes. */
    1.14 +    if (device->thread && (SDL_ThreadID() == device->threadid)) {
    1.15 +        return SDL_TRUE;
    1.16 +    }
    1.17 +
    1.18 +    return SDL_FALSE;
    1.19 +}
    1.20 +
    1.21  static void
    1.22  SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
    1.23  {
    1.24 -    if (device->thread && (SDL_ThreadID() == device->threadid)) {
    1.25 -        return;
    1.26 +    if (!is_in_audio_device_thread(device)) {
    1.27 +        SDL_LockMutex(device->mixer_lock);
    1.28      }
    1.29 -    SDL_LockMutex(device->mixer_lock);
    1.30  }
    1.31  
    1.32  static void
    1.33  SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
    1.34  {
    1.35 -    if (device->thread && (SDL_ThreadID() == device->threadid)) {
    1.36 -        return;
    1.37 +    if (!is_in_audio_device_thread(device)) {
    1.38 +        SDL_UnlockMutex(device->mixer_lock);
    1.39      }
    1.40 -    SDL_UnlockMutex(device->mixer_lock);
    1.41  }
    1.42  
    1.43