Replaced manual management of recursive pthread mutexes with
authorRyan C. Gordon <icculus@icculus.org>
Wed, 21 Mar 2007 04:31:13 +0000
changeset 846 df684fce470e
parent 845 3f150ffcf50c
child 847 5e5e6c067413
Replaced manual management of recursive pthread mutexes with PTHREAD_MUTEX_RECURSIVE.
platform/unix.c
--- a/platform/unix.c	Tue Mar 20 18:33:56 2007 +0000
+++ b/platform/unix.c	Wed Mar 21 04:31:13 2007 +0000
@@ -477,6 +477,7 @@
 } /* __PHYSFS_platformSetDefaultAllocator */
 
 
+
 #if (defined PHYSFS_NO_PTHREADS_SUPPORT)
 
 PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) { return(0x0001); }
@@ -487,13 +488,6 @@
 
 #else
 
-typedef struct
-{
-    pthread_mutex_t mutex;
-    pthread_t owner;
-    PHYSFS_uint32 count;
-} PthreadMutex;
-
 /* Just in case; this is a panic value. */
 #if ((!defined SIZEOF_INT) || (SIZEOF_INT <= 0))
 #  define SIZEOF_INT 4
@@ -517,62 +511,56 @@
 
 void *__PHYSFS_platformCreateMutex(void)
 {
+    pthread_mutex_t *m = NULL;
+    pthread_mutexattr_t attr;
     int rc;
-    PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
-    BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
-    rc = pthread_mutex_init(&m->mutex, NULL);
+
+    rc = pthread_mutexattr_init(&attr);
+    if (rc == 0)
+    {
+        rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+        if (rc == 0)
+        {
+            m = (pthread_mutex_t *) allocator.Malloc(sizeof (pthread_mutex_t));
+            if (m == NULL)
+                rc = ENOMEM;
+            else
+                rc = pthread_mutex_init(m, &attr);
+        } /* if */
+        pthread_mutexattr_destroy(&attr);
+    } /* if */
+
     if (rc != 0)
     {
         allocator.Free(m);
         BAIL_MACRO(strerror(rc), NULL);
     } /* if */
 
-    m->count = 0;
-    m->owner = (pthread_t) 0xDEADBEEF;
     return((void *) m);
 } /* __PHYSFS_platformCreateMutex */
 
 
 void __PHYSFS_platformDestroyMutex(void *mutex)
 {
-    PthreadMutex *m = (PthreadMutex *) mutex;
-
-    /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
-    if ((m->owner == pthread_self()) && (m->count > 0))
-        pthread_mutex_unlock(&m->mutex);
-
-    pthread_mutex_destroy(&m->mutex);
+    pthread_mutex_t *m = (pthread_mutex_t *) mutex;
+    pthread_mutex_destroy(m);
     allocator.Free(m);
 } /* __PHYSFS_platformDestroyMutex */
 
 
 int __PHYSFS_platformGrabMutex(void *mutex)
 {
-    PthreadMutex *m = (PthreadMutex *) mutex;
-    pthread_t tid = pthread_self();
-    if (m->owner != tid)
-    {
-        if (pthread_mutex_lock(&m->mutex) != 0)
-            return(0);
-        m->owner = tid;
-    } /* if */
-
-    m->count++;
+    pthread_mutex_t *m = (pthread_mutex_t *) mutex;
+    if (pthread_mutex_lock(m) != 0)
+        return(0);
     return(1);
 } /* __PHYSFS_platformGrabMutex */
 
 
 void __PHYSFS_platformReleaseMutex(void *mutex)
 {
-    PthreadMutex *m = (PthreadMutex *) mutex;
-    if (m->owner == pthread_self())
-    {
-        if (--m->count == 0)
-        {
-            m->owner = (pthread_t) 0xDEADBEEF;
-            pthread_mutex_unlock(&m->mutex);
-        } /* if */
-    } /* if */
+    pthread_mutex_t *m = (pthread_mutex_t *) mutex;
+    pthread_mutex_unlock(m);
 } /* __PHYSFS_platformReleaseMutex */
 
 #endif /* !PHYSFS_NO_PTHREADS_SUPPORT */