activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r825256 - in /activemq/activemq-cpp/trunk/activemq-cpp/src: main/decaf/lang/Thread.cpp test/decaf/lang/ThreadTest.cpp test/decaf/lang/ThreadTest.h
Date Wed, 14 Oct 2009 20:11:08 GMT
Author: tabish
Date: Wed Oct 14 20:11:07 2009
New Revision: 825256

URL: http://svn.apache.org/viewvc?rev=825256&view=rev
Log:
Implements timed joins for all platforms.

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

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=825256&r1=825255&r2=825256&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 Wed Oct 14 20:11:07
2009
@@ -122,7 +122,9 @@
 
         static void runCallback( ThreadProperties* properties ) {
 
-            properties->state = Thread::RUNNABLE;
+            synchronized( &properties->mutex ) {
+                properties->state = Thread::RUNNABLE;
+            }
 
             // Invoke run on the task.
             try{
@@ -150,10 +152,15 @@
                 }
             }
 
-            // Indicate we are done.
-            properties->state = Thread::TERMINATED;
-        }
+            synchronized( &properties->mutex ) {
+
+                // Indicate we are done.
+                properties->state = Thread::TERMINATED;
 
+                // Signal any joined threads.
+                properties->mutex.notifyAll();
+            }
+        }
     };
 
 }}
@@ -418,29 +425,34 @@
             "Thread::join( millisecs, nanos ) - Nanoseconds must be in range [0...999999]"
);
     }
 
-    if( this->properties->state < Thread::RUNNABLE ) {
-        return;
-    }
-
-    #ifdef HAVE_PTHREAD_H
+    synchronized( &this->properties->mutex ) {
 
-        void* theReturn = NULL;
-
-        // TODO - Still need a way to do this if the non-posix method doesn't exist.
-        #if HAVE_PTHREAD_TIMEDJOIN_NP
-
-            long long totalTime = TimeUnit::MILLISECONDS.toNanos( millisecs ) + nanos;
-
-            timespec time;
-            time.tv_nsec = totalTime % 1000000000;
-            time.tv_sec = totalTime / 1000000000;
+        if( this->properties->state < Thread::RUNNABLE ) {
+            return;
+        }
 
-            pthread_timedjoin_np( properties->handle, &theReturn, &time );
+        this->properties->mutex.wait( millisecs, nanos );
+    }
 
