Replaced tabs with spaces.
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@1132 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
965a15a5f0
commit
6b42268ba9
1 changed files with 110 additions and 106 deletions
|
@ -33,10 +33,10 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <loki/SafeFormat.h>
|
#include <loki/SafeFormat.h>
|
||||||
#include "ThreadPool.hpp"
|
#include "ThreadPool.hpp"
|
||||||
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
static unsigned int g = 0;
|
static unsigned int g = 0;
|
||||||
|
|
||||||
|
@ -56,27 +56,27 @@ class LockableObject : public ::Loki::ObjectLevelLockable< LockableObject >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef ::Loki::ObjectLevelLockable< LockableObject > BaseClass;
|
typedef ::Loki::ObjectLevelLockable< LockableObject > BaseClass;
|
||||||
|
|
||||||
explicit LockableObject( unsigned int index ) :
|
explicit LockableObject( unsigned int index ) :
|
||||||
BaseClass(), m_index( index ), m_value( ObjectCount ) {}
|
BaseClass(), m_index( index ), m_value( ObjectCount ) {}
|
||||||
|
|
||||||
~LockableObject( void ) {}
|
~LockableObject( void ) {}
|
||||||
|
|
||||||
unsigned int GetIndex( void ) const { return m_index; }
|
unsigned int GetIndex( void ) const { return m_index; }
|
||||||
|
|
||||||
unsigned int GetValue( void ) const { return m_value; }
|
unsigned int GetValue( void ) const { return m_value; }
|
||||||
|
|
||||||
void SetValue( unsigned int value ) { m_value = value; }
|
void SetValue( unsigned int value ) { m_value = value; }
|
||||||
|
|
||||||
void DoSomething( void );
|
void DoSomething( void );
|
||||||
|
|
||||||
void Print( unsigned int threadIndex );
|
void Print( unsigned int threadIndex );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
const unsigned int m_index;
|
const unsigned int m_index;
|
||||||
unsigned int m_value;
|
unsigned int m_value;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -84,18 +84,20 @@ private:
|
||||||
|
|
||||||
void LockableObject::DoSomething( void)
|
void LockableObject::DoSomething( void)
|
||||||
{
|
{
|
||||||
assert( NULL != this );
|
assert( NULL != this );
|
||||||
DO;
|
DO;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
void LockableObject::Print( unsigned int threadIndex )
|
void LockableObject::Print( unsigned int threadIndex )
|
||||||
{
|
{
|
||||||
assert( NULL != this );
|
assert( NULL != this );
|
||||||
const char * message = ( threadIndex != m_value ) ? "Mismatch!" : "";
|
const char * result = ( threadIndex != m_value ) ? "Mismatch!" : "";
|
||||||
::Loki::Printf( "Object: [%u] Thread: [%u] Value: [%u] %s\n" )
|
::std::string message;
|
||||||
( m_index )( threadIndex )( m_value )( message );
|
::Loki::SPrintf( message, "Object: [%u] Thread: [%u] Value: [%u] %s\n" )
|
||||||
|
( m_index )( threadIndex )( m_value )( result );
|
||||||
|
cout << message;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
@ -104,20 +106,20 @@ typedef ::std::vector< LockableObject * > LockableObjects;
|
||||||
|
|
||||||
LockableObjects & GetLockableObjects( void )
|
LockableObjects & GetLockableObjects( void )
|
||||||
{
|
{
|
||||||
static LockableObjects objects;
|
static LockableObjects objects;
|
||||||
return objects;
|
return objects;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
LockableObject * GetLockableObject( unsigned int index )
|
LockableObject * GetLockableObject( unsigned int index )
|
||||||
{
|
{
|
||||||
LockableObjects & objects = GetLockableObjects();
|
LockableObjects & objects = GetLockableObjects();
|
||||||
if ( objects.size() <= index )
|
if ( objects.size() <= index )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
LockableObject * object = objects[ index ];
|
LockableObject * object = objects[ index ];
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
@ -127,40 +129,40 @@ void * RunObjectTest( void * p )
|
||||||
const unsigned int threadIndex = reinterpret_cast< unsigned int >( p );
|
const unsigned int threadIndex = reinterpret_cast< unsigned int >( p );
|
||||||
assert( threadIndex < ThreadCount );
|
assert( threadIndex < ThreadCount );
|
||||||
|
|
||||||
unsigned int failCount = 0;
|
unsigned int failCount = 0;
|
||||||
for ( unsigned int ii = 0; ii < ObjectCount; ++ii )
|
for ( unsigned int ii = 0; ii < ObjectCount; ++ii )
|
||||||
{
|
{
|
||||||
LockableObject * object = GetLockableObject( ii );
|
LockableObject * object = GetLockableObject( ii );
|
||||||
assert( NULL != object );
|
assert( NULL != object );
|
||||||
LockableObject::Lock lock( *object );
|
LockableObject::Lock lock( *object );
|
||||||
(void)lock;
|
(void)lock;
|
||||||
object->SetValue( threadIndex );
|
object->SetValue( threadIndex );
|
||||||
object->DoSomething();
|
object->DoSomething();
|
||||||
object->Print( threadIndex );
|
object->Print( threadIndex );
|
||||||
object->DoSomething();
|
object->DoSomething();
|
||||||
const unsigned int value = object->GetValue();
|
const unsigned int value = object->GetValue();
|
||||||
if ( value != threadIndex )
|
if ( value != threadIndex )
|
||||||
++failCount;
|
++failCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
FailCounts[ threadIndex ] = failCount;
|
FailCounts[ threadIndex ] = failCount;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
void DoObjectLockTest( void )
|
void DoObjectLockTest( void )
|
||||||
{
|
{
|
||||||
cout << "Starting DoObjectLockTest" << endl;
|
cout << "Starting DoObjectLockTest" << endl;
|
||||||
|
|
||||||
LockableObjects & objects = GetLockableObjects();
|
LockableObjects & objects = GetLockableObjects();
|
||||||
objects.reserve( ObjectCount );
|
objects.reserve( ObjectCount );
|
||||||
for ( unsigned int ii = 0; ii < ObjectCount; ++ii )
|
for ( unsigned int ii = 0; ii < ObjectCount; ++ii )
|
||||||
{
|
{
|
||||||
LockableObject * object = new LockableObject( ii );
|
LockableObject * object = new LockableObject( ii );
|
||||||
objects.push_back( object );
|
objects.push_back( object );
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
ThreadPool pool;
|
ThreadPool pool;
|
||||||
|
@ -169,17 +171,17 @@ void DoObjectLockTest( void )
|
||||||
pool.Join();
|
pool.Join();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int totalFails = 0;
|
unsigned int totalFails = 0;
|
||||||
for ( unsigned int ii = 0; ii < ThreadCount; ++ii )
|
for ( unsigned int ii = 0; ii < ThreadCount; ++ii )
|
||||||
{
|
{
|
||||||
const unsigned int failCount = FailCounts[ ii ];
|
const unsigned int failCount = FailCounts[ ii ];
|
||||||
::Loki::Printf( "Thread: [%u] Failures: [%u]\n" )( ii )( failCount );
|
::Loki::Printf( "Thread: [%u] Failures: [%u]\n" )( ii )( failCount );
|
||||||
totalFails += failCount;
|
totalFails += failCount;
|
||||||
}
|
}
|
||||||
const char * result = ( 0 == totalFails ) ? "Passed" : "FAILED";
|
const char * result = ( 0 == totalFails ) ? "Passed" : "FAILED";
|
||||||
|
|
||||||
cout << "Finished DoObjectLockTest. Total Fails: " << totalFails << " Result: "
|
cout << "Finished DoObjectLockTest. Total Fails: " << totalFails << " Result: "
|
||||||
<< result << endl;
|
<< result << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
@ -188,31 +190,33 @@ class LockableClass : public ::Loki::ClassLevelLockable< LockableClass >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef ::Loki::ClassLevelLockable< LockableClass > BaseClass;
|
typedef ::Loki::ClassLevelLockable< LockableClass > BaseClass;
|
||||||
|
|
||||||
explicit LockableClass( unsigned int index ) : BaseClass(), m_index( index ) {}
|
explicit LockableClass( unsigned int index ) : BaseClass(), m_index( index ) {}
|
||||||
|
|
||||||
~LockableClass( void ) {}
|
~LockableClass( void ) {}
|
||||||
|
|
||||||
unsigned int GetIndex( void ) const { return m_index; }
|
unsigned int GetIndex( void ) const { return m_index; }
|
||||||
|
|
||||||
void Print( unsigned int threadIndex );
|
void Print( unsigned int threadIndex );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const unsigned int m_index;
|
/// Assignment operator is not implemented.
|
||||||
|
LockableClass & operator = ( const LockableClass & );
|
||||||
|
const unsigned int m_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
void LockableClass::Print( unsigned int threadIndex )
|
void LockableClass::Print( unsigned int threadIndex )
|
||||||
{
|
{
|
||||||
assert( NULL != this );
|
assert( NULL != this );
|
||||||
DO; ::Loki::Printf( "%u: %u: -----\n" )( m_index )( threadIndex );
|
DO; ::Loki::Printf( "%u: %u: -----\n" )( m_index )( threadIndex );
|
||||||
DO; ::Loki::Printf( "%u: %u: ----\n" )( m_index )( threadIndex );
|
DO; ::Loki::Printf( "%u: %u: ----\n" )( m_index )( threadIndex );
|
||||||
DO; ::Loki::Printf( "%u: %u: ---\n" )( m_index )( threadIndex );
|
DO; ::Loki::Printf( "%u: %u: ---\n" )( m_index )( threadIndex );
|
||||||
DO; ::Loki::Printf( "%u: %u: --\n" )( m_index )( threadIndex );
|
DO; ::Loki::Printf( "%u: %u: --\n" )( m_index )( threadIndex );
|
||||||
DO; ::Loki::Printf( "%u: %u: -\n" )( m_index )( threadIndex );
|
DO; ::Loki::Printf( "%u: %u: -\n" )( m_index )( threadIndex );
|
||||||
DO; ::Loki::Printf( "%u: %u: \n" )( m_index )( threadIndex );
|
DO; ::Loki::Printf( "%u: %u:\n" )( m_index )( threadIndex );
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
@ -221,20 +225,20 @@ typedef ::std::vector< LockableClass * > LockableClasses;
|
||||||
|
|
||||||
LockableClasses & GetLockableClasses( void )
|
LockableClasses & GetLockableClasses( void )
|
||||||
{
|
{
|
||||||
static LockableClasses objects;
|
static LockableClasses objects;
|
||||||
return objects;
|
return objects;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
LockableClass * GetLockableClass( unsigned int index )
|
LockableClass * GetLockableClass( unsigned int index )
|
||||||
{
|
{
|
||||||
LockableClasses & objects = GetLockableClasses();
|
LockableClasses & objects = GetLockableClasses();
|
||||||
if ( objects.size() <= index )
|
if ( objects.size() <= index )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
LockableClass * object = objects[ index ];
|
LockableClass * object = objects[ index ];
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
@ -244,31 +248,31 @@ void * RunClassTest( void * p )
|
||||||
const unsigned int threadIndex = reinterpret_cast< unsigned int >( p );
|
const unsigned int threadIndex = reinterpret_cast< unsigned int >( p );
|
||||||
assert( threadIndex < ThreadCount );
|
assert( threadIndex < ThreadCount );
|
||||||
|
|
||||||
for ( unsigned int ii = 0; ii < ClassCount; ++ii )
|
for ( unsigned int ii = 0; ii < ClassCount; ++ii )
|
||||||
{
|
{
|
||||||
LockableClass * object = GetLockableClass( ii );
|
LockableClass * object = GetLockableClass( ii );
|
||||||
assert( NULL != object );
|
assert( NULL != object );
|
||||||
LockableClass::Lock lock( *object );
|
LockableClass::Lock lock( *object );
|
||||||
(void)lock;
|
(void)lock;
|
||||||
object->Print( threadIndex );
|
object->Print( threadIndex );
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
void DoClassLockTest( void )
|
void DoClassLockTest( void )
|
||||||
{
|
{
|
||||||
cout << "Starting DoClassLockTest" << endl;
|
cout << "Starting DoClassLockTest" << endl;
|
||||||
|
|
||||||
LockableClasses & objects = GetLockableClasses();
|
LockableClasses & objects = GetLockableClasses();
|
||||||
objects.reserve( ClassCount );
|
objects.reserve( ClassCount );
|
||||||
for ( unsigned int ii = 0; ii < ClassCount; ++ii )
|
for ( unsigned int ii = 0; ii < ClassCount; ++ii )
|
||||||
{
|
{
|
||||||
LockableClass * object = new LockableClass( ii );
|
LockableClass * object = new LockableClass( ii );
|
||||||
objects.push_back( object );
|
objects.push_back( object );
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
ThreadPool pool;
|
ThreadPool pool;
|
||||||
|
@ -277,24 +281,24 @@ void DoClassLockTest( void )
|
||||||
pool.Join();
|
pool.Join();
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "Finished DoClassLockTest" << endl;
|
cout << "Finished DoClassLockTest" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
int main( int argc, const char * const argv[] )
|
int main( int argc, const char * const argv[] )
|
||||||
{
|
{
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
char ender;
|
char ender;
|
||||||
|
|
||||||
DoObjectLockTest();
|
DoObjectLockTest();
|
||||||
cout << "Press <Enter> key to continue. ";
|
cout << "Press <Enter> key to continue. ";
|
||||||
cin.get( ender );
|
cin.get( ender );
|
||||||
|
|
||||||
DoClassLockTest();
|
DoClassLockTest();
|
||||||
cout << "Press <Enter> key to finish. ";
|
cout << "Press <Enter> key to finish. ";
|
||||||
cin.get( ender );
|
cin.get( ender );
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue