Attempt to resolve pool issues on CentOS and REHL

git-svn-id: https://svn.apache.org/repos/asf/activemq/activemq-cpp/branches/pools-revamp@705899 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/Makefile.am b/src/main/Makefile.am
index f728031..51f72a2 100644
--- a/src/main/Makefile.am
+++ b/src/main/Makefile.am
@@ -81,7 +81,7 @@
     activemq/transport/filters/TcpTransportFactory.cpp \
     activemq/transport/filters/LoggingTransport.cpp \
     activemq/transport/filters/LoggingTransportFactory.cpp \
-    decaf/internal/AprRuntime.cpp \
+    decaf/internal/DecafRuntime.cpp \
     decaf/internal/AprPool.cpp \
     decaf/internal/util/ByteArrayAdapter.cpp \
     decaf/internal/util/HexStringParser.cpp \
@@ -348,6 +348,7 @@
     decaf/lang/Thread.h \
     decaf/lang/Number.h \
     decaf/lang/Runnable.h \
+    decaf/lang/Runtime.h \
     decaf/lang/Math.h \
     decaf/lang/System.h \
     decaf/lang/Boolean.h \
@@ -425,7 +426,7 @@
     decaf/util/logging/SimpleLogger.h \
     decaf/util/logging/PropertiesChangeListener.h \
     decaf/internal/AprPool.h \
-    decaf/internal/AprRuntime.h \
+    decaf/internal/DecafRuntime.h \
     decaf/internal/util/HexStringParser.h \
     decaf/internal/util/ByteArrayAdapter.h \
     decaf/internal/nio/BufferFactory.h \
diff --git a/src/main/activemq/support/InitDirector.cpp b/src/main/activemq/support/InitDirector.cpp
index f6e501b..8ee21ac 100644
--- a/src/main/activemq/support/InitDirector.cpp
+++ b/src/main/activemq/support/InitDirector.cpp
@@ -17,7 +17,7 @@
 #include "InitDirector.h"
 
 #include <decaf/util/logging/LogWriter.h>
-#include <decaf/internal/AprPool.h>
+#include <decaf/lang/Runtime.h>
 #include <activemq/transport/IOTransportFactory.h>
 #include <activemq/transport/MockTransportFactory.h>
 #include <activemq/transport/filters/AsyncSendTransportFactory.h>