-        #endif
-    #else
-        unsigned int rv = WaitForSingleObject( properties->handle, (DWORD)millisecs );
-    #endif
+//    #ifdef HAVE_PTHREAD_H
+//
+//        void* theReturn = NULL;
+//
+//        // TODO - Still need a way to do this if the non-posix method doesn't exist.
+//        #if HAVE_PTHREAD_TIMEDJOIN_NP
+//
+//            long long totalTime = TimeUnit::MILLISECONDS.toNanos( millisecs ) + nanos;
+//
+//            timespec time;
+//            time.tv_nsec = totalTime % 1000000000;
+//            time.tv_sec = totalTime / 1000000000;
+//
+//            pthread_timedjoin_np( properties->handle, &theReturn, &time );
+//
+//        #endif
+//    #else
+//        unsigned int rv = WaitForSingleObject( properties->handle, (DWORD)millisecs
);
+//    #endif
 }
 
 ////////////////////////////////////////////////////////////////////////////////

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/ThreadTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/ThreadTest.cpp?rev=825256&r1=825255&r2=825256&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/ThreadTest.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/ThreadTest.cpp Wed Oct 14
20:11:07 2009
@@ -208,60 +208,107 @@
 ////////////////////////////////////////////////////////////////////////////////
 void ThreadTest::testDelegate(){
 
-      Delegate test;
-      int initialValue = test.getStuff();
+    Delegate test;
+    int initialValue = test.getStuff();
 
-      Thread thread( &test );
-      thread.start();
-      thread.join();
-
-      int finalValue = test.getStuff();
-
-      // The values should be different - this proves
-      // that the runnable was run.
-      CPPUNIT_ASSERT( finalValue != initialValue );
+    Thread thread( &test );
+    thread.start();
+    thread.join();
+
+    int finalValue = test.getStuff();
+
+    // The values should be different - this proves
+    // that the runnable was run.
+    CPPUNIT_ASSERT( finalValue != initialValue );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void ThreadTest::testDerived() {
 
-      Derived test;
-      int initialValue = test.getStuff();
+    Derived test;
+    int initialValue = test.getStuff();
 
-      test.start();
-      test.join();
+    test.start();
+    test.join();
 
-      int finalValue = test.getStuff();
+    int finalValue = test.getStuff();
 
-      // The values should be different - this proves
-      // that the runnable was run.
-      CPPUNIT_ASSERT( finalValue != initialValue );
+    // The values should be different - this proves
+    // that the runnable was run.
+    CPPUNIT_ASSERT( finalValue != initialValue );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void ThreadTest::testJoin() {
+void ThreadTest::testJoin1() {
 
-      JoinTest test;
+    JoinTest test;
 
-      // Joining a non-started thread should just return.
-      CPPUNIT_ASSERT_NO_THROW( test.join() );
-      CPPUNIT_ASSERT_NO_THROW( test.join( 10 ) );
-      CPPUNIT_ASSERT_NO_THROW( test.join( 10, 10 ) );
+    // Joining a non-started thread should just return.
+    CPPUNIT_ASSERT_NO_THROW( test.join() );
+    CPPUNIT_ASSERT_NO_THROW( test.join( 10 ) );
+    CPPUNIT_ASSERT_NO_THROW( test.join( 10, 10 ) );
 
-      CPPUNIT_ASSERT_MESSAGE( "Thread is alive", !test.isAlive() );
-      time_t startTime = time( NULL );
-      test.start();
-      test.join();
-      time_t endTime = time( NULL );
-      CPPUNIT_ASSERT_MESSAGE( "Joined Thread is still alive", !test.isAlive() );
+    CPPUNIT_ASSERT_MESSAGE( "Thread is alive", !test.isAlive() );
+    time_t startTime = time( NULL );
+    test.start();
+    test.join();
+    time_t endTime = time( NULL );
+    CPPUNIT_ASSERT_MESSAGE( "Joined Thread is still alive", !test.isAlive() );
 
-      time_t delta = endTime - startTime;
+    time_t delta = endTime - startTime;
 
-      // Should be about 5 seconds that elapsed.
-      CPPUNIT_ASSERT( delta >= 1 && delta <= 3 );
+    // Should be about 2 seconds that elapsed.
+    CPPUNIT_ASSERT( delta >= 1 && delta <= 3 );
 
-      // Thread should be able to join itself, use a timeout so we don't freeze
-      Thread::currentThread()->join( 100 );
+    // Thread should be able to join itself, use a timeout so we don't freeze
+    Thread::currentThread()->join( 100 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ThreadTest::testJoin2() {
+
+    JoinTest test;
+
+    // Joining a non-started thread should just return.
+    CPPUNIT_ASSERT_NO_THROW( test.join() );
+    CPPUNIT_ASSERT_NO_THROW( test.join( 10 ) );
+    CPPUNIT_ASSERT_NO_THROW( test.join( 10, 10 ) );
+
+    CPPUNIT_ASSERT_MESSAGE( "Thread is alive", !test.isAlive() );
+    time_t startTime = time( NULL );
+    test.start();
+    test.join( 3500, 999999 );
+    time_t endTime = time( NULL );
+    CPPUNIT_ASSERT_MESSAGE( "Joined Thread is still alive", !test.isAlive() );
+
+    time_t delta = endTime - startTime;
+
+    // Should be about 2 seconds that elapsed.
+    CPPUNIT_ASSERT( delta >= 1 && delta <= 3 );
+
+    // Thread should be able to join itself, use a timeout so we don't freeze
+    Thread::currentThread()->join( 100 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ThreadTest::testJoin3() {
+
+    JoinTest test;
+
+    // Joining a non-started thread should just return.
+    CPPUNIT_ASSERT_NO_THROW( test.join() );
+    CPPUNIT_ASSERT_NO_THROW( test.join( 10 ) );
+    CPPUNIT_ASSERT_NO_THROW( test.join( 10, 10 ) );
+
+    CPPUNIT_ASSERT_MESSAGE( "Thread is alive", !test.isAlive() );
+    test.start();
+    test.join( 0, 999999 );
+    CPPUNIT_ASSERT_MESSAGE( "Joined Thread is not still alive", test.isAlive() );
+    test.join( 3500, 999999 );
+    CPPUNIT_ASSERT_MESSAGE( "Joined Thread is still alive", !test.isAlive() );
+
+    // Thread should be able to join itself, use a timeout so we don't freeze
+    Thread::currentThread()->join( 100 );
 }
 
 ////////////////////////////////////////////////////////////////////////////////

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/ThreadTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/ThreadTest.h?rev=825256&r1=825255&r2=825256&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/ThreadTest.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/ThreadTest.h Wed Oct 14 20:11:07
2009
@@ -36,7 +36,9 @@
       CPPUNIT_TEST( testConstructor_3 );
       CPPUNIT_TEST( testDelegate );
       CPPUNIT_TEST( testDerived );
-      CPPUNIT_TEST( testJoin );
+      CPPUNIT_TEST( testJoin1 );
+      CPPUNIT_TEST( testJoin2 );
+      CPPUNIT_TEST( testJoin3 );
       CPPUNIT_TEST( testSetPriority );
       CPPUNIT_TEST( testIsAlive );
       CPPUNIT_TEST( testGetId );
@@ -60,7 +62,9 @@
         void testConstructor_3();
         void testDelegate();
         void testDerived();
-        void testJoin();
+        void testJoin1();
+        void testJoin2();
+        void testJoin3();
         void testSetPriority();
         void testIsAlive();
         void testGetId();



Mime
View raw message