This patch rearranges the code in apr_thread_mutex_lock/unlock
to reduce the number of if-statements per call.  This reduces
the time spent in these functions by about 10%.

--Brian

Index: srclib/apr/locks/unix/thread_mutex.c
===================================================================
RCS file: /home/cvspublic/apr/locks/unix/thread_mutex.c,v
retrieving revision 1.5
diff -u -r1.5 thread_mutex.c
--- srclib/apr/locks/unix/thread_mutex.c        22 Oct 2001 08:30:08 -0000      
1.5
+++ srclib/apr/locks/unix/thread_mutex.c        1 Jan 2002 02:40:46 -0000
@@ -128,26 +128,35 @@
     apr_status_t rv;
 
 #if APR_HAS_THREADS
-    if (mutex->nested && apr_os_thread_equal(mutex->owner,
-                                             apr_os_thread_current())) {
-        mutex->owner_ref++;
-        return APR_SUCCESS;
-    }
-#endif
+    if (mutex->nested) {
+        if (apr_os_thread_equal(mutex->owner, apr_os_thread_current())) {
+            mutex->owner_ref++;
+            return APR_SUCCESS;
+        }
 
-    rv = pthread_mutex_lock(&mutex->mutex);
-    if (rv) {
+        rv = pthread_mutex_lock(&mutex->mutex);
+        if (rv) {
 #ifdef PTHREAD_SETS_ERRNO
-        rv = errno;
+            rv = errno;
 #endif
-        return rv;
-    }
+            return rv;
+        }
 
-#if APR_HAS_THREADS
-    if (mutex->nested) {
         mutex->owner = apr_os_thread_current();
         mutex->owner_ref = 1;
     }
+    else {
+#endif
+        rv = pthread_mutex_lock(&mutex->mutex);
+        if (rv) {
+#ifdef PTHREAD_SETS_ERRNO
+            rv = errno;
+#endif
+            return rv;
+        }
+
+#if APR_HAS_THREADS
+    }
 #endif
 
     return rv;
@@ -194,24 +203,29 @@
             if (mutex->owner_ref > 0)
                 return APR_SUCCESS;
         }
-    }
-#endif
-
-    status = pthread_mutex_unlock(&mutex->mutex);
-    if (status) {
+        status = pthread_mutex_unlock(&mutex->mutex);
+        if (status) {
 #ifdef PTHREAD_SETS_ERRNO
-        status = errno;
+            status = errno;
 #endif
-        return status;
-    }
+            return status;
+        }
 
-#if APR_HAS_THREADS
-    if (mutex->nested) {
         memset(&mutex->owner, 0, sizeof mutex->owner);
         mutex->owner_ref = 0;
     }
+    else {
+#endif
+        status = pthread_mutex_unlock(&mutex->mutex);
+        if (status) {
+#ifdef PTHREAD_SETS_ERRNO
+            status = errno;
+#endif
+            return status;
+        }
+#if APR_HAS_THREADS
+    }
 #endif
-    
     return status;
 }
 

Reply via email to