@@ -37,8 +37,7 @@
 InitDirector::InitDirector() {
 
     if( refCount == 0 ) {
-        decaf::internal::AprPool initPools;
-        initPools.getAprPool();
+        decaf::lang::Runtime::getRuntime();
         connector::stomp::StompConnectorFactory::getInstance();
         connector::openwire::OpenWireConnectorFactory::getInstance();
         transport::filters::TcpTransportFactory::getInstance();
diff --git a/src/main/decaf/internal/AprPool.cpp b/src/main/decaf/internal/AprPool.cpp
index 2531c8d..f4c4281 100644
--- a/src/main/decaf/internal/AprPool.cpp
+++ b/src/main/decaf/internal/AprPool.cpp
@@ -16,6 +16,7 @@
  */
 
 #include "AprPool.h"
+#include <decaf/lang/Runtime.h>
 
 using namespace decaf;
 using namespace decaf::internal;
@@ -36,7 +37,7 @@
 void AprPool::allocatePool() const {
 
     if( aprPool == NULL ) {
-        apr_pool_create( &aprPool, NULL );
+        apr_pool_create_unmanaged( &aprPool );
     }
 }
 
@@ -61,10 +62,8 @@
 ////////////////////////////////////////////////////////////////////////////////
 apr_pool_t* AprPool::getAprPool() const {
 
-    // Creates a single static instance that will on the first call
-    // init apr and remain in memory until we shutdown and then free
-    // the apr resources.
-    static AprRuntime aprRuntime;
+    // Ensure there is a Runtime instance created.
+    decaf::lang::Runtime::getRuntime();
 
     // Ensure that the pool has been allocated.
     allocatePool();
diff --git a/src/main/decaf/internal/AprPool.h b/src/main/decaf/internal/AprPool.h
index bf5a550..b21dcf4 100644
--- a/src/main/decaf/internal/AprPool.h
+++ b/src/main/decaf/internal/AprPool.h
@@ -19,7 +19,6 @@
 #define _DECAF_INTERNAL_APRPOOL_H_
 
 #include <decaf/util/Config.h>
-#include <decaf/internal/AprRuntime.h>
 
 #include <apr_pools.h>
 
diff --git a/src/main/decaf/internal/AprRuntime.cpp b/src/main/decaf/internal/DecafRuntime.cpp
similarity index 64%
rename from src/main/decaf/internal/AprRuntime.cpp
rename to src/main/decaf/internal/DecafRuntime.cpp
index 5405389..5729c02 100644
--- a/src/main/decaf/internal/AprRuntime.cpp
+++ b/src/main/decaf/internal/DecafRuntime.cpp
@@ -15,25 +15,46 @@
  * limitations under the License.
  */
 
-#include "AprRuntime.h"
+#include "DecafRuntime.h"
 
 #include <apr.h>
 #include <apr_general.h>
 #include <apr_pools.h>
 
+using namespace decaf;
 using namespace decaf::internal;
+using namespace decaf::lang;
 
 ////////////////////////////////////////////////////////////////////////////////
-AprRuntime::AprRuntime() {
-            
+DecafRuntime::DecafRuntime() {
+
     // Initializes the APR Runtime from within a library.
     apr_initialize();
+
+    // Create a Global Pool for Threads to use
+    apr_pool_create_ex( &aprPool, NULL, NULL, NULL );
+
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-AprRuntime::~AprRuntime() {
-    
+DecafRuntime::~DecafRuntime() {
+
+    // Destroy the Global Thread Memory Pool
+    apr_pool_destroy( aprPool );
+
     // Cleans up APR data structures.
     apr_terminate();
-} 
+}
 
+////////////////////////////////////////////////////////////////////////////////
+apr_pool_t* DecafRuntime::getGlobalPool() const {
+    return this->aprPool;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+Runtime* Runtime::getRuntime() {
+
+    static DecafRuntime runtime;
+
+    return &runtime;
+}
diff --git a/src/main/decaf/internal/AprRuntime.h b/src/main/decaf/internal/DecafRuntime.h
similarity index 63%
copy from src/main/decaf/internal/AprRuntime.h
copy to src/main/decaf/internal/DecafRuntime.h
index 42e89dc..7a66ad2 100644
--- a/src/main/decaf/internal/AprRuntime.h
+++ b/src/main/decaf/internal/DecafRuntime.h
@@ -15,8 +15,12 @@
  * limitations under the License.
  */
 
-#ifndef _DECAF_INTERNAL_APRRUNTIME_H
-#define _DECAF_INTERNAL_APRRUNTIME_H
+#ifndef _DECAF_INTERNAL_DECAFRUNTIME_H
+#define _DECAF_INTERNAL_DECAFRUNTIME_H
+
+#include <decaf/util/Config.h>
+#include <decaf/lang/Runtime.h>
+#include <apr_pools.h>
 
 namespace decaf {
 namespace internal {
@@ -24,21 +28,34 @@
     /**
      * Handles APR initialization and termination.
      */
-    class AprRuntime {
+    class DECAF_API DecafRuntime : public decaf::lang::Runtime {
+    private:
+
+        /**
+         * Internal APR pool
+         */
+        mutable apr_pool_t* aprPool;
+
     public:
-    
+
         /**
          * Initializes the APR Runtime for a library.
          */
-        AprRuntime();
-    
+        DecafRuntime();
+
         /**
          * Terminates the APR Runtime for a library.
          */
-        virtual ~AprRuntime();
-    
+        virtual ~DecafRuntime();
+
+        /**
+         * Grants access to the Global APR Pool instance that should be
+         * used when creating new threads.
+         */
+        apr_pool_t* getGlobalPool() const;
+
     };
-    
+
 }}
 
-#endif /*_DECAF_INTERNAL_APRRUNTIME_H*/
+#endif /*_DECAF_INTERNAL_DECAFRUNTIME_H*/
diff --git a/src/main/decaf/internal/AprRuntime.h b/src/main/decaf/lang/Runtime.h
similarity index 65%
rename from src/main/decaf/internal/AprRuntime.h
rename to src/main/decaf/lang/Runtime.h
index 42e89dc..08b5702 100644
--- a/src/main/decaf/internal/AprRuntime.h
+++ b/src/main/decaf/lang/Runtime.h
@@ -15,30 +15,29 @@
  * limitations under the License.
  */
 
-#ifndef _DECAF_INTERNAL_APRRUNTIME_H
-#define _DECAF_INTERNAL_APRRUNTIME_H
+#ifndef _DECAF_LANG_RUNTIME_H_
+#define _DECAF_LANG_RUNTIME_H_
+
+#include <decaf/util/Config.h>
 
 namespace decaf {
-namespace internal {
+namespace lang {
 
-    /**
-     * Handles APR initialization and termination.
-     */
-    class AprRuntime {
+    class DECAF_API Runtime {
     public:
-    
+
+        virtual ~Runtime() {}
+
         /**
-         * Initializes the APR Runtime for a library.
+         * Gets the single instance of the Decaf Runtime for this Process.
+         *
+         * @returns pointer to the single Decaf Runtime instance that exists
+         *          for this process
          */
-        AprRuntime();
-    
-        /**
-         * Terminates the APR Runtime for a library.
-         */
-        virtual ~AprRuntime();
-    
+        static Runtime* getRuntime();
+
     };
-    
+
 }}
 
-#endif /*_DECAF_INTERNAL_APRRUNTIME_H*/
+#endif /*_DECAF_LANG_RUNTIME_H_*/
diff --git a/src/main/decaf/lang/Thread.cpp b/src/main/decaf/lang/Thread.cpp
index 6387432..fa247e3 100644
--- a/src/main/decaf/lang/Thread.cpp
+++ b/src/main/decaf/lang/Thread.cpp
@@ -20,6 +20,7 @@
 #include <apr_time.h>
 #include <apr_portable.h>
 
+#include <decaf/internal/DecafRuntime.h>
 #include <decaf/lang/Exception.h>
 #include <decaf/lang/exceptions/RuntimeException.h>
 
@@ -57,12 +58,14 @@
             "Thread::start - Thread already started");
     }
 
+    DecafRuntime* runtime = dynamic_cast<DecafRuntime*>( Runtime::getRuntime() );
+
     apr_status_t err = apr_thread_create(
         &this->threadHandle,
         NULL,
         runCallback,
         this,
-        pool.getAprPool() );
+        runtime->getGlobalPool() );
 
     if( err != APR_SUCCESS ) {
         throw Exception(
diff --git a/src/main/decaf/lang/Thread.h b/src/main/decaf/lang/Thread.h
index 5e6165d..f777547 100644
--- a/src/main/decaf/lang/Thread.h
+++ b/src/main/decaf/lang/Thread.h
@@ -23,7 +23,6 @@
 #include <stdexcept>
 #include <assert.h>
 
-#include <decaf/internal/AprPool.h>
 #include <apr_thread_proc.h>
 
 namespace decaf{
@@ -45,11 +44,6 @@
         Runnable* task;
 
         /**
-         * APR Pool to allocate thread from
-         */
-        decaf::internal::AprPool pool;
-
-        /**
          * APR Thread Handle
          */
         apr_thread_t* threadHandle;
diff --git a/src/test/main.cpp b/src/test/main.cpp
index 9d725c2..9b8a4df 100644
--- a/src/test/main.cpp
+++ b/src/test/main.cpp
@@ -20,9 +20,13 @@
 #include <cppunit/BriefTestProgressListener.h>
 #include <cppunit/TestResult.h>
 #include <activemq/util/Config.h>
+#include <decaf/lang/Runtime.h>
 
 int main( int argc AMQCPP_UNUSED, char **argv AMQCPP_UNUSED)
 {
+    // ensure that we start the runtime.
+    decaf::lang::Runtime::getRuntime();
+
     CppUnit::TextUi::TestRunner runner;
     CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
     runner.addTest( registry.makeTest() );
@@ -30,7 +34,7 @@
     // Shows a message as each test starts
     CppUnit::BriefTestProgressListener listener;
     runner.eventManager().addListener( &listener );
-    
+
     bool wasSuccessful = runner.run( "", false );
     return !wasSuccessful;
 }