activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r553351 - in /activemq/activemq-cpp/trunk/src/test/activemq/concurrent: MutexTest.cpp MutexTest.h
Date Thu, 05 Jul 2007 00:12:43 GMT
Author: tabish
Date: Wed Jul  4 17:12:42 2007
New Revision: 553351

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

Modified:
    activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.cpp
    activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.h

Modified: activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.cpp?view=diff&rev=553351&r1=553350&r2=553351
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.cpp Wed Jul  4 17:12:42
2007
@@ -19,3 +19,321 @@
 
 CPPUNIT_TEST_SUITE_REGISTRATION( activemq::concurrent::MutexTest );
 
+using namespace std;
+using namespace activemq;
+using namespace activemq::concurrent;
+
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::testTimedWait(){
+
+   try
+   {
+      MyTimedWaitingThread test;
+      time_t startTime = time( NULL );
+      test.start();
+      test.join();
+      time_t endTime = time( NULL );
+
+      time_t delta = endTime - startTime;
+
+      CPPUNIT_ASSERT( delta >= 1 && delta <= 3 );
+  }
+  catch(exceptions::ActiveMQException& ex)
+  {
+     std::cout << ex.getMessage() << std::endl;
+  }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::testWait(){
+
+    try
+    {
+        MyWaitingThread test;
+        test.start();
+
+        Thread::sleep(1000);
+
+        synchronized(&test)
+        {
+            for( int ix=0; ix<100; ix++ ){
+                test.value += 1;
+            }
+
+            test.notify();
+        }
+
+        test.join();
+
+        CPPUNIT_ASSERT( test.value == 2500 );
+    }
+    catch(exceptions::ActiveMQException& ex)
+    {
+        ex.setMark( __FILE__, __LINE__ );
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::test()
+{
+    MyThread test;
+
+    synchronized(&test){
+
+        test.start();
+
+        for( int ix=0; ix<100; ix++ ){
+            test.value += 1;
+        }
+    }
+
+    test.join();
+
+    CPPUNIT_ASSERT( test.value == 2500 );
+}
+
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::testNotify()
+{
+    try{
+        Mutex mutex;
+        Mutex started;
+        Mutex completed;
+
+        const int numThreads = 30;
+        MyNotifiedThread* threads[numThreads];
+
+        // Create and start all the threads.
+        for( int ix=0; ix<numThreads; ++ix ){
+            threads[ix] = new MyNotifiedThread( &mutex, &started, &completed
);
+            threads[ix]->start();
+        }
+
+        synchronized( &started )
+        {
+            int count = 0;
+
+            while( count < ( numThreads ) )
+            {
+                started.wait( 30 );
+                count++;
+            }
+        }
+
+        synchronized(&mutex)
+        {
+            mutex.notify();
+        }
+
+        Thread::sleep( 1000 );
+
+        int counter = 0;
+        for( int ix=0; ix<numThreads; ++ix ){
+            if( threads[ix]->done ){
+                counter++;
+            }
+        }
+
+        // Make sure only 1 thread was notified.
+        CPPUNIT_ASSERT( counter == 1 );
+
+        synchronized(&mutex)
+        {
+            // Notify all threads.
+            for( int ix=0; ix<numThreads-1; ++ix ){
+                mutex.notify();
+            }
+        }
+
+        synchronized( &started )
+        {
+            int count = 0;
+
+            while( count < ( numThreads ) )
+            {
+                started.wait( 30 );
+                count++;
+            }
+        }
+
+        int numComplete = 0;
+        for( int ix=0; ix<numThreads; ++ix ){
+            if( threads[ix]->done ){
+                numComplete++;
+            }
+        }
+        CPPUNIT_ASSERT( numComplete == numThreads );
+
+        synchronized( &mutex ){
+          mutex.wait( 5 );
+        }
+
+        synchronized( &mutex )
+        {
+            mutex.notifyAll();
+        }
+
+        // Delete all the threads.
+        for( int ix=0; ix<numThreads; ++ix ){
+            delete threads[ix];
+        }
+
+    }catch( exceptions::ActiveMQException& ex ){
+        ex.setMark( __FILE__, __LINE__ );
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::testNotifyAll()
+{
+    try{
+        Mutex mutex;
+        Mutex started;
+        Mutex completed;
+
+        const int numThreads = 100;
+        MyNotifiedThread* threads[numThreads];
+
+        // Create and start all the threads.
+        for( int ix=0; ix<numThreads; ++ix ){
+            threads[ix] = new MyNotifiedThread( &mutex, &started, &completed
);
+            threads[ix]->start();
+        }
+
+        synchronized( &started )
+        {
+            int count = 0;
+
+            while( count < ( numThreads ) )
+            {
+                started.wait( 30 );
+                count++;
+            }
+        }
+
+        for( int ix=0; ix<numThreads; ++ix )
+        {
+            if( threads[ix]->done == true ){
+                printf("threads[%d] is done prematurely\n", ix );
+            }
+            CPPUNIT_ASSERT( threads[ix]->done == false );
+        }
+
+        // Notify all threads.
+        synchronized( &mutex ){
+            mutex.notifyAll();
+        }
+
+        synchronized( &completed )
+        {
+            int count = 0;
+
+            while( count < ( numThreads ) )
+            {
+                completed.wait( 30 );
+                count++;
+            }
+        }
+
+        int numComplete = 0;
+        for( int ix=0; ix<numThreads; ++ix ){
+            if( threads[ix]->done ){
+                numComplete++;
+            }
+        }
+        //printf("numComplete: %d, numThreads: %d\n", numComplete, numThreads );
+        CPPUNIT_ASSERT( numComplete == numThreads );
+
+        // Delete all the threads.
+        for( int ix=0; ix<numThreads; ++ix ){
+            threads[ix]->join();
+            delete threads[ix];
+        }
+
+    }catch( exceptions::ActiveMQException& ex ){
+        ex.setMark( __FILE__, __LINE__ );
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::testRecursiveLock()
+{
+    try{
+        Mutex mutex;
+
+        const int numThreads = 30;
+        MyRecursiveLockThread* threads[numThreads];
+
+        // Create and start all the threads.
+        for( int ix=0; ix<numThreads; ++ix ){
+            threads[ix] = new MyRecursiveLockThread( &mutex );
+            threads[ix]->start();
+        }
+
+        // Sleep so all the threads can get to the wait.
+        Thread::sleep( 1000 );
+
+        for( int ix=0; ix<numThreads; ++ix ){
+            if( threads[ix]->done == true ){
+                std::cout << "threads[" << ix
+                          << "] is done prematurely\n";
+            }
+            CPPUNIT_ASSERT( threads[ix]->done == false );
+        }
+
+        // Notify all threads.
+        synchronized( &mutex )
+        {
+            synchronized( &mutex )
+            {
+                mutex.notifyAll();
+            }
+        }
+
+        // Sleep to give the threads time to wake up.
+        Thread::sleep( 1000 );
+
+        for( int ix=0; ix<numThreads; ++ix ){
+            if( threads[ix]->done != true ){
+                std::cout<< "threads[" << ix << "] is not done\n";
+            }
+            CPPUNIT_ASSERT( threads[ix]->done == true );
+        }
+
+        // Delete all the threads.
+        for( int ix=0; ix<numThreads; ++ix ){
+            delete threads[ix];
+        }
+    }catch( exceptions::ActiveMQException& ex ){
+        ex.setMark( __FILE__, __LINE__ );
+    }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::testDoubleLock() {
+
+    try{
+        Mutex mutex1;
+        Mutex mutex2;
+
+        MyDoubleLockThread thread(&mutex1, &mutex2);
+
+        thread.start();
+
+        // Let the thread get both locks
+        Thread::sleep( 200 );
+
+        // Lock mutex 2, thread is waiting on it
+        synchronized(&mutex2)
+        {
+           mutex2.notify();
+        }
+
+        // Let the thread die
+        thread.join();
+
+        CPPUNIT_ASSERT( thread.done );
+    }catch( exceptions::ActiveMQException& ex ){
+        ex.setMark( __FILE__, __LINE__ );
+    }
+}

Modified: activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.h?view=diff&rev=553351&r1=553350&r2=553351
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.h (original)
+++ activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.h Wed Jul  4 17:12:42
2007
@@ -15,8 +15,8 @@
  * limitations under the License.
  */
 
-#ifndef ACTIVEMQ_CONCURRENT_MUTEXTEST_H_
-#define ACTIVEMQ_CONCURRENT_MUTEXTEST_H_
+#ifndef _ACTIVEMQ_CONCURRENT_MUTEXTEST_H_
+#define _ACTIVEMQ_CONCURRENT_MUTEXTEST_H_
 
 #include <cppunit/TestFixture.h>
 #include <cppunit/extensions/HelperMacros.h>
@@ -43,71 +43,71 @@
 
     public:
 
-        class MyThread 
-        : 
+        class MyThread
+        :
             public Thread,
             public Synchronizable{
-    
+
         private:
-        
+
             Mutex mutex;
-            
+
         public:
-        
+
             int value;
             MyThread(){ value = 0;}
             virtual ~MyThread(){}
-            
+
             virtual void lock() throw(exceptions::ActiveMQException){
                 mutex.lock();
-            }    
+            }
             virtual void unlock() throw(exceptions::ActiveMQException){
                 mutex.unlock();
-            }       
+            }
             virtual void wait() throw(exceptions::ActiveMQException){
                 mutex.wait();
             }
             virtual void wait(unsigned long millisecs) throw(exceptions::ActiveMQException){
                 mutex.wait( millisecs );
-            }  
+            }
             virtual void notify() throw(exceptions::ActiveMQException){
                 mutex.notify();
             }
             virtual void notifyAll() throw(exceptions::ActiveMQException){
                 mutex.notifyAll();
-            }            
-        
+            }
+
             virtual void run(){
-                
+
                 {
                     Lock lock (this);
-                    
-                    value = value * 25;                 
+
+                    value = value * 25;
                 }
             }
-        
+
         };
-      
-        class MyWaitingThread 
-        : 
+
+        class MyWaitingThread
+        :
             public Thread,
             public Synchronizable{
-         
+
         private:
-      
+
             Mutex mutex;
-         
+
         public:
-     
+
             int value;
             MyWaitingThread(){ value = 0;}
             virtual ~MyWaitingThread(){}
             virtual void lock() throw(exceptions::ActiveMQException){
                 mutex.lock();
-            }    
+            }
             virtual void unlock() throw(exceptions::ActiveMQException){
                 mutex.unlock();
-            }       
+            }
             virtual void wait() throw(exceptions::ActiveMQException){
                 mutex.wait();
             }
@@ -119,19 +119,19 @@
             }
             virtual void notifyAll() throw(exceptions::ActiveMQException){
                 mutex.notifyAll();
-            }  
-         
+            }
+
             virtual void run(){
-         
+
                 try
                 {
-                    synchronized(this)            
+                    synchronized(this)
                     {
                         this->wait();
-            
+
                         std::cout.flush();
-                 
-                        value = value * 25;              
+
+                        value = value * 25;
                     }
                 }
                 catch(exceptions::ActiveMQException& ex)
@@ -139,545 +139,241 @@
                     ex.setMark( __FILE__, __LINE__ );
                 }
             }
