apr_os_proc_mutex_put_ex: Allow to specify whether the OS native
mutex should or not be cleaned up (destroyed) with the constructed
APR mutex (given pool), and default to not for the simple _put()
function.


git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1738925 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES b/CHANGES
index 7ab9a5a..46e1cdf 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,11 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 2.0.0
 
+  *) apr_os_proc_mutex_put_ex: Allow to specify whether the OS native
+     mutex should or not be cleaned up (destroyed) with the constructed
+     APR mutex (given pool), and default to not for the simple _put()
+     function.  [Yann Ylavic]
+
   *) apr_file_io: Add apr_file_pipe_create_pools() allowing a pair of
      pipes to be created, each in a different pool. [Graham Leggett]
 
diff --git a/include/apr_portable.h b/include/apr_portable.h
index 7b4c5be..7bb7a35 100644
--- a/include/apr_portable.h
+++ b/include/apr_portable.h
@@ -437,6 +437,8 @@
  * @param pmutex The apr proc mutex we are converting to.
  * @param ospmutex The os specific proc mutex to convert.
  * @param mech The apr mutex locking mechanism
+ * @param register_cleanup Whether to destroy the os mutex with the apr
+ *        one (either on explicit destroy or pool cleanup).
  * @param cont The pool to use if it is needed.
  * @remark Allows for disambiguation for platforms with multiple mechanisms
  *         available.
