Clarifying hard-to-understand piece of code.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 22 Jul 2014 23:12:21 -0400
changeset 9010 6d059ed9b6ca
parent 9009 ddbca09f8f9d
child 9011 c3591f14ab7e
Clarifying hard-to-understand piece of code.
src/audio/SDL_audio.c
--- a/src/audio/SDL_audio.c	Fri Jul 18 14:10:45 2014 -0700
+++ b/src/audio/SDL_audio.c	Tue Jul 22 23:12:21 2014 -0400
@@ -205,22 +205,34 @@
     return -1;
 }
 
+static SDL_INLINE SDL_bool
+is_in_audio_device_thread(SDL_AudioDevice * device)
+{
+    /* The device thread locks the same mutex, but not through the public API.
+       This check is in case the application, in the audio callback,
+       tries to lock the thread that we've already locked from the
+       device thread...just in case we only have non-recursive mutexes. */
+    if (device->thread && (SDL_ThreadID() == device->threadid)) {
+        return SDL_TRUE;
+    }
+
+    return SDL_FALSE;
+}
+
 static void
 SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
 {
-    if (device->thread && (SDL_ThreadID() == device->threadid)) {
-        return;
+    if (!is_in_audio_device_thread(device)) {
+        SDL_LockMutex(device->mixer_lock);
     }
-    SDL_LockMutex(device->mixer_lock);
 }
 
 static void
 SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
 {
-    if (device->thread && (SDL_ThreadID() == device->threadid)) {
-        return;
+    if (!is_in_audio_device_thread(device)) {
+        SDL_UnlockMutex(device->mixer_lock);
     }
-    SDL_UnlockMutex(device->mixer_lock);
 }