A little cleanup. Moved class to SmartPtr.cpp and SmartPtr.h files.

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@1110 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
rich_sposato 2011-09-20 22:34:24 +00:00
parent 80a07d19e8
commit 16094ffe39
16 changed files with 450 additions and 344 deletions

View file

@ -6,24 +6,24 @@
<Option compiler="cygwin" />
<Build>
<Target title="Debug_GCC">
<Option output="obj\Debug_GCC\LevelMutex" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Debug_GCC\" />
<Option output="obj/Debug_GCC/LevelMutex" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug_GCC/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-W" />
<Add option="-g" />
<Add directory="..\..\include" />
<Add directory="..\..\include\loki" />
<Add option="-W" />
<Add directory="../../include" />
<Add directory="../../include/loki" />
</Compiler>
<Linker>
<Add library="..\..\lib\GCC\Loki_D.a" />
<Add library="..\..\..\PThreads\lib\pthreadVC2.lib" />
<Add library="../../lib/GCC/Loki_D.a" />
<Add library="/usr/lib/libpthread.so" />
</Linker>
</Target>
<Target title="Release_GCC">
<Option output="obj\Release_GCC\LevelMutex" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Release_GCC\" />
<Option output="obj/Release_GCC/LevelMutex" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release_GCC/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
@ -31,33 +31,33 @@
<Add option="-Os" />
<Add option="-O3" />
<Add option="-W" />
<Add directory="..\..\include" />
<Add directory="..\..\include\loki" />
<Add directory="../../include" />
<Add directory="../../include/loki" />
</Compiler>
<Linker>
<Add library="..\..\lib\GCC\Loki.a" />
<Add library="..\..\..\PThreads\lib\pthreadVC2.lib" />
<Add library="../../lib/GCC/Loki.a" />
<Add library="/usr/lib/libpthread.so" />
</Linker>
</Target>
<Target title="Debug_Cygwin">
<Option output="obj\Debug_Cygwin\LevelMutex" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Debug_Cygwin\" />
<Option output="obj/Debug_Cygwin/LevelMutex" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug_Cygwin/" />
<Option type="1" />
<Option compiler="cygwin" />
<Compiler>
<Add option="-W" />
<Add option="-g" />
<Add directory="..\..\include" />
<Add directory="..\..\include\loki" />
<Add directory="../../include" />
<Add directory="../../include/loki" />
</Compiler>
<Linker>
<Add library="..\..\lib\Cygwin\Loki_D.a" />
<Add library="..\..\..\PThreads\lib\pthreadVC2.lib" />
<Add library="../../lib/Cygwin/Loki_D.a" />
<Add library="../../../PThreads/lib/pthreadVC2.lib" />
</Linker>
</Target>
<Target title="Release_Cygwin">
<Option output="obj\Release_Cygwin\LevelMutex" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Release_Cygwin\" />
<Option output="obj/Release_Cygwin/LevelMutex" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release_Cygwin/" />
<Option type="1" />
<Option compiler="cygwin" />
<Compiler>
@ -65,12 +65,12 @@
<Add option="-Os" />
<Add option="-O3" />
<Add option="-W" />
<Add directory="..\..\include" />
<Add directory="..\..\include\loki" />
<Add directory="../../include" />
<Add directory="../../include/loki" />
</Compiler>
<Linker>
<Add library="..\..\lib\Cygwin\Loki.a" />
<Add library="..\..\..\PThreads\lib\pthreadVC2.lib" />
<Add library="../../lib/Cygwin/Loki.a" />
<Add library="../../../PThreads/lib/pthreadVC2.lib" />
</Linker>
</Target>
</Build>

View file

