From a7df9020de97e360eb49e89089188b093d79bdc7 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <slouken@libsdl.org>
Date: Fri, 3 Feb 2006 06:33:54 +0000
Subject: [PATCH] Favor using pthread_mutexattr_settype() on Linux.

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%401327
---
 configure.in                    | 49 ++++++++++++++++++---------------
 src/thread/linux/SDL_sysmutex.c | 12 ++++----
 2 files changed, 32 insertions(+), 29 deletions(-)

diff --git a/configure.in b/configure.in
index 42a39d0b..a77c7d14 100644
--- a/configure.in
+++ b/configure.in
@@ -1201,6 +1201,10 @@ CheckPTHREAD()
 [  --enable-pthread-sem    use pthread semaphores [default=yes]],
                   , enable_pthread_sem=yes)
     case "$target" in
+        *-*-linux*)
+            pthread_cflags="-D_REENTRANT -D_GNU_SOURCE"
+            pthread_lib="-lpthread"
+            ;;
         *-*-bsdi*)
             pthread_cflags="-D_REENTRANT -D_THREAD_SAFE"
             pthread_lib=""
@@ -1288,29 +1292,30 @@ CheckPTHREAD()
             # Check to see if recursive mutexes are available
             AC_MSG_CHECKING(for recursive mutexes)
             has_recursive_mutexes=no
-            AC_TRY_LINK([
-              #include <pthread.h>
-            ],[
-              pthread_mutexattr_t attr;
-              #if defined(linux) && !(defined(__arm__) && defined(QWS))
-              pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
-              #else
-              pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
-              #endif
-            ],[
-            has_recursive_mutexes=yes
-            ])
-            # Some systems have broken recursive mutex implementations
-            case "$target" in
-                *-*-darwin*)
-                    has_recursive_mutexes=no
-                    ;;
-                *-*-solaris*)
-                    has_recursive_mutexes=no
-                    ;;
-            esac
+            if test x$has_recursive_mutexes = xno; then
+                AC_TRY_COMPILE([
+                  #include <pthread.h>
+                ],[
+                  pthread_mutexattr_t attr;
+                  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+                ],[
+                has_recursive_mutexes=yes
+                CFLAGS="$CFLAGS -DPTHREAD_RECURSIVE_MUTEX"
+                ])
+            fi
+            if test x$has_recursive_mutexes = xno; then
+                AC_TRY_COMPILE([
+                  #include <pthread.h>
+                ],[
+                  pthread_mutexattr_t attr;
+                  pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
+                ],[
+                has_recursive_mutexes=yes
+                CFLAGS="$CFLAGS -DPTHREAD_RECURSIVE_MUTEX_NP"
+                ])
+            fi
             AC_MSG_RESULT($has_recursive_mutexes)
-            if test x$has_recursive_mutexes != xyes; then
+            if test x$has_recursive_mutexes = xno; then
                 CFLAGS="$CFLAGS -DPTHREAD_NO_RECURSIVE_MUTEX"
             fi
 
diff --git a/src/thread/linux/SDL_sysmutex.c b/src/thread/linux/SDL_sysmutex.c
index 4c6422d2..3a4ef738 100644
--- a/src/thread/linux/SDL_sysmutex.c
+++ b/src/thread/linux/SDL_sysmutex.c
@@ -62,15 +62,13 @@ SDL_mutex *SDL_CreateMutex (void)
 	mutex = (SDL_mutex *)calloc(1, sizeof(*mutex));
 	if ( mutex ) {
 		pthread_mutexattr_init(&attr);
-#ifdef PTHREAD_NO_RECURSIVE_MUTEX
-		/* No extra attributes necessary */
-#else
-#ifdef linux
+#if defined(PTHREAD_RECURSIVE_MUTEX)
+		pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+#elif defined(PTHREAD_RECURSIVE_MUTEX_NP)
 		pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
 #else
-		pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
-#endif
-#endif /* PTHREAD_NO_RECURSIVE_MUTEX */
+		/* No extra attributes necessary */
+#endif /* PTHREAD_RECURSIVE_MUTEX */
 		if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) {
 			SDL_SetError("pthread_mutex_init() failed");
 			free(mutex);
-- 
2.18.1