Moved constructions of ThreadPool into blocks within functions. Added cout lines to say when tests start and finish.

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@1167 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
rich_sposato 2011-10-22 00:29:15 +00:00
parent ab47363c9b
commit 16bfa36172

View file

@ -22,11 +22,10 @@
#include "MultiThreadTests.hpp" #include "MultiThreadTests.hpp"
#include <assert.h> #include <assert.h>
#include <process.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <SafeFormat.h> #include <loki/SafeFormat.h>
#include "ThreadPool.hpp" #include "ThreadPool.hpp"
#include "Thing.hpp" #include "Thing.hpp"
@ -239,29 +238,40 @@ void * ValueUnsafeThread( void * p )
void MultiThreadSimpleTest( void ) void MultiThreadSimpleTest( void )
{ {
cout << "Starting MultiThreadSimpleTest." << endl;
char ender;
Thing::Init( 0 ); Thing::Init( 0 );
const unsigned int threadCount = 5; const unsigned int threadCount = 5;
ThreadPool pool( threadCount );
cout << endl << "Doing thread-locked print test. This test should pass. and not deadlock" << endl; {
::system( "pause" ); ThreadPool pool( threadCount );
cout << "Doing thread-locked print test. This test should pass and not deadlock" << endl;
cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
pool.Start( PrintSafeThread, p ); pool.Start( PrintSafeThread, p );
} }
pool.JoinAll(); pool.JoinAll();
}
cout << endl << "Doing thread-unsafe print test. This test may fail." << endl; {
::system( "pause" ); ThreadPool pool( threadCount );
cout << endl << "Doing thread-unsafe print test. This test may fail, but not deadlock." << endl;
cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
pool.Start( PrintUnsafeThread, p ); pool.Start( PrintUnsafeThread, p );
} }
pool.JoinAll(); pool.JoinAll();
}
Thing::Destroy(); Thing::Destroy();
cout << "Finished MultiThreadSimpleTest." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -289,6 +299,7 @@ void * TryLockThread( void * p )
} }
const unsigned int gotValue = thing.GetValue(); const unsigned int gotValue = thing.GetValue();
assert( gotValue != value ); assert( gotValue != value );
(void)gotValue;
} }
assert( mutex.IsLockedByAnotherThread() ); assert( mutex.IsLockedByAnotherThread() );
@ -299,13 +310,13 @@ void * TryLockThread( void * p )
void MultiThreadTryLockTest( void ) void MultiThreadTryLockTest( void )
{ {
cout << "Starting MultiThreadTryLockTest." << endl;
char ender;
static const unsigned int threadCount = 3; static const unsigned int threadCount = 3;
Thing::Init( 0 ); Thing::Init( 0 );
volatile Thing & thing = Thing::GetIt(); volatile Thing & thing = Thing::GetIt();
volatile SleepMutex & mutex = thing.GetMutex(); volatile SleepMutex & mutex = thing.GetMutex();
cout << endl << "Doing multi-threaded TryLock test. This test should not deadlock." << endl;
::system( "pause" );
// First step is to lock the mutex in the main thread so no child thread // First step is to lock the mutex in the main thread so no child thread
// can ever lock it, change the value, or anything like that. // can ever lock it, change the value, or anything like that.
MutexErrors::Type result = mutex.Lock(); MutexErrors::Type result = mutex.Lock();
@ -313,7 +324,12 @@ void MultiThreadTryLockTest( void )
bool okay = mutex.IsLockedByCurrentThread(); bool okay = mutex.IsLockedByCurrentThread();
assert( okay ); assert( okay );
thing.SetValue( threadCount ); thing.SetValue( threadCount );
{
ThreadPool pool( threadCount ); ThreadPool pool( threadCount );
cout << endl << "Doing multi-threaded TryLock test. This test should not deadlock." << endl;
cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
@ -322,6 +338,8 @@ void MultiThreadTryLockTest( void )
pool.JoinAll(); pool.JoinAll();
const unsigned int value = thing.GetValue(); const unsigned int value = thing.GetValue();
assert( value == threadCount ); assert( value == threadCount );
}
result = mutex.Unlock(); result = mutex.Unlock();
assert( MutexErrors::Success == result ); assert( MutexErrors::Success == result );
okay = !mutex.IsLockedByCurrentThread(); okay = !mutex.IsLockedByCurrentThread();
@ -330,19 +348,26 @@ void MultiThreadTryLockTest( void )
assert( okay ); assert( okay );
Thing::Destroy(); Thing::Destroy();
cout << "Finished MultiThreadTryLockTest." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void MultiThreadReentrantTest( void ) void MultiThreadReentrantTest( void )
{ {
cout << "Starting MultiThreadReentrantTest." << endl;
char ender;
Thing::Init( 0 ); Thing::Init( 0 );
const unsigned int threadCount = 8; const unsigned int threadCount = 8;
TestResults::Create( threadCount ); TestResults::Create( threadCount );
ThreadPool pool( threadCount );
{
ThreadPool pool( threadCount );
cout << endl << "Doing thread-safe value test. This test should pass and not deadlock." << endl; cout << endl << "Doing thread-safe value test. This test should pass and not deadlock." << endl;
::system( "pause" ); cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
@ -350,9 +375,13 @@ void MultiThreadReentrantTest( void )
} }
pool.JoinAll(); pool.JoinAll();
TestResults::GetIt()->OutputResults(); TestResults::GetIt()->OutputResults();
}
{
ThreadPool pool( threadCount );
cout << endl << "Doing thread-unsafe value test. This test may fail." << endl; cout << endl << "Doing thread-unsafe value test. This test may fail." << endl;
::system( "pause" ); cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
@ -360,9 +389,12 @@ void MultiThreadReentrantTest( void )
} }
pool.JoinAll(); pool.JoinAll();
TestResults::GetIt()->OutputResults(); TestResults::GetIt()->OutputResults();
}
TestResults::Destroy(); TestResults::Destroy();
Thing::Destroy(); Thing::Destroy();
cout << "Finished MultiThreadReentrantTest." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -567,6 +599,7 @@ void * MultiLockSafeThread( void * p )
bool okay = locker.Unlock(); bool okay = locker.Unlock();
assert( okay ); assert( okay );
(void)okay;
::GoToSleep( 2 ); ::GoToSleep( 2 );
} }
} }
@ -646,13 +679,18 @@ void * MultiLockUnsafeThread( void * p )
void MultiThreadMultiLockTest( void ) void MultiThreadMultiLockTest( void )
{ {
cout << "Starting MultiThreadMultiLockTest." << endl;
char ender;
Thing::MakePool( thingCount ); Thing::MakePool( thingCount );
const unsigned int threadCount = 8; const unsigned int threadCount = 8;
TestResults::Create( threadCount ); TestResults::Create( threadCount );
ThreadPool pool( threadCount );
{
ThreadPool pool( threadCount );
cout << endl << "Doing thread-safe multi-lock test. This test should pass and not deadlock." << endl; cout << endl << "Doing thread-safe multi-lock test. This test should pass and not deadlock." << endl;
::system( "pause" ); cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
@ -660,10 +698,14 @@ void MultiThreadMultiLockTest( void )
} }
pool.JoinAll(); pool.JoinAll();
TestResults::GetIt()->OutputResults(); TestResults::GetIt()->OutputResults();
}
{
ThreadPool pool( threadCount );
cout << endl << "Doing thread-unsafe multi-lock test. This test may fail." << endl; cout << endl << "Doing thread-unsafe multi-lock test. This test may fail." << endl;
TestResults::GetIt()->Reset( threadCount ); TestResults::GetIt()->Reset( threadCount );
::system( "pause" ); cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
@ -671,9 +713,12 @@ void MultiThreadMultiLockTest( void )
} }
pool.JoinAll(); pool.JoinAll();
TestResults::GetIt()->OutputResults(); TestResults::GetIt()->OutputResults();
}
TestResults::Destroy(); TestResults::Destroy();
Thing::DestroyPool(); Thing::DestroyPool();
cout << "Finished MultiThreadMultiLockTest." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -770,7 +815,7 @@ void * MultiLockRandomUnsafeThread( void * p )
place += ::rand() % 3; place += ::rand() % 3;
if ( thingCount <= place ) if ( thingCount <= place )
break; break;
thing = thing = const_cast< Thing * >( Thing::GetFromPool( place ) ); thing = const_cast< Thing * >( Thing::GetFromPool( place ) );
assert( nullptr != thing ); assert( nullptr != thing );
pool.push_back( thing ); pool.push_back( thing );
} }
@ -808,13 +853,18 @@ void * MultiLockRandomUnsafeThread( void * p )
void MultiThreadRandomMultiLockTest( void ) void MultiThreadRandomMultiLockTest( void )
{ {
cout << "Starting MultiThreadRandomMultiLockTest." << endl;
char ender;
Thing::MakePool( thingCount ); Thing::MakePool( thingCount );
const unsigned int threadCount = 8; const unsigned int threadCount = 8;
TestResults::Create( threadCount ); TestResults::Create( threadCount );
ThreadPool pool( threadCount );
{
ThreadPool pool( threadCount );
cout << endl << "Doing thread-safe random multi-lock test. This test should pass and not deadlock." << endl; cout << endl << "Doing thread-safe random multi-lock test. This test should pass and not deadlock." << endl;
::system( "pause" ); cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
@ -822,10 +872,14 @@ void MultiThreadRandomMultiLockTest( void )
} }
pool.JoinAll(); pool.JoinAll();
TestResults::GetIt()->OutputResults(); TestResults::GetIt()->OutputResults();
}
{
ThreadPool pool( threadCount );
cout << endl << "Doing thread-unsafe random multi-lock test. This test may fail." << endl; cout << endl << "Doing thread-unsafe random multi-lock test. This test may fail." << endl;
TestResults::GetIt()->Reset( threadCount ); TestResults::GetIt()->Reset( threadCount );
::system( "pause" ); cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
@ -833,9 +887,12 @@ void MultiThreadRandomMultiLockTest( void )
} }
pool.JoinAll(); pool.JoinAll();
TestResults::GetIt()->OutputResults(); TestResults::GetIt()->OutputResults();
}
TestResults::Destroy(); TestResults::Destroy();
Thing::DestroyPool(); Thing::DestroyPool();
cout << "Finished MultiThreadRandomMultiLockTest." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -936,13 +993,18 @@ void * UnsafeHierarchyTest( void * p )
void MultiThreadHierarchySingleLockTest( void ) void MultiThreadHierarchySingleLockTest( void )
{ {
cout << "Starting MultiThreadHierarchySingleLockTest." << endl;
char ender;
LevelThing::MakePool( thingCount ); LevelThing::MakePool( thingCount );
const unsigned int threadCount = 8; const unsigned int threadCount = 8;
TestResults::Create( threadCount ); TestResults::Create( threadCount );
ThreadPool pool( threadCount );
{
ThreadPool pool( threadCount );
cout << endl << "Doing thread-safe hierarchy test. This test should pass and not deadlock." << endl; cout << endl << "Doing thread-safe hierarchy test. This test should pass and not deadlock." << endl;
::system( "pause" ); cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
@ -950,9 +1012,13 @@ void MultiThreadHierarchySingleLockTest( void )
} }
pool.JoinAll(); pool.JoinAll();
TestResults::GetIt()->OutputResults(); TestResults::GetIt()->OutputResults();
}
{
ThreadPool pool( threadCount );
cout << endl << "Doing thread-unsafe hierarchy test. This test may fail." << endl; cout << endl << "Doing thread-unsafe hierarchy test. This test may fail." << endl;
::system( "pause" ); cout << "Press <Enter> key to start test. ";
cin.get( ender );
TestResults::GetIt()->Reset( threadCount ); TestResults::GetIt()->Reset( threadCount );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
@ -961,9 +1027,12 @@ void MultiThreadHierarchySingleLockTest( void )
} }
pool.JoinAll(); pool.JoinAll();
TestResults::GetIt()->OutputResults(); TestResults::GetIt()->OutputResults();
}
TestResults::Destroy(); TestResults::Destroy();
LevelThing::DestroyPool(); LevelThing::DestroyPool();
cout << "Finished MultiThreadHierarchySingleLockTest." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -1064,14 +1133,18 @@ void * UnsafeHierarchyMultiLockTest( void * p )
void MultiThreadHierarchyMultiLockTest( void ) void MultiThreadHierarchyMultiLockTest( void )
{ {
cout << "Starting MultiThreadHierarchyMultiLockTest." << endl;
char ender;
MultiLevelPool::MakePool( 10, thingCount ); MultiLevelPool::MakePool( 10, thingCount );
const unsigned int threadCount = 8; const unsigned int threadCount = 8;
TestResults::Create( threadCount ); TestResults::Create( threadCount );
ThreadPool pool( threadCount );
{
ThreadPool pool( threadCount );
cout << endl << "Doing thread-safe multilock hierarchy test. This test should pass and not deadlock." << endl; cout << endl << "Doing thread-safe multilock hierarchy test. This test should pass and not deadlock." << endl;
::system( "pause" ); cout << "Press <Enter> key to start test. ";
cin.get( ender );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
void * p = reinterpret_cast< void * >( ii ); void * p = reinterpret_cast< void * >( ii );
@ -1079,9 +1152,13 @@ void MultiThreadHierarchyMultiLockTest( void )
} }
pool.JoinAll(); pool.JoinAll();
TestResults::GetIt()->OutputResults(); TestResults::GetIt()->OutputResults();
}
{
ThreadPool pool( threadCount );
cout << endl << "Doing thread-unsafe multilock hierarchy test. This test may fail." << endl; cout << endl << "Doing thread-unsafe multilock hierarchy test. This test may fail." << endl;
::system( "pause" ); cout << "Press <Enter> key to start test. ";
cin.get( ender );
TestResults::GetIt()->Reset( threadCount ); TestResults::GetIt()->Reset( threadCount );
for ( unsigned int ii = 0; ii < threadCount; ++ii ) for ( unsigned int ii = 0; ii < threadCount; ++ii )
{ {
@ -1090,9 +1167,12 @@ void MultiThreadHierarchyMultiLockTest( void )
} }
pool.JoinAll(); pool.JoinAll();
TestResults::GetIt()->OutputResults(); TestResults::GetIt()->OutputResults();
}
TestResults::Destroy(); TestResults::Destroy();
MultiLevelPool::DestroyPool(); MultiLevelPool::DestroyPool();
cout << "Finished MultiThreadHierarchyMultiLockTest." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------