@ -4,9 +4,9 @@
// Copyright (c) 2008, 2009 Richard Sposato
// The copyright on this file is protected under the terms of the MIT license.
//
// Permission to use, copy, modify, distribute and sell this software for any
// purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this
// Permission to use, copy, modify, distribute and sell this software for any
// purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation.
//
// The author makes no representations about the suitability of this software
@ -22,7 +22,7 @@
#include "MultiThreadTests.hpp"
#include <assert.h>
#include <process.h>
//#include <process.h>
#include <stdlib.h>
#include <time.h>
@ -239,6 +239,8 @@ void * ValueUnsafeThread( void * p )
void MultiThreadSimpleTest( void )
{
cout << "Starting MultiThreadSimpleTest." << endl;
Thing::Init( 0 );
const unsigned int threadCount = 5;
ThreadPool pool( threadCount );
@ -262,6 +264,8 @@ void MultiThreadSimpleTest( void )
pool.JoinAll();
Thing::Destroy();
cout << "Finished MultiThreadSimpleTest." << endl;
}
// ----------------------------------------------------------------------------
@ -299,13 +303,17 @@ void * TryLockThread( void * p )
void MultiThreadTryLockTest( void )
{
cout << "Starting MultiThreadTryLockTest." << endl;
static const unsigned int threadCount = 3;
Thing::Init( 0 );
volatile Thing & thing = Thing::GetIt();
volatile SleepMutex & mutex = thing.GetMutex();
cout << endl << "Doing multi-threaded TryLock test. This test should not deadlock." << endl;
::system( "pause" );
cout << "Press enter key to continue." << endl;
char theKey = 0;
cin.get( theKey );
// 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.
MutexErrors::Type result = mutex.Lock();
@ -313,36 +321,46 @@ void MultiThreadTryLockTest( void )
bool okay = mutex.IsLockedByCurrentThread();
assert( okay );
thing.SetValue( threadCount );
ThreadPool pool( threadCount );
for ( unsigned int ii = 0; ii < threadCount; ++ii )
{
void * p = reinterpret_cast< void * >( ii );
pool.Start( TryLockThread, p );
ThreadPool pool( threadCount );
for ( unsigned int ii = 0; ii < threadCount; ++ii )
{
void * p = reinterpret_cast< void * >( ii );
pool.Start( TryLockThread, p );
}
pool.JoinAll();
}
pool.JoinAll();
const unsigned int value = thing.GetValue();
assert( value == threadCount );
result = mutex.Unlock();
assert( MutexErrors::Success == result );
okay = !mutex.IsLockedByCurrentThread();
assert( okay );
okay = !mutex.IsLocked();
assert( okay );
const unsigned int value = thing.GetValue();
assert( value == threadCount );
result = mutex.Unlock();
assert( MutexErrors::Success == result );
okay = !mutex.IsLockedByCurrentThread();
assert( okay );
okay = !mutex.IsLocked();
assert( okay );
Thing::Destroy();
cout << "Finished MultiThreadTryLockTest." << endl;
}
// ----------------------------------------------------------------------------
void MultiThreadReentrantTest( void )
{
cout << "Starting MultiThreadReentrantTest." << endl;
Thing::Init( 0 );
const unsigned int threadCount = 8;
TestResults::Create( threadCount );
ThreadPool pool( threadCount );
cout << endl << "Doing thread-safe value test. This test should pass and not deadlock." << endl;
::system( "pause" );
cout << "Press enter key to continue." << endl;
char theKey = 0;
cin.get( theKey );
for ( unsigned int ii = 0; ii < threadCount; ++ii )
{
void * p = reinterpret_cast< void * >( ii );
@ -352,7 +370,8 @@ void MultiThreadReentrantTest( void )
TestResults::GetIt()->OutputResults();
cout << endl << "Doing thread-unsafe value test. This test may fail." << endl;
::system( "pause" );
cout << "Press enter key to continue." << endl;
cin.get( theKey );
for ( unsigned int ii = 0; ii < threadCount; ++ii )
{
void * p = reinterpret_cast< void * >( ii );
@ -363,6 +382,8 @@ void MultiThreadReentrantTest( void )
TestResults::Destroy();
Thing::Destroy();
cout << "Finished MultiThreadReentrantTest." << endl;
}
// ----------------------------------------------------------------------------
@ -646,6 +667,8 @@ void * MultiLockUnsafeThread( void * p )
void MultiThreadMultiLockTest( void )
{
cout << "Starting MultiThreadMultiLockTest." << endl;
Thing::MakePool( thingCount );
const unsigned int threadCount = 8;
TestResults::Create( threadCount );
@ -674,6 +697,8 @@ void MultiThreadMultiLockTest( void )
TestResults::Destroy();
Thing::DestroyPool();
cout << "Finished MultiThreadMultiLockTest." << endl;
}
// ----------------------------------------------------------------------------
@ -808,6 +833,8 @@ void * MultiLockRandomUnsafeThread( void * p )
void MultiThreadRandomMultiLockTest( void )
{
cout << "Starting MultiThreadRandomMultiLockTest." << endl;
Thing::MakePool( thingCount );
const unsigned int threadCount = 8;
TestResults::Create( threadCount );
@ -836,6 +863,8 @@ void MultiThreadRandomMultiLockTest( void )
TestResults::Destroy();
Thing::DestroyPool();
cout << "Finished MultiThreadRandomMultiLockTest." << endl;
}
// ----------------------------------------------------------------------------
@ -936,6 +965,8 @@ void * UnsafeHierarchyTest( void * p )
void MultiThreadHierarchySingleLockTest( void )
{
cout << "Starting MultiThreadHierarchySingleLockTest." << endl;
LevelThing::MakePool( thingCount );
const unsigned int threadCount = 8;
TestResults::Create( threadCount );
@ -964,6 +995,8 @@ void MultiThreadHierarchySingleLockTest( void )
TestResults::Destroy();
LevelThing::DestroyPool();
cout << "Finished MultiThreadHierarchySingleLockTest." << endl;
}
// ----------------------------------------------------------------------------
@ -1065,6 +1098,8 @@ void * UnsafeHierarchyMultiLockTest( void * p )
void MultiThreadHierarchyMultiLockTest( void )
{
cout << "Starting MultiThreadHierarchyMultiLockTest." << endl;
MultiLevelPool::MakePool( 10, thingCount );
const unsigned int threadCount = 8;
TestResults::Create( threadCount );
@ -1093,6 +1128,8 @@ void MultiThreadHierarchyMultiLockTest( void )
TestResults::Destroy();
MultiLevelPool::DestroyPool();
cout << "Finished MultiThreadHierarchyMultiLockTest." << endl;
}
// ----------------------------------------------------------------------------

View file

@ -20,7 +20,7 @@
#include <assert.h>
#include <process.h>
//#include <process.h>
#if !defined( _MSC_VER )
#include <unistd.h> // needed for the usleep function.
#endif
@ -86,11 +86,13 @@ Thread::~Thread( void )
bool Thread::WaitForThread( void ) volatile
{
assert( IsValid( m_owner ) );
const volatile Thread * current = Thread::GetCurrentThread();
if ( this == current )
return false;
if ( m_status == Thread::Dead )
return false;
while ( this->m_status == Thread::Active )
{
// Call the wait policy.
@ -100,6 +102,8 @@ bool Thread::WaitForThread( void ) volatile
::usleep( 1000 );
#endif
}
m_status = Thread::Idle;
return true;
}

View file

@ -48,6 +48,8 @@ typedef ::Loki::LevelMutex< ::Loki::SpinLevelMutex, 1,
void SingleThreadSimpleTest( void )
{
cout << "Starting SingleThreadSimpleTest." << endl;
const unsigned int priorLevel = GetCurrentThreadsLevel();
const unsigned int priorLockCount = CountLocksInCurrentThread();
const unsigned int priorMutexCount = CountMutexesInCurrentThread();
@ -171,6 +173,8 @@ void SingleThreadSimpleTest( void )
assert( okay );
okay = ( CountMutexesAtCurrentLevel() == priorLevelMutexCount );
assert( okay );
cout << "Finished SingleThreadSimpleTest." << endl;
}
// ----------------------------------------------------------------------------
@ -178,6 +182,8 @@ void SingleThreadSimpleTest( void )
void SingleThreadReentrantTest( void )
{
cout << "Starting SingleThreadReentrantTest." << endl;
const unsigned int priorLevel = GetCurrentThreadsLevel();
const unsigned int priorLockCount = CountLocksInCurrentThread();
const unsigned int priorMutexCount = CountMutexesInCurrentThread();
@ -283,6 +289,8 @@ void SingleThreadReentrantTest( void )
assert( okay );
okay = ( CountMutexesAtCurrentLevel() == priorLevelMutexCount );
assert( okay );
cout << "Finished SingleThreadReentrantTest." << endl;
}
// ----------------------------------------------------------------------------
@ -290,6 +298,8 @@ void SingleThreadReentrantTest( void )
void SingleThreadSimpleMultiLockTest( void )
{
cout << "Starting SingleThreadSimpleMultiLockTest." << endl;
const unsigned int priorLevel = GetCurrentThreadsLevel();
const unsigned int priorLockCount = CountLocksInCurrentThread();
const unsigned int priorMutexCount = CountMutexesInCurrentThread();
@ -481,6 +491,8 @@ void SingleThreadSimpleMultiLockTest( void )
assert( okay );
okay = ( CountMutexesAtCurrentLevel() == priorLevelMutexCount );
assert( okay );
cout << "Finished SingleThreadSimpleMultiLockTest." << endl;
}
// ----------------------------------------------------------------------------
@ -488,6 +500,8 @@ void SingleThreadSimpleMultiLockTest( void )
void SingleThreadExceptionTest( void )
{
cout << "Starting SingleThreadExceptionTest." << endl;
const unsigned int priorLevel = GetCurrentThreadsLevel();
const unsigned int priorLockCount = CountLocksInCurrentThread();
const unsigned int priorMutexCount = CountMutexesInCurrentThread();
@ -636,6 +650,8 @@ void SingleThreadExceptionTest( void )
assert( okay );
okay = ( CountMutexesAtCurrentLevel() == priorLevelMutexCount );
assert( okay );
cout << "Finished SingleThreadExceptionTest." << endl;
}
// ----------------------------------------------------------------------------
@ -660,6 +676,7 @@ int main( int argc, const char * const argv[] )
MultiThreadSimpleTest();
MultiThreadTryLockTest();
cout << "main 679" << endl;
MultiThreadReentrantTest();
MultiThreadMultiLockTest();
MultiThreadRandomMultiLockTest();