activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r887732 - in /activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main: activemq/core/ActiveMQSession.cpp decaf/lang/Thread.cpp decaf/lang/Thread.h
Date Sun, 06 Dec 2009 18:25:26 GMT
Author: tabish
Date: Sun Dec  6 18:25:26 2009
New Revision: 887732

URL: http://svn.apache.org/viewvc?rev=887732&view=rev
Log:
http://issues.apache.org/activemq/browse/AMQCPP-271

Fix the Threads class to work with Foreign Thread instances.  Move the close marker in session
to after the close of the session resources.

Modified:
    activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/activemq/core/ActiveMQSession.cpp
    activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/decaf/lang/Thread.cpp
    activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/decaf/lang/Thread.h

Modified: activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/activemq/core/ActiveMQSession.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/activemq/core/ActiveMQSession.cpp?rev=887732&r1=887731&r2=887732&view=diff
==============================================================================
--- activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/activemq/core/ActiveMQSession.cpp
(original)
+++ activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/activemq/core/ActiveMQSession.cpp
Sun Dec  6 18:25:26 2009
@@ -118,9 +118,6 @@
 
     try {
 
-        // Now indicate that this session is closed.
-        closed = true;
-
         // Stop the dispatch executor.
         stop();
 
@@ -158,6 +155,9 @@
             }
         }
 
+        // Now indicate that this session is closed.
+        closed = true;
+
         // Remove this sessions from the connection
         this->connection->removeSession( this );
 

Modified: activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/decaf/lang/Thread.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/decaf/lang/Thread.cpp?rev=887732&r1=887731&r2=887732&view=diff
==============================================================================
--- activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/decaf/lang/Thread.cpp
(original)
+++ activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/decaf/lang/Thread.cpp
Sun Dec  6 18:25:26 2009
@@ -31,6 +31,8 @@
 #include <decaf/util/concurrent/TimeUnit.h>
 #include <decaf/util/concurrent/Mutex.h>
 
+#include <vector>
+
 #if HAVE_PTHREAD_H
 #include <pthread.h>
 #endif
@@ -169,6 +171,7 @@
 namespace{
 
     Thread* mainThread = NULL;
+    std::vector<Thread*> foreignThreads;
 
     #ifdef HAVE_PTHREAD_H
 
@@ -224,51 +227,67 @@
 ////////////////////////////////////////////////////////////////////////////////
 void Thread::initThreading() {
 
-    mainThread = new Thread( "Main Thread" );
+    // We mark the thread where Decaf's Init routine is called from as our Main Thread.
+    mainThread = Thread::createForeignThreadInstance( "Main Thread" );
+}
 
-    mainThread->properties->state = Thread::RUNNABLE;
-    mainThread->properties->priority = Thread::NORM_PRIORITY;
-    mainThread->properties->parent = mainThread;
+////////////////////////////////////////////////////////////////////////////////
+void Thread::shutdownThreading() {
 
-    #ifdef HAVE_PTHREAD_H
+    // Clear the Main Thread instance pointer, this indicates we are Shutdown.
+    mainThread = NULL;
 
-        mainThread->properties->handle = pthread_self();
+    // Destroy any Foreign Thread Facades that were created during runtime.
+    std::vector<Thread*>::iterator iter = foreignThreads.begin();
+    for( ; iter != foreignThreads.end(); ++iter ) {
+        delete *iter;
+    }
+    foreignThreads.clear();
 
-        // Create the Key used to store the Current Thread data
-        pthread_key_create( &currentThreadKey, NULL );
-        pthread_setspecific( currentThreadKey, mainThread );
+    #ifdef HAVE_PTHREAD_H
 
-    #else
+        // Destroy the current Thread key now, no longer needed.
+        pthread_key_delete( currentThreadKey );
 
-        mainThread->properties->handle = ::GetCurrentThread();
+    #else
 
-        // Create the key used to store the Current Thread data
-        currentThreadKey = ::TlsAlloc();
-        ::TlsSetValue( currentThreadKey, mainThread );
+        // Destroy our TLS resources before we shutdown.
+        ::TlsFree( currentThreadKey );
 
     #endif
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void Thread::shutdownThreading() {
+Thread* Thread::createForeignThreadInstance( const std::string& name ) {
+
+    Thread* foreignThread = new Thread( name );
+
+    foreignThread->properties->state = Thread::RUNNABLE;
+    foreignThread->properties->priority = Thread::NORM_PRIORITY;
+    foreignThread->properties->parent = NULL;
 
     #ifdef HAVE_PTHREAD_H
 
-        // Destroy the Main Thread instance.
-        delete mainThread;
+        foreignThread->properties->handle = pthread_self();
 
-        // Destroy the current Thread key now, no longer needed.
-        pthread_key_delete( currentThreadKey );
+        // Create the Key used to store the Current Thread data
+        pthread_key_create( &currentThreadKey, NULL );
+        pthread_setspecific( currentThreadKey, foreignThread );
 
     #else
 
-        // Destroy the Main Thread instance.
-        delete mainThread;
+        foreignThread->properties->handle = ::GetCurrentThread();
 
-        // Destroy our TLS resources before we shutdown.
-        ::TlsFree( currentThreadKey );
+        // Create the key used to store the Current Thread data
+        currentThreadKey = ::TlsAlloc();
+        ::TlsSetValue( currentThreadKey, foreignThread );
 
     #endif
+
+    // Store the Facade for later deletion
+    foreignThreads.push_back( foreignThread );
+
+    return foreignThread;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -573,6 +592,11 @@
 ////////////////////////////////////////////////////////////////////////////////
 Thread* Thread::currentThread() {
 
+    if( mainThread == NULL ) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "The Decaf Threading API is in a Shutdown State." );
+    }
+
     void* result = NULL;
 
     #ifdef HAVE_PTHREAD_H
@@ -585,9 +609,11 @@
 
     #endif
 
+    // This is a foreign Thread (Not Created By Decaf) lets create a stand in Thread
+    // instance so that other threads in Decaf can join it and wait on it.
     if( result == NULL ) {
-        throw RuntimeException(
-            __FILE__, __LINE__, "Failed to find the Current Thread pointer in the TLS." );
+        result = Thread::createForeignThreadInstance(
+            std::string( "ForeignThread-" ) + Integer::toString( Thread::getId() ) );
     }
 
     return (Thread*)result;

Modified: activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/decaf/lang/Thread.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/decaf/lang/Thread.h?rev=887732&r1=887731&r2=887732&view=diff
==============================================================================
--- activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/decaf/lang/Thread.h
(original)
+++ activemq/activemq-cpp/branches/activemq-cpp-3.1.x/activemq-cpp/src/main/decaf/lang/Thread.h
Sun Dec  6 18:25:26 2009
@@ -359,6 +359,9 @@
         // Initialize the Threads internal state
         void initialize( Runnable* task, const std::string& name );
 
+        // Create a Thread Facade for threads not created by the Decaf Library.
+        static Thread* createForeignThreadInstance( const std::string& name );
+
         // Called by the Decaf Runtime at startup to allow the Platform Threading
         // code to initialize any necessary Threading constructs needed to support
         // the features of this class.



Mime
View raw message