You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by yl...@apache.org on 2020/11/25 00:01:33 UTC

svn commit: r1883806 - /apr/apr/trunk/memory/unix/apr_pools.c

Author: ylavic
Date: Wed Nov 25 00:01:32 2020
New Revision: 1883806

URL: http://svn.apache.org/viewvc?rev=1883806&view=rev
Log:
apr_pools: add and use pool_[un]lock() and parent_[un]lock() helpers.

To avoid #ifdef'ery in relevant code and thus help readers.

Modified:
    apr/apr/trunk/memory/unix/apr_pools.c

Modified: apr/apr/trunk/memory/unix/apr_pools.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/memory/unix/apr_pools.c?rev=1883806&r1=1883805&r2=1883806&view=diff
==============================================================================
--- apr/apr/trunk/memory/unix/apr_pools.c (original)
+++ apr/apr/trunk/memory/unix/apr_pools.c Wed Nov 25 00:01:32 2020
@@ -1467,6 +1467,41 @@ error:
  * Debug helper functions
  */
 
+static APR_INLINE
+void pool_lock(apr_pool_t *pool)
+{
+#if APR_HAS_THREADS
+    apr_thread_mutex_lock(pool->mutex);
+#endif /* APR_HAS_THREADS */
+}
+
+static APR_INLINE
+void pool_unlock(apr_pool_t *pool)
+{
+#if APR_HAS_THREADS
+    apr_thread_mutex_unlock(pool->mutex);
+#endif /* APR_HAS_THREADS */
+}
+
+#if APR_HAS_THREADS
+static APR_INLINE
+apr_thread_mutex_t *parent_lock(apr_pool_t *pool)
+{
+    if (pool->parent) {
+        apr_thread_mutex_lock(pool->parent->mutex);
+        return pool->parent->mutex;
+    }
+    return NULL;
+}
+
+static APR_INLINE
+void parent_unlock(apr_thread_mutex_t *mutex)
+{
+    if (mutex) {
+        apr_thread_mutex_unlock(mutex);
+    }
+}
+#endif /* APR_HAS_THREADS */
 
 /*
  * Walk the pool tree rooted at pool, depth first.  When fn returns
@@ -1484,9 +1519,7 @@ static int apr_pool_walk_tree(apr_pool_t
     if (rv)
         return rv;
 
-#if APR_HAS_THREADS
-    apr_thread_mutex_lock(pool->mutex);
-#endif /* APR_HAS_THREADS */
+    pool_lock(pool);
 
     child = pool->child;
     while (child) {
@@ -1497,9 +1530,7 @@ static int apr_pool_walk_tree(apr_pool_t
         child = child->sibling;
     }
 
-#if APR_HAS_THREADS
-    apr_thread_mutex_unlock(pool->mutex);
-#endif /* APR_HAS_THREADS */
+    pool_unlock(pool);
 
     return rv;
 }
@@ -1889,7 +1920,7 @@ APR_DECLARE(void) apr_pool_clear_debug(a
                                        const char *file_line)
 {
 #if APR_HAS_THREADS
-    apr_thread_mutex_t *mutex = NULL;
+    apr_thread_mutex_t *mutex;
 #endif
 
     apr_pool_check_lifetime(pool);
@@ -1899,10 +1930,7 @@ APR_DECLARE(void) apr_pool_clear_debug(a
      * own mutex it won't be accessed by apr_pool_walk_tree after
      * it has been destroyed.
      */
-    if (pool->parent != NULL) {
-        mutex = pool->parent->mutex;
-        apr_thread_mutex_lock(mutex);
-    }
+    mutex = parent_lock(pool);
 #endif
 
 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
@@ -1913,8 +1941,7 @@ APR_DECLARE(void) apr_pool_clear_debug(a
 
 #if APR_HAS_THREADS
     /* If we had our own mutex, it will have been destroyed by
-     * the registered cleanups.  Recreate the mutex.  Unlock
-     * the mutex we obtained above.
+     * the registered cleanups.  Recreate it.
      */
     if (mutex != pool->mutex) {
         /*
@@ -1925,9 +1952,9 @@ APR_DECLARE(void) apr_pool_clear_debug(a
         (void)apr_thread_mutex_create(&pool->mutex,
                                       APR_THREAD_MUTEX_NESTED, pool);
     }
-    if (mutex != NULL) {
-        apr_thread_mutex_unlock(mutex);
-    }
+
+    /* Unlock the mutex we obtained above */
+    parent_unlock(mutex);
 #endif /* APR_HAS_THREADS */
 }
 
@@ -1955,7 +1982,7 @@ APR_DECLARE(void) apr_pool_destroy_debug
                                          const char *file_line)
 {
 #if APR_HAS_THREADS
-    apr_thread_mutex_t *mutex = NULL;
+    apr_thread_mutex_t *mutex;
 #endif
 
     apr_pool_check_lifetime(pool);
@@ -1975,10 +2002,7 @@ APR_DECLARE(void) apr_pool_destroy_debug
     /* Lock the parent mutex before destroying so that it's not accessed
      * concurrently by apr_pool_walk_tree.
      */
-    if (pool->parent != NULL) {
-        mutex = pool->parent->mutex;
-        apr_thread_mutex_lock(mutex);
-    }
+    mutex = parent_lock(pool);
 #endif
 
 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
@@ -1989,9 +2013,7 @@ APR_DECLARE(void) apr_pool_destroy_debug
 
 #if APR_HAS_THREADS
     /* Unlock the mutex we obtained above */
-    if (mutex != NULL) {
-        apr_thread_mutex_unlock(mutex);
-    }
+    parent_unlock(mutex);
 #endif /* APR_HAS_THREADS */
 }
 
@@ -2056,9 +2078,7 @@ APR_DECLARE(apr_status_t) apr_pool_creat
 #endif /* APR_HAS_THREADS */
 
     if ((pool->parent = parent) != NULL) {
-#if APR_HAS_THREADS
-        apr_thread_mutex_lock(parent->mutex);
-#endif /* APR_HAS_THREADS */
+        pool_lock(parent);
 
         if ((pool->sibling = parent->child) != NULL)
             pool->sibling->ref = &pool->sibling;
@@ -2066,9 +2086,7 @@ APR_DECLARE(apr_status_t) apr_pool_creat
         parent->child = pool;
         pool->ref = &parent->child;
 
-#if APR_HAS_THREADS
-        apr_thread_mutex_unlock(parent->mutex);
-#endif /* APR_HAS_THREADS */
+        pool_unlock(parent);
     }
     else {
         pool->sibling = NULL;