activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r886350 - /activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp
Date Thu, 03 Dec 2009 00:52:09 GMT
Author: tabish
Date: Thu Dec  3 00:52:08 2009
New Revision: 886350

URL: http://svn.apache.org/viewvc?rev=886350&view=rev
Log:
Fixes a potential deadlock during thread creation found while testing on windows.

Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp?rev=886350&r1=886349&r2=886350&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp Thu Dec  3 00:52:08
2009
@@ -185,7 +185,7 @@
             pthread_setspecific( currentThreadKey, NULL );
             pthread_exit(0);
 
-			properties->state = Thread::TERMINATED;
+            properties->state = Thread::TERMINATED;
 
             return NULL;
         }
@@ -210,7 +210,7 @@
 
             ::CloseHandle( properties->handle );
 
-			properties->state = Thread::TERMINATED;
+            properties->state = Thread::TERMINATED;
 
             return NULL;
         }
@@ -330,44 +330,52 @@
                 "Thread::start - Thread already started");
         }
 
-        #ifdef HAVE_PTHREAD_H
-            int result = pthread_create( &( properties->handle ),
-                                         &( properties->attributes ),
-                                         threadWorker, properties.get() );
-
-            if( result != 0 ) {
-                throw RuntimeException(
-                    __FILE__, __LINE__, "Failed to create new Thread." );
-            }
+        // The lock here acts as a gate to the newly created thread, it won't begin execution
+        // until this method completes which ensures that if the main thread calls start
and
+        // then join but the child thread has already exited there won't be a deadlock on
the
+        // wait handle.  Also the new thread won't be able to update any internal data until
+        // creation is complete which ensure that there is no state corruption.
+        synchronized( &this->properties->mutex ) {
+
+            #ifdef HAVE_PTHREAD_H
+                int result = pthread_create( &( properties->handle ),
+                                             &( properties->attributes ),
+                                             threadWorker, properties.get() );
 
-        #else
+                if( result != 0 ) {
+                    throw RuntimeException(
+                        __FILE__, __LINE__, "Failed to create new Thread." );
+                }
 
-            unsigned int threadId = 0;
+            #else
 
-            #ifndef _WIN32_WCE
+                unsigned int threadId = 0;
 
-                properties->handle = (HANDLE)_beginthreadex(
-                     NULL, (DWORD)0, threadWorker, properties.get(), 0, &threadId );
+                #ifndef _WIN32_WCE
 
-            #else
+                    properties->handle = (HANDLE)_beginthreadex(
+                         NULL, (DWORD)0, threadWorker, properties.get(), 0, &threadId
);
 
-                properties->hanlde = CreateThread( NULL, 0, threadWorker, properties.get(),
0, &threadId ) );
+                #else
 
-                if( properties->handle == 0 ) {
-                    throw RuntimeException(
-                        __FILE__, __LINE__, "Failed to create new Thread." );
-                }
+                    properties->hanlde = CreateThread( NULL, 0, threadWorker, properties.get(),
0, &threadId ) );
+
+                    if( properties->handle == 0 ) {
+                        throw RuntimeException(
+                            __FILE__, __LINE__, "Failed to create new Thread." );
+                    }
+
+                #endif
 
             #endif
 
-        #endif
+            // Only try and set this if its not the default value.
+            if( properties->priority != Thread::NORM_PRIORITY ) {
+                setPriority( properties->priority );
+            }
 
-        // Only try and set this if its not the default value.
-        if( properties->priority != Thread::NORM_PRIORITY ) {
-            setPriority( properties->priority );
+            properties->state = Thread::RUNNABLE;
         }
-
-        properties->state = Thread::RUNNABLE;
      }
      DECAF_CATCH_RETHROW( IllegalThreadStateException )
      DECAF_CATCH_RETHROW( RuntimeException )



Mime
View raw message