@@ -444,6 +446,7 @@
 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put_ex(apr_proc_mutex_t **pmutex,
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_lockmech_e mech,
+                                                int register_cleanup,
                                                 apr_pool_t *cont); 
 
 /**
diff --git a/locks/beos/proc_mutex.c b/locks/beos/proc_mutex.c
index d5c6817..1b46871 100644
--- a/locks/beos/proc_mutex.c
+++ b/locks/beos/proc_mutex.c
@@ -224,6 +224,7 @@
 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put_ex(apr_proc_mutex_t **pmutex,
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_lockmech_e mech,
+                                                int register_cleanup,
                                                 apr_pool_t *pool)
 {
     if (pool == NULL) {
@@ -232,12 +233,18 @@
     if (mech != APR_LOCK_DEFAULT && mech != APR_LOCK_DEFAULT_TIMED) {
         return APR_ENOTIMPL;
     }
+
     if ((*pmutex) == NULL) {
         (*pmutex) = (apr_proc_mutex_t *)apr_pcalloc(pool, sizeof(apr_proc_mutex_t));
         (*pmutex)->pool = pool;
     }
     (*pmutex)->Lock = ospmutex->sem;
     (*pmutex)->LockCount = ospmutex->ben;
+
+    if (register_cleanup) {
+        apr_pool_cleanup_register(pool, *pmutex, _proc_mutex_cleanup,
+                                  apr_pool_cleanup_null);
+    }
     return APR_SUCCESS;
 }
 
@@ -245,6 +252,7 @@
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_pool_t *pool)
 {
-    return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT_TIMED, pool);
+    return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT_TIMED,
+                                    0, pool);
 }
 
diff --git a/locks/netware/proc_mutex.c b/locks/netware/proc_mutex.c
index 95dc8f8..581b243 100644
--- a/locks/netware/proc_mutex.c
+++ b/locks/netware/proc_mutex.c
@@ -150,6 +150,7 @@
 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put_ex(apr_proc_mutex_t **pmutex,
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_lockmech_e mech,
+                                                int register_cleanup,
                                                 apr_pool_t *pool)
 {
     if (pool == NULL) {
@@ -166,6 +167,11 @@
     (*pmutex)->mutex = apr_pcalloc(pool, sizeof(apr_thread_mutex_t));
     (*pmutex)->mutex->mutex = *ospmutex;
     (*pmutex)->mutex->pool = pool;
+
+    if (register_cleanup) {
+        apr_pool_cleanup_register(pool, *pmutex, apr_proc_mutex_cleanup,
+                                  apr_pool_cleanup_null);
+    }
     return APR_SUCCESS;
 }
 
@@ -173,6 +179,7 @@
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_pool_t *pool)
 {
-    return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT, pool);
+    return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT,
+                                    0, pool);
 }
 
diff --git a/locks/os2/proc_mutex.c b/locks/os2/proc_mutex.c
index 83f64c8..847f775 100644
--- a/locks/os2/proc_mutex.c
+++ b/locks/os2/proc_mutex.c
@@ -268,6 +268,7 @@
 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put_ex(apr_proc_mutex_t **pmutex,
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_lockmech_e mech,
+                                                int register_cleanup,
                                                 apr_pool_t *pool)
 {
     apr_proc_mutex_t *new;
@@ -285,6 +286,10 @@
     new->hMutex     = *ospmutex;
     *pmutex = new;
 
+    if (register_cleanup) {
+        apr_pool_cleanup_register(pool, *pmutex, apr_proc_mutex_cleanup,
+                                  apr_pool_cleanup_null);
+    }
     return APR_SUCCESS;
 }
 
@@ -292,6 +297,7 @@
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_pool_t *pool)
 {
-    return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT_TIMED, pool);
+    return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT_TIMED,
+                                    0, pool);
 }
 
diff --git a/locks/unix/proc_mutex.c b/locks/unix/proc_mutex.c
index 2edeed3..93df3ad 100644
--- a/locks/unix/proc_mutex.c
+++ b/locks/unix/proc_mutex.c
@@ -1364,12 +1364,14 @@
 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put_ex(apr_proc_mutex_t **pmutex,
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_lockmech_e mech,
+                                                int register_cleanup,
                                                 apr_pool_t *pool)
 {
     apr_status_t rv;
     if (pool == NULL) {
         return APR_ENOPOOL;
     }
+
     if ((*pmutex) == NULL) {
         (*pmutex) = (apr_proc_mutex_t *)apr_pcalloc(pool,
                                                     sizeof(apr_proc_mutex_t));
@@ -1382,6 +1384,11 @@
                              0, pool);
     }
 #endif
+
+    if (rv == APR_SUCCESS && register_cleanup) {
+        apr_pool_cleanup_register(pool, *pmutex, apr_proc_mutex_cleanup, 
+                                  apr_pool_cleanup_null);
+    }
     return rv;
 }
 
@@ -1389,6 +1396,7 @@
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_pool_t *pool)
 {
-    return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT, pool);
+    return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT,
+                                    0, pool);
 }
 
diff --git a/locks/win32/proc_mutex.c b/locks/win32/proc_mutex.c
index 83042ce..9e227f7 100644
--- a/locks/win32/proc_mutex.c
+++ b/locks/win32/proc_mutex.c
@@ -265,6 +265,7 @@
 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put_ex(apr_proc_mutex_t **pmutex,
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_lockmech_e mech,
+                                                int register_cleanup,
                                                 apr_pool_t *pool)
 {
     if (pool == NULL) {
@@ -273,12 +274,18 @@
     if (mech != APR_LOCK_DEFAULT && mech != APR_LOCK_DEFAULT_TIMED) {
         return APR_ENOTIMPL;
     }
+
     if ((*pmutex) == NULL) {
         (*pmutex) = (apr_proc_mutex_t *)apr_palloc(pool,
                                                    sizeof(apr_proc_mutex_t));
         (*pmutex)->pool = pool;
     }
     (*pmutex)->handle = *ospmutex;
+
+    if (register_cleanup) {
+        apr_pool_cleanup_register(pool, *pmutex, proc_mutex_cleanup,
+                                  apr_pool_cleanup_null);
+    }
     return APR_SUCCESS;
 }
 
@@ -286,6 +293,7 @@
                                                 apr_os_proc_mutex_t *ospmutex,
                                                 apr_pool_t *pool)
 {
-    return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT_TIMED, pool);
+    return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT_TIMED,
+                                    0, pool);
 }