On Wed, 2009-06-10 at 11:13 +1000, Bojan Smojver wrote:
> Just for the record.

Or, maybe even this. Compiles and passes make check (surprisingly!).

-- 
Bojan
Index: misc/apu_dso.c
===================================================================
--- misc/apu_dso.c	(revision 783165)
+++ misc/apu_dso.c	(working copy)
@@ -27,6 +27,7 @@
 #include "apr_hash.h"
 #include "apr_file_io.h"
 #include "apr_env.h"
+#include "apr_atomic.h"
 
 #include "apu_internal.h"
 #include "apu_version.h"
@@ -37,6 +38,7 @@
 static apr_thread_mutex_t* mutex = NULL;
 #endif
 static apr_hash_t *dsos = NULL;
+static apr_uint32_t initialised = 0, in_init = 1;
 
 #if APR_HAS_THREADS
 apr_status_t apu_dso_mutex_lock()
@@ -76,7 +78,12 @@
     apr_pool_t *global;
     apr_pool_t *parent;
 
-    if (dsos != NULL) {
+    if (apr_atomic_inc32(&initialised)) {
+        apr_atomic_set32(&initialised, 1); /* prevent wrap-around */
+
+        while (apr_atomic_read32(&in_init)) /* wait until we get fully inited */
+            ;
+
         return APR_SUCCESS;
     }
 
@@ -94,6 +101,8 @@
     apr_pool_cleanup_register(global, NULL, apu_dso_term,
                               apr_pool_cleanup_null);
 
+    apr_atomic_dec32(&in_init);
+
     return ret;
 }
 
Index: dbm/apr_dbm.c
===================================================================
--- dbm/apr_dbm.c	(revision 783165)
+++ dbm/apr_dbm.c	(working copy)
@@ -24,6 +24,7 @@
 #define APR_WANT_STRFUNC
 #include "apr_want.h"
 #include "apr_general.h"
+#include "apr_atomic.h"
 
 #include "apu_config.h"
 #include "apu.h"
@@ -59,6 +60,7 @@
 #if APU_DSO_BUILD
 
 static apr_hash_t *drivers = NULL;
+static apr_uint32_t initialised = 0, in_init = 1;
 
 static apr_status_t dbm_term(void *ptr)
 {
@@ -117,8 +119,13 @@
     }
     else usertype = 1;
 
-    if (!drivers)
-    {
+    if (apr_atomic_inc32(&initialised)) {
+        apr_atomic_set32(&initialised, 1); /* prevent wrap-around */
+
+        while (apr_atomic_read32(&in_init)) /* wait until we get fully inited */
+            ;
+    }
+    else {
         apr_pool_t *parent;
 
         /* Top level pool scope, need process-scope lifetime */
@@ -133,6 +140,8 @@
 
         apr_pool_cleanup_register(pool, NULL, dbm_term,
                                   apr_pool_cleanup_null);
+
+        apr_atomic_dec32(&in_init);
     }
 
     rv = apu_dso_mutex_lock();

Reply via email to