You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2008/10/26 14:53:27 UTC
svn commit: r707992 -
/activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp
Author: tabish
Date: Sun Oct 26 06:53:26 2008
New Revision: 707992
URL: http://svn.apache.org/viewvc?rev=707992&view=rev
Log:
Test Code Cleanup
Modified:
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp
Modified: activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp?rev=707992&r1=707991&r2=707992&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp Sun Oct 26 06:53:26 2008
@@ -31,7 +31,7 @@
public:
- int value;
+ volatile int value;
MyThread(){ value = 0;}
virtual ~MyThread(){}
@@ -90,8 +90,9 @@
public:
- int value;
- MyWaitingThread(){ value = 0;}
+ volatile int value;
+ volatile bool started;
+ MyWaitingThread(){ value = 0; started = false; }
virtual ~MyWaitingThread(){}
virtual void lock() throw(lang::Exception){
mutex.lock();
@@ -116,9 +117,9 @@
try {
- synchronized(this) {
+ synchronized( this ) {
+ this->started = true;
this->wait();
- std::cout.flush();
value = value * 25;
}
@@ -136,7 +137,9 @@
MyWaitingThread test;
test.start();
- Thread::sleep( 1000 );
+ while( !test.started ) {
+ Thread::sleep(1);
+ }
synchronized( &test )
{
@@ -150,8 +153,10 @@
test.join();
CPPUNIT_ASSERT( test.value == 2500 );
+
} catch( lang::Exception& ex ) {
ex.setMark( __FILE__, __LINE__ );
+ CPPUNIT_ASSERT( false );
}
}
@@ -163,8 +168,10 @@
public:
- int value;
- MyTimedWaitingThread(){ value = 0;}
+ volatile int value;
+ volatile bool started;
+
+ MyTimedWaitingThread(){ value = 0; started = false; }
virtual ~MyTimedWaitingThread(){}
virtual void lock() throw(lang::Exception){
mutex.lock();
@@ -190,7 +197,8 @@
try {
synchronized( this ) {
- this->wait(2000);
+ this->started = true;
+ this->wait(1100);
value = 666;
}
} catch( lang::Exception& ex ) {
@@ -212,10 +220,11 @@
time_t delta = endTime - startTime;
- CPPUNIT_ASSERT( delta >= 1 && delta <= 3 );
+ CPPUNIT_ASSERT( delta >= 1 && delta <= 2 );
} catch(lang::Exception& ex) {
std::cout << ex.getMessage() << std::endl;
+ CPPUNIT_ASSERT( false );
}
}
@@ -223,7 +232,7 @@
class MyNotifiedThread : public lang::Thread, public Synchronizable{
public:
- bool done;
+ volatile bool done;
Mutex* mutex;
Mutex* started;
Mutex* completed;
@@ -301,7 +310,7 @@
int count = 0;
while( count < ( numThreads ) ) {
- started.wait( 30 );
+ started.wait( 40 );
count++;
}
}
@@ -310,7 +319,7 @@
mutex.notify();
}
- Thread::sleep( 1000 );
+ Thread::sleep( 1200 );
int counter = 0;
for( int ix=0; ix<numThreads; ++ix ){
@@ -333,7 +342,7 @@
int count = 0;
while( count < ( numThreads ) ) {
- started.wait( 30 );
+ started.wait( 40 );
count++;
}
}
@@ -434,69 +443,64 @@
}catch( lang::Exception& ex ){
ex.setMark( __FILE__, __LINE__ );
+ CPPUNIT_ASSERT( false );
}
}
///////////////////////////////////////////////////////////////////////////////
-class MyRecursiveLockThread
-:
- public lang::Thread,
- public Synchronizable{
-
+class MyRecursiveLockThread : public lang::Thread, public Synchronizable{
public:
- bool done;
- Mutex* mutex;
+ volatile bool done;
+ Mutex* mutex;
public:
- int value;
- MyRecursiveLockThread(Mutex* mutex){ this->mutex = mutex; done = false; }
- virtual ~MyRecursiveLockThread(){}
- virtual void lock() throw(lang::Exception){
- mutex->lock();
- }
- virtual void unlock() throw(lang::Exception){
- mutex->unlock();
- }
- virtual void wait() throw(lang::Exception){
- mutex->wait();
- }
- virtual void wait(unsigned long millisecs) throw(lang::Exception){
- mutex->wait( millisecs );
- }
- virtual void notify() throw(lang::Exception){
- mutex->notify();
- }
- virtual void notifyAll() throw(lang::Exception){
- mutex->notifyAll();
- }
-
- virtual void run(){
-
- try
- {
- done = false;
- synchronized(this)
- {
- synchronized(this)
- {
- this->wait();
- done = true;
+ volatile int value;
+ MyRecursiveLockThread(Mutex* mutex){ this->mutex = mutex; done = false; }
+ virtual ~MyRecursiveLockThread(){}
+ virtual void lock() throw(lang::Exception){
+ mutex->lock();
+ }
+ virtual void unlock() throw(lang::Exception){
+ mutex->unlock();
+ }
+ virtual void wait() throw(lang::Exception){
+ mutex->wait();
+ }
+ virtual void wait(unsigned long millisecs) throw(lang::Exception){
+ mutex->wait( millisecs );
+ }
+ virtual void notify() throw(lang::Exception){
+ mutex->notify();
+ }
+ virtual void notifyAll() throw(lang::Exception){
+ mutex->notifyAll();
+ }
+
+ virtual void run(){
+
+ try {
+
+ done = false;
+ synchronized(this) {
+ synchronized(this) {
+ this->wait();
+ done = true;
+ }
}
- }
- }
- catch(lang::Exception& ex)
- {
- ex.setMark( __FILE__, __LINE__ );
- }
- }
+
+ } catch(lang::Exception& ex) {
+ ex.setMark( __FILE__, __LINE__ );
+ }
+ }
};
////////////////////////////////////////////////////////////////////////////////
-void MutexTest::testRecursiveLock()
-{
- try{
+void MutexTest::testRecursiveLock() {
+
+ try {
+
Mutex mutex;
const int numThreads = 30;
@@ -509,7 +513,7 @@
}
// Sleep so all the threads can get to the wait.
- Thread::sleep( 1000 );
+ Thread::sleep( 1100 );
for( int ix=0; ix<numThreads; ++ix ){
if( threads[ix]->done == true ){
@@ -520,16 +524,14 @@
}
// Notify all threads.
- synchronized( &mutex )
- {
- synchronized( &mutex )
- {
+ synchronized( &mutex ) {
+ synchronized( &mutex ) {
mutex.notifyAll();
}
}
// Sleep to give the threads time to wake up.
- Thread::sleep( 1000 );
+ Thread::sleep( 1100 );
for( int ix=0; ix<numThreads; ++ix ){
if( threads[ix]->done != true ){
@@ -545,58 +547,52 @@
}catch( lang::Exception& ex ){
ex.setMark( __FILE__, __LINE__ );
+ CPPUNIT_ASSERT( false );
}
}
///////////////////////////////////////////////////////////////////////////////
-class MyDoubleLockThread
-:
- public lang::Thread
-{
-
+class MyDoubleLockThread : public lang::Thread {
public:
- bool done;
- Mutex* mutex1;
- Mutex* mutex2;
+ volatile 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;
+ volatile 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(lang::Exception& ex)
- {
- ex.setMark( __FILE__, __LINE__ );
- }
- }
+
+ } catch(lang::Exception& ex) {
+ ex.setMark( __FILE__, __LINE__ );
+ }
+ }
};
////////////////////////////////////////////////////////////////////////////////
-void MutexTest::testDoubleLock()
-{
- try{
+void MutexTest::testDoubleLock() {
+
+ try {
+
Mutex mutex1;
Mutex mutex2;
@@ -605,11 +601,10 @@
thread.start();
// Let the thread get both locks
- Thread::sleep( 200 );
+ Thread::sleep( 300 );
// Lock mutex 2, thread is waiting on it
- synchronized(&mutex2)
- {
+ synchronized( &mutex2 ) {
mutex2.notify();
}
@@ -617,18 +612,19 @@
thread.join();
CPPUNIT_ASSERT( thread.done );
+
}catch( lang::Exception& ex ){
ex.setMark( __FILE__, __LINE__ );
+ CPPUNIT_ASSERT( false );
}
}
///////////////////////////////////////////////////////////////////////////////
-class MyStoppableThread : public lang::Runnable
-{
+class MyStoppableThread : public lang::Runnable {
public:
- bool started;
- bool closed;
+ volatile bool started;
+ volatile bool closed;
Mutex mutex;
lang::Thread* thread;
util::Random rand;
@@ -719,8 +715,10 @@
if( !isStarted() ) {
mutex.notifyAll();
- // Wait for more data or to be woken up.
- mutex.wait();
+ if( !closed ) {
+ // Wait for more data or to be woken up.
+ mutex.wait();
+ }
}
}
}
@@ -732,6 +730,7 @@
////////////////////////////////////////////////////////////////////////////////
void MutexTest::testStressMutex(){
+
MyStoppableThread tester;
tester.start();