-      };
+        };
 
-      class MyTimedWaitingThread 
-      : 
-         public Thread,
-         public Synchronizable{
-        
-      private:
-    
-         Mutex mutex;
-         
-      public:
-     
-         int value;
-         MyTimedWaitingThread(){ value = 0;}
-         virtual ~MyTimedWaitingThread(){}
-         virtual void lock() throw(exceptions::ActiveMQException){
-             mutex.lock();
-         }    
-         virtual void unlock() throw(exceptions::ActiveMQException){
-             mutex.unlock();
-         }       
-         virtual void wait() throw(exceptions::ActiveMQException){
-             mutex.wait();
-         }
-         virtual void wait(unsigned long millisecs) throw(exceptions::ActiveMQException){
-             mutex.wait( millisecs );
-         }
-         virtual void notify() throw(exceptions::ActiveMQException){
-             mutex.notify();
-         }
-         virtual void notifyAll() throw(exceptions::ActiveMQException){
-             mutex.notifyAll();
-         }  
-        
-         virtual void run(){
-      
-            try
-            {
-               synchronized(this)            
-               {
-                  this->wait(2000);
-                        
-                  value = 666;              
-               }
+        class MyTimedWaitingThread
+        :
+            public Thread,
+            public Synchronizable{
+
+        private:
+
+            Mutex mutex;
+
+        public:
+
+            int value;
+            MyTimedWaitingThread(){ value = 0;}
+            virtual ~MyTimedWaitingThread(){}
+            virtual void lock() throw(exceptions::ActiveMQException){
+                mutex.lock();
             }
-            catch(exceptions::ActiveMQException& ex)
-            {
-                ex.setMark( __FILE__, __LINE__ );
+            virtual void unlock() throw(exceptions::ActiveMQException){
+                mutex.unlock();
             }
-         }
-      };
-     
-      class MyNotifiedThread 
-      : 
-         public Thread,
-         public Synchronizable{
-        
-      public:
-      
-         bool done;
-         Mutex* mutex;
-         Mutex* started;
-         Mutex* completed;
-         
-      public:
-     
-         int value;
-         MyNotifiedThread(Mutex* mutex, Mutex* started, Mutex* completed ){ 
-            this->mutex = mutex; 
-            this->started = started;
-            this->completed = completed;
-            this->done = false; 
-         }
-         virtual ~MyNotifiedThread(){}
-         virtual void lock() throw(exceptions::ActiveMQException){
-             mutex->lock();
-         }    
-         virtual void unlock() throw(exceptions::ActiveMQException){
-             mutex->unlock();
-         }       
-         virtual void wait() throw(exceptions::ActiveMQException){
-             mutex->wait();
-         }
-         virtual void wait(unsigned long millisecs) throw(exceptions::ActiveMQException){
-             mutex->wait( millisecs );
-         }
-         virtual void notify() throw(exceptions::ActiveMQException){
-             mutex->notify();
-         }
-         virtual void notifyAll() throw(exceptions::ActiveMQException){
-             mutex->notifyAll();
-         }  
-        
-         virtual void run(){
-      
-            try
-            {
-               done = false;
-               synchronized(this)            
-               {
-                  synchronized( started )
-                  {
-                     started->notify();
-                  }
-                  
-                  this->wait();
-                  done = true;
-
-                  synchronized( completed )
-                  {
-                     completed->notify();
-                  }
-               }
+            virtual void wait() throw(exceptions::ActiveMQException){
+                mutex.wait();
             }
-            catch(exceptions::ActiveMQException& ex)
-            {
-               ex.setMark( __FILE__, __LINE__ );
+            virtual void wait(unsigned long millisecs) throw(exceptions::ActiveMQException){
+                mutex.wait( millisecs );
             }
-         }
-      };
-      
-      class MyRecursiveLockThread 
-      : 
-         public Thread,
-         public Synchronizable{
-        
-      public:
-      
-         bool done;
-         Mutex* mutex;
-         
-      public:
-     
-         int value;
-         MyRecursiveLockThread(Mutex* mutex){ this->mutex = mutex; done = false; }
-         virtual ~MyRecursiveLockThread(){}
-         virtual void lock() throw(exceptions::ActiveMQException){
-             mutex->lock();
-         }    
-         virtual void unlock() throw(exceptions::ActiveMQException){
-             mutex->unlock();
-         }       
-         virtual void wait() throw(exceptions::ActiveMQException){
-             mutex->wait();
-         }
-         virtual void wait(unsigned long millisecs) throw(exceptions::ActiveMQException){
-             mutex->wait( millisecs );
-         }
-         virtual void notify() throw(exceptions::ActiveMQException){
-             mutex->notify();
-         }
-         virtual void notifyAll() throw(exceptions::ActiveMQException){
-             mutex->notifyAll();
-         }  
-        
-         virtual void run(){
-      
-            try
-            {
-               done = false;
-               synchronized(this)            
-               {
-                  synchronized(this)
-                  {
-                     this->wait();
-                     done = true;
-                  }
-               }
+            virtual void notify() throw(exceptions::ActiveMQException){
+                mutex.notify();
             }
-            catch(exceptions::ActiveMQException& ex)
-            {
-               ex.setMark( __FILE__, __LINE__ );
+            virtual void notifyAll() throw(exceptions::ActiveMQException){
+                mutex.notifyAll();
             }
-         }
-      };
 
-      class MyDoubleLockThread 
-      : 
-         public Thread
-      {
-        
-      public:
-      
-         bool done;
-         Mutex* mutex1;
-         Mutex* mutex2;
-         
-      public:
-     
-         int value;
-         MyDoubleLockThread(Mutex* mutex1, Mutex* mutex2)
-         {
-            this->mutex1 = mutex1;
-            this->mutex2 = mutex2;
-            done = false;
-         }
-
-         virtual ~MyDoubleLockThread(){}
-        
-         virtual void run(){
-      
-            try
-            {
-               done = false;
-               synchronized(mutex1)            
-               {
-                  synchronized(mutex2)
-                  {
-                     mutex2->wait();
-                     done = true;
-                  }
-               }
-            }
-            catch(exceptions::ActiveMQException& ex)
-            {
-               ex.setMark( __FILE__, __LINE__ );
-            }
-         }
-      };
+            virtual void run(){
+                try
+                {
+                   synchronized(this)
+                   {
+                      this->wait(2000);
 
-   public:
-    
-        virtual ~MutexTest(){}
-        virtual void setUp(){} 
-        virtual void tearDown(){}
-        
-      void testTimedWait(){
+                      value = 666;
+                   }
+                }
+                catch(exceptions::ActiveMQException& ex)
+                {
+                    ex.setMark( __FILE__, __LINE__ );
+                }
+            }
+        };
 
-         try
-         {
-            MyTimedWaitingThread test;
-            time_t startTime = time( NULL );
-            test.start();
-            test.join();
-            time_t endTime = time( NULL );
-       
-            time_t delta = endTime - startTime;
-
-            CPPUNIT_ASSERT( delta >= 1 && delta <= 3 );
-        }
-        catch(exceptions::ActiveMQException& ex)
-        {
-           std::cout << ex.getMessage() << std::endl;
-        }
-      }
+        class MyNotifiedThread
+        :
+            public Thread,
+            public Synchronizable{
 
-        void testWait(){
+        public:
 
-         try
-         {
-            MyWaitingThread test;
-            test.start();
+            bool done;
+            Mutex* mutex;
+            Mutex* started;
+            Mutex* completed;
 
-            Thread::sleep(1000);
+        public:
 
-            synchronized(&test)
-            {
-                for( int ix=0; ix<100; ix++ ){
-                    test.value += 1;
-                }
-  
-               test.notify();
+            int value;
+            MyNotifiedThread(Mutex* mutex, Mutex* started, Mutex* completed ){
+                this->mutex = mutex;
+                this->started = started;
+                this->completed = completed;
+                this->done = false;
+            }
+            virtual ~MyNotifiedThread(){}
+            virtual void lock() throw(exceptions::ActiveMQException){
+                mutex->lock();
+            }
+            virtual void unlock() throw(exceptions::ActiveMQException){
+                mutex->unlock();
+            }
+            virtual void wait() throw(exceptions::ActiveMQException){
+                mutex->wait();
+            }
+            virtual void wait(unsigned long millisecs) throw(exceptions::ActiveMQException){
+                mutex->wait( millisecs );
+            }
+            virtual void notify() throw(exceptions::ActiveMQException){
+                mutex->notify();
+            }
+            virtual void notifyAll() throw(exceptions::ActiveMQException){
+                mutex->notifyAll();
             }
-   
-            test.join();
-          
-            CPPUNIT_ASSERT( test.value == 2500 );
 
-        }
-        catch(exceptions::ActiveMQException& ex)
-        {
-           ex.setMark( __FILE__, __LINE__ );
-        }
-        }
-      
-        void test()
-        {
-            MyThread test;
-            
-            synchronized(&test){
-
-                test.start();
-             
-                for( int ix=0; ix<100; ix++ ){
-                    test.value += 1;
-                }
-            }
-         
-            test.join();
-         
-            CPPUNIT_ASSERT( test.value == 2500 );
-        }
-      
-        void testNotify()
-        {
-            try{
-                Mutex mutex;
-                Mutex started;
-                Mutex completed;
-
-                const int numThreads = 30;
-                MyNotifiedThread* threads[numThreads];
-             
-                // Create and start all the threads.
-                for( int ix=0; ix<numThreads; ++ix ){
-                    threads[ix] = new MyNotifiedThread( &mutex, &started, &completed
);
-                    threads[ix]->start();
-                }
-             
-                synchronized( &started )
-                {
-                    int count = 0;
-                    
-                    while( count < ( numThreads ) )
-                    {
-                        started.wait( 30 );
-                        count++;
-                    }
-                }
-             
-                synchronized(&mutex)
-                {
-                    mutex.notify();
-                }
-             
-                Thread::sleep( 1000 );
-             
-                int counter = 0;
-                for( int ix=0; ix<numThreads; ++ix ){
-                    if( threads[ix]->done ){
-                        counter++;
-                    }
-                }
-             
-                // Make sure only 1 thread was notified.
-                CPPUNIT_ASSERT( counter == 1 ); 
-             
-                synchronized(&mutex)
-                {
-                    // Notify all threads.
-                    for( int ix=0; ix<numThreads-1; ++ix ){
-                        mutex.notify();
-                    }
-                }
-             
-                synchronized( &started )
+            virtual void run(){
+
+                try
                 {
-                    int count = 0;
-                    
-                    while( count < ( numThreads ) )
+                    done = false;
+                    synchronized(this)
                     {
-                        started.wait( 30 );
-                        count++;
+                        synchronized( started )
+                        {
+                            started->notify();
+                        }
+
+                        this->wait();
+                        done = true;
+
+                        synchronized( completed )
+                        {
+                            completed->notify();
+                        }
                     }
                 }
-             
-                int numComplete = 0;
-                for( int ix=0; ix<numThreads; ++ix ){
-                    if( threads[ix]->done ){
-                        numComplete++;
-                    }  
-                }
-                CPPUNIT_ASSERT( numComplete == numThreads );
-                
-                synchronized( &mutex )
-                {
-                    mutex.wait( 5 );
-                }
-                
-                synchronized( &mutex )
+                catch(exceptions::ActiveMQException& ex)
                 {
-                    mutex.notifyAll();
+                    ex.setMark( __FILE__, __LINE__ );
                 }
+            }
+        };
 
-                // Delete all the threads.
-                for( int ix=0; ix<numThreads; ++ix ){
-                    delete threads[ix];
-                }
-                                
-            }catch( exceptions::ActiveMQException& ex ){
-                ex.setMark( __FILE__, __LINE__ );
+        class MyRecursiveLockThread
+        :
+            public Thread,
+            public Synchronizable{
+
+        public:
+
+            bool done;
+            Mutex* mutex;
+
+        public:
+
+            int value;
+            MyRecursiveLockThread(Mutex* mutex){ this->mutex = mutex; done = false; }
+            virtual ~MyRecursiveLockThread(){}
+            virtual void lock() throw(exceptions::ActiveMQException){
+                mutex->lock();
             }
-        }
-      
-        void testNotifyAll()
-        {
-            try{
-                Mutex mutex;
-                Mutex started;
-                Mutex completed;
-             
-                const int numThreads = 100;
-                MyNotifiedThread* threads[numThreads];
-             
-                // Create and start all the threads.
-                for( int ix=0; ix<numThreads; ++ix ){
-                    threads[ix] = new MyNotifiedThread( &mutex, &started, &completed
);
-                    threads[ix]->start();
-                }
-             
-                synchronized( &started )
-                {
-                    int count = 0;
-                    
-                    while( count < ( numThreads ) )
-                    {
-                        started.wait( 30 );
-                        count++;
-                    }
-                }
-             
-                for( int ix=0; ix<numThreads; ++ix )
+            virtual void unlock() throw(exceptions::ActiveMQException){
+                mutex->unlock();
+            }
+            virtual void wait() throw(exceptions::ActiveMQException){
+                mutex->wait();
+            }
+            virtual void wait(unsigned long millisecs) throw(exceptions::ActiveMQException){
+                mutex->wait( millisecs );
+            }
+            virtual void notify() throw(exceptions::ActiveMQException){
+                mutex->notify();
+            }
+            virtual void notifyAll() throw(exceptions::ActiveMQException){
+                mutex->notifyAll();
+            }
+
+            virtual void run(){
+
+                try
                 {
-                    if( threads[ix]->done == true ){
-                        printf("threads[%d] is done prematurely\n", ix );
-                    }
-                    CPPUNIT_ASSERT( threads[ix]->done == false );            
-                }
-             
-                // Notify all threads.
-                synchronized( &mutex ){
-                   mutex.notifyAll();
-                }
-             
-                synchronized( &completed )
-                {
-                    int count = 0;
-                    
-                    while( count < ( numThreads ) )
+                    done = false;
+                    synchronized(this)
                     {
-                        completed.wait( 30 );
-                        count++;
+                        synchronized(this)
+                        {
+                            this->wait();
+                            done = true;
+                        }
                     }
                 }
-             
-                int numComplete = 0;
-                for( int ix=0; ix<numThreads; ++ix ){
-                    if( threads[ix]->done ){
-                        numComplete++;
-                    }  
-                }
-                //printf("numComplete: %d, numThreads: %d\n", numComplete, numThreads );
-                CPPUNIT_ASSERT( numComplete == numThreads );
-             
-                // Delete all the threads.
-                for( int ix=0; ix<numThreads; ++ix ){
-                    threads[ix]->join();
-                    delete threads[ix];
+                catch(exceptions::ActiveMQException& ex)
+                {
+                    ex.setMark( __FILE__, __LINE__ );
                 }
-
-            }catch( exceptions::ActiveMQException& ex ){
-                ex.setMark( __FILE__, __LINE__ );
             }
-        }
+        };
 
-        void testRecursiveLock()
+        class MyDoubleLockThread
+        :
+            public Thread
         {
-            try{
-                Mutex mutex;
-             
-                const int numThreads = 30;
-                MyRecursiveLockThread* threads[numThreads];
-             
-                // Create and start all the threads.
-                for( int ix=0; ix<numThreads; ++ix ){
-                    threads[ix] = new MyRecursiveLockThread( &mutex );
-                    threads[ix]->start();
-                }
-             
-                // Sleep so all the threads can get to the wait.
-                Thread::sleep( 1000 );
-             
-                for( int ix=0; ix<numThreads; ++ix ){
-                    if( threads[ix]->done == true ){
-                        std::cout << "threads[" << ix 
-                                  << "] is done prematurely\n";
-                    }
-                    CPPUNIT_ASSERT( threads[ix]->done == false );            
-                }
-             
-                // Notify all threads.
-                synchronized( &mutex )
+
+        public:
+
+            bool done;
+            Mutex* mutex1;
+            Mutex* mutex2;
+
+        public:
+
+            int value;
+            MyDoubleLockThread(Mutex* mutex1, Mutex* mutex2)
+            {
+                this->mutex1 = mutex1;
+                this->mutex2 = mutex2;
+                done = false;
+            }
+
+            virtual ~MyDoubleLockThread(){}
+
+            virtual void run(){
+
+                try
                 {
-                    synchronized( &mutex )
-                    {
-                        mutex.notifyAll();
-                    }
+                   done = false;
+                   synchronized(mutex1)
+                   {
+                      synchronized(mutex2)
+                      {
+                         mutex2->wait();
+                         done = true;
+                      }
+                   }
                 }
-             
-                // Sleep to give the threads time to wake up.
-                Thread::sleep( 1000 );
-             
-                for( int ix=0; ix<numThreads; ++ix ){
-                    if( threads[ix]->done != true ){
-                        std::cout<< "threads[" << ix << "] is not done\n";
-                    }
-                    CPPUNIT_ASSERT( threads[ix]->done == true );            
+                catch(exceptions::ActiveMQException& ex)
+                {
+                   ex.setMark( __FILE__, __LINE__ );
                 }
+            }
+        };
 
-                // Delete all the threads.
-                for( int ix=0; ix<numThreads; ++ix ){
-                    delete threads[ix];
-                }
+    public:
 
-            }catch( exceptions::ActiveMQException& ex ){
-                ex.setMark( __FILE__, __LINE__ );
-            }
-        }
+        virtual ~MutexTest(){}
+        virtual void setUp(){}
+        virtual void tearDown(){}
 
-        void testDoubleLock()
-        {
-            try{
-                Mutex mutex1;
-                Mutex mutex2;
-
-                MyDoubleLockThread thread(&mutex1, &mutex2);
-    
-                thread.start();
-    
-                // Let the thread get both locks
-                Thread::sleep( 200 );
-    
-                // Lock mutex 2, thread is waiting on it
-                synchronized(&mutex2)
-                {
-                   mutex2.notify();
-                }
-    
-                // Let the thread die
-                thread.join();
-    
-                CPPUNIT_ASSERT( thread.done );
-            }catch( exceptions::ActiveMQException& ex ){
-                ex.setMark( __FILE__, __LINE__ );
-            }
-        }
+        void testTimedWait();
+        void testWait();
+        void test();
+        void testNotify();
+        void testNotifyAll();
+        void testRecursiveLock();
+        void testDoubleLock();
     };
-    
+
 }}
 
 #endif /*ACTIVEMQ_CONCURRENT_MUTEXTEST_H_*/



Mime
View raw message