fix line endings

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@896 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2008-08-08 22:20:05 +00:00
parent 0c4789ee11
commit 683474eff9
18 changed files with 5859 additions and 5859 deletions

View file

@ -1,211 +1,211 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="LevelMutex"
ProjectGUID="{7C09E027-5484-4641-8310-BDDEB1EC8676}"
RootNamespace="LevelMutex"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="0"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="../../include/loki;../../include"
StringPooling="true"
MinimalRebuild="true"
BasicRuntimeChecks="3"
SmallerTypeCheck="true"
RuntimeLibrary="3"
EnableFunctionLevelLinking="true"
WarningLevel="4"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="0"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
InlineFunctionExpansion="1"
EnableIntrinsicFunctions="true"
AdditionalIncludeDirectories="../../include/loki;../../include"
StringPooling="true"
RuntimeLibrary="2"
StructMemberAlignment="1"
EnableFunctionLevelLinking="true"
WarningLevel="4"
DebugInformationFormat="0"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
GenerateDebugInformation="true"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\main.cpp"
>
</File>
<File
RelativePath=".\MultiThreadTests.cpp"
>
</File>
<File
RelativePath=".\Thing.cpp"
>
</File>
<File
RelativePath=".\ThreadPool.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\MultiThreadTests.hpp"
>
</File>
<File
RelativePath=".\Thing.hpp"
>
</File>
<File
RelativePath=".\ThreadPool.hpp"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="LevelMutex"
ProjectGUID="{7C09E027-5484-4641-8310-BDDEB1EC8676}"
RootNamespace="LevelMutex"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="0"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="../../include/loki;../../include"
StringPooling="true"
MinimalRebuild="true"
BasicRuntimeChecks="3"
SmallerTypeCheck="true"
RuntimeLibrary="3"
EnableFunctionLevelLinking="true"
WarningLevel="4"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="0"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
InlineFunctionExpansion="1"
EnableIntrinsicFunctions="true"
AdditionalIncludeDirectories="../../include/loki;../../include"
StringPooling="true"
RuntimeLibrary="2"
StructMemberAlignment="1"
EnableFunctionLevelLinking="true"
WarningLevel="4"
DebugInformationFormat="0"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
GenerateDebugInformation="true"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\main.cpp"
>
</File>
<File
RelativePath=".\MultiThreadTests.cpp"
>
</File>
<File
RelativePath=".\Thing.cpp"
>
</File>
<File
RelativePath=".\ThreadPool.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\MultiThreadTests.hpp"
>
</File>
<File
RelativePath=".\Thing.hpp"
>
</File>
<File
RelativePath=".\ThreadPool.hpp"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

File diff suppressed because it is too large Load diff

View file

@ -1,55 +1,55 @@
////////////////////////////////////////////////////////////////////////////////
//
// Part of LevelMutex test program for The Loki Library
// Copyright (c) 2008 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 notice appear in supporting documentation.
//
// The author makes no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
//
////////////////////////////////////////////////////////////////////////////////
// $Id$
#ifndef LOKI_MULTI_THREAD_TESTS_H_INCLUDED
#define LOKI_MULTI_THREAD_TESTS_H_INCLUDED
// ----------------------------------------------------------------------------
void SetToRedoSingleTests( bool redo );
bool WillRedoSingleTests( void );
void SingleThreadSimpleTest( void );
void SingleThreadReentrantTest( void );
void SingleThreadSimpleMultiLockTest( void );
void SingleThreadComplexMultiLockTest( bool doSetup );
void SingleThreadExceptionTest( void );
void MultiThreadSimpleTest( void );
void MultiThreadTryLockTest( void );
void MultiThreadReentrantTest( void );
void MultiThreadMultiLockTest( void );
void MultiThreadRandomMultiLockTest( void );
void MultiThreadHierarchySingleLockTest( void );
void MultiThreadHierarchyMultiLockTest( void );
// ----------------------------------------------------------------------------
#endif
////////////////////////////////////////////////////////////////////////////////
//
// Part of LevelMutex test program for The Loki Library
// Copyright (c) 2008 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 notice appear in supporting documentation.
//
// The author makes no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
//
////////////////////////////////////////////////////////////////////////////////
// $Id$
#ifndef LOKI_MULTI_THREAD_TESTS_H_INCLUDED
#define LOKI_MULTI_THREAD_TESTS_H_INCLUDED
// ----------------------------------------------------------------------------
void SetToRedoSingleTests( bool redo );
bool WillRedoSingleTests( void );
void SingleThreadSimpleTest( void );
void SingleThreadReentrantTest( void );
void SingleThreadSimpleMultiLockTest( void );
void SingleThreadComplexMultiLockTest( bool doSetup );
void SingleThreadExceptionTest( void );
void MultiThreadSimpleTest( void );
void MultiThreadTryLockTest( void );
void MultiThreadReentrantTest( void );
void MultiThreadMultiLockTest( void );
void MultiThreadRandomMultiLockTest( void );
void MultiThreadHierarchySingleLockTest( void );
void MultiThreadHierarchyMultiLockTest( void );
// ----------------------------------------------------------------------------
#endif

File diff suppressed because it is too large Load diff

View file

@ -1,364 +1,364 @@
////////////////////////////////////////////////////////////////////////////////
//
// Part of LevelMutex test program for The Loki Library
// Copyright (c) 2008 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 notice appear in supporting documentation.
//
// The author makes no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
//
////////////////////////////////////////////////////////////////////////////////
// $Id$
// ----------------------------------------------------------------------------
#ifndef LOKI_TEST_LEVEL_MUTEX_THING_H
#define LOKI_TEST_LEVEL_MUTEX_THING_H
#include <LevelMutex.h>
#include <Allocator.h>
#include <vector>
// ----------------------------------------------------------------------------
void GoToSleep( unsigned int milliSeconds );
// ----------------------------------------------------------------------------
class ExceptionTossingMutex : public ::Loki::SleepLevelMutex
{
public:
enum ThrowingPolicy
{
Never,
Always,
Randomly
};
ExceptionTossingMutex( unsigned int level );
virtual ~ExceptionTossingMutex( void );
virtual ::Loki::MutexErrors::Type Lock( void ) volatile;
virtual ::Loki::MutexErrors::Type TryLock( void ) volatile;
virtual ::Loki::MutexErrors::Type Unlock( void ) volatile;
inline ThrowingPolicy GetTossPolicy( void ) const { return m_policy; }
inline void SetTossPolicy( ThrowingPolicy policy ) { m_policy = policy; }
private:
ExceptionTossingMutex( void );
ExceptionTossingMutex( const ExceptionTossingMutex & );
ExceptionTossingMutex & operator = ( const ExceptionTossingMutex & );
ThrowingPolicy m_policy;
};
typedef ::Loki::LevelMutex< ExceptionTossingMutex, 1,
::Loki::JustReturnMutexError, ::Loki::NoMutexWait > ExceptionMutex;
typedef ::Loki::LevelMutex< ::Loki::SleepLevelMutex, 1,
::Loki::JustReturnMutexError, ::Loki::MutexSleepWaits > SleepMutex;
// ----------------------------------------------------------------------------
class TestResults
{
public:
static bool Create( unsigned int threadCount );
static void Destroy( void );
inline static TestResults * GetIt( void ) { return s_instance; }
void Reset( unsigned int threadCount );
void SetResult( unsigned int threadIndex, unsigned int total,
unsigned int fails );
void OutputResults( void );
private:
struct TestResult
{
TestResult( void );
TestResult( const TestResult & that );
~TestResult( void );
unsigned int m_total;
unsigned int m_fails;
};
typedef ::std::vector< TestResult, ::Loki::LokiAllocator< TestResult > > Results;
explicit TestResults( unsigned int threadCount );
~TestResults( void );
TestResults( void );
TestResults( const TestResults & );
TestResults & operator = ( const TestResults & );
static TestResults * s_instance;
Results m_results;
};
// ----------------------------------------------------------------------------
class Thing
{
public:
typedef ::std::vector< volatile Thing * > ThingPool;
static volatile Thing & GetIt( void ) { return *s_thing; }
static void Init( unsigned int value );
static void Destroy( void );
static volatile Thing * GetFromPool( unsigned int index );
static bool MakePool( unsigned int count );
static unsigned int GetPoolSize( void );
static void DestroyPool( void );
void Print( unsigned int value, unsigned int index, unsigned int startSize ) const volatile;
void Print( unsigned int value, unsigned int index, unsigned int startSize ) const;
unsigned int GetValue( void ) const volatile { return m_value; }
unsigned int GetValue( void ) const { return m_value; }
void SetValue( unsigned int value ) volatile;
void SetValue( unsigned int value ) { m_value = value; }
inline volatile SleepMutex & GetMutex( void ) volatile { return m_mutex; }
inline const volatile SleepMutex & GetMutex( void ) const volatile { return m_mutex; }
private:
explicit Thing( unsigned int value );
~Thing( void );
Thing( void );
Thing( const Thing & );
Thing & operator = ( const Thing & );
static volatile Thing * s_thing;
static ThingPool s_pool;
static TestResults s_results;
mutable volatile SleepMutex m_mutex;
unsigned int m_value;
};
typedef ::std::vector< Thing * > UnsafeThingPool;
// ----------------------------------------------------------------------------
template < class Thingy >
class Unlocker
{
public:
explicit inline Unlocker( volatile Thingy * thing ) : m_thing( thing ) {}
inline Unlocker( const Unlocker & that ) : m_thing( that.m_thing )
{
const_cast< Unlocker & >( that ).m_thing = NULL;
}
inline ~Unlocker( void ) { Unlock(); }
inline void Unlock( void )
{
if ( NULL != m_thing )
m_thing->UnlockHierarchy();
}
private:
volatile Thingy * m_thing;
};
// ----------------------------------------------------------------------------
class LevelThing
{
public:
typedef Unlocker< LevelThing > Unlocker;
static volatile LevelThing * GetFromPool( unsigned int index );
static bool MakePool( unsigned int count );
static void DestroyPool( void );
Unlocker LockHierarchy( void ) volatile;
void UnlockHierarchy( void ) volatile;
void SetValue( unsigned int value ) volatile;
void SetValue( unsigned int value );
inline unsigned int GetValue( void ) const volatile { return m_value; }
inline unsigned int GetValue( void ) const { return m_value; }
bool DoValuesMatch( unsigned int value ) const volatile;
bool DoValuesMatch( unsigned int value ) const;
inline volatile ::Loki::LevelMutexInfo & GetMutex( void ) volatile { return m_mutex; }
inline const volatile ::Loki::LevelMutexInfo & GetMutex( void ) const volatile { return m_mutex; }
private:
typedef ::std::vector< volatile LevelThing * > LevelThingPool;
LevelThing( unsigned int level, unsigned int place );
~LevelThing( void );
LevelThing( void );
LevelThing( const LevelThing & );
LevelThing & operator = ( const LevelThing & );
static LevelThingPool s_pool;
mutable volatile SleepMutex m_mutex;
const unsigned int m_place;
unsigned int m_value;
};
// ----------------------------------------------------------------------------
class SomeThing
{
public:
SomeThing( unsigned int level, unsigned int place );
~SomeThing( void );
inline unsigned int GetLevel( void ) const volatile { return m_level; }
void SetValue( unsigned int value ) volatile;
void SetValue( unsigned int value );
inline unsigned int GetValue( void ) const volatile { return m_value; }
inline unsigned int GetValue( void ) const { return m_value; }
inline volatile ::Loki::LevelMutexInfo & GetMutex( void ) volatile { return m_mutex; }
inline const volatile ::Loki::LevelMutexInfo & GetMutex( void ) const volatile { return m_mutex; }
private:
SomeThing( void );
SomeThing( const SomeThing & );
SomeThing & operator = ( const SomeThing & );
mutable volatile SleepMutex m_mutex;
const unsigned int m_place;
const unsigned int m_level;
unsigned int m_value;
};
typedef ::std::vector< volatile SomeThing * > SomeThingPool;
typedef SomeThingPool::iterator SomeThingPoolIter;
// ----------------------------------------------------------------------------
class ManyThingsPool
{
public:
explicit ManyThingsPool( unsigned int level, unsigned int count );
~ManyThingsPool( void );
volatile SomeThing * GetFromPool( unsigned int index );
unsigned int GetCount( void ) const;
private:
ManyThingsPool( void );
ManyThingsPool( const ManyThingsPool & );
ManyThingsPool & operator = ( const ManyThingsPool & );
SomeThingPool m_pool;
};
// ----------------------------------------------------------------------------
class MultiLevelPool
{
public:
static void MakePool( unsigned int count, unsigned int thingCount );
static void DestroyPool( void );
static ManyThingsPool * GetFromPool( unsigned int index );
static unsigned int GetCount( void );
private:
typedef ::std::vector< ManyThingsPool * > MultiThingPool;
~MultiLevelPool( void );
MultiLevelPool( void );
MultiLevelPool( const MultiLevelPool & );
MultiLevelPool & operator = ( const MultiLevelPool & );
static MultiThingPool s_pool;
};
// ----------------------------------------------------------------------------
void CheckForMatchingValues( unsigned int & failCount, unsigned int & testCount,
unsigned int value, const SomeThingPool & pool );
void CheckForMatchingValues( unsigned int & failCount, unsigned int & testCount,
unsigned int value, const SomeThingPool & pool, bool locked );
void MakePool( SomeThingPool & pool );
void LockThese( SomeThingPool & pool );
void UnlockThese( SomeThingPool & pool );
unsigned int CountLockedByThisThread( const SomeThingPool & pool );
// ----------------------------------------------------------------------------
#endif
////////////////////////////////////////////////////////////////////////////////
//
// Part of LevelMutex test program for The Loki Library
// Copyright (c) 2008 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 notice appear in supporting documentation.
//
// The author makes no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
//
////////////////////////////////////////////////////////////////////////////////
// $Id$
// ----------------------------------------------------------------------------
#ifndef LOKI_TEST_LEVEL_MUTEX_THING_H
#define LOKI_TEST_LEVEL_MUTEX_THING_H
#include <LevelMutex.h>
#include <Allocator.h>
#include <vector>
// ----------------------------------------------------------------------------
void GoToSleep( unsigned int milliSeconds );
// ----------------------------------------------------------------------------
class ExceptionTossingMutex : public ::Loki::SleepLevelMutex
{
public:
enum ThrowingPolicy
{
Never,
Always,
Randomly
};
ExceptionTossingMutex( unsigned int level );
virtual ~ExceptionTossingMutex( void );
virtual ::Loki::MutexErrors::Type Lock( void ) volatile;
virtual ::Loki::MutexErrors::Type TryLock( void ) volatile;
virtual ::Loki::MutexErrors::Type Unlock( void ) volatile;
inline ThrowingPolicy GetTossPolicy( void ) const { return m_policy; }
inline void SetTossPolicy( ThrowingPolicy policy ) { m_policy = policy; }
private:
ExceptionTossingMutex( void );
ExceptionTossingMutex( const ExceptionTossingMutex & );
ExceptionTossingMutex & operator = ( const ExceptionTossingMutex & );
ThrowingPolicy m_policy;
};
typedef ::Loki::LevelMutex< ExceptionTossingMutex, 1,
::Loki::JustReturnMutexError, ::Loki::NoMutexWait > ExceptionMutex;
typedef ::Loki::LevelMutex< ::Loki::SleepLevelMutex, 1,
::Loki::JustReturnMutexError, ::Loki::MutexSleepWaits > SleepMutex;
// ----------------------------------------------------------------------------
class TestResults
{
public:
static bool Create( unsigned int threadCount );
static void Destroy( void );
inline static TestResults * GetIt( void ) { return s_instance; }
void Reset( unsigned int threadCount );
void SetResult( unsigned int threadIndex, unsigned int total,
unsigned int fails );
void OutputResults( void );
private:
struct TestResult
{
TestResult( void );
TestResult( const TestResult & that );
~TestResult( void );
unsigned int m_total;
unsigned int m_fails;
};
typedef ::std::vector< TestResult, ::Loki::LokiAllocator< TestResult > > Results;
explicit TestResults( unsigned int threadCount );
~TestResults( void );
TestResults( void );
TestResults( const TestResults & );
TestResults & operator = ( const TestResults & );
static TestResults * s_instance;
Results m_results;
};
// ----------------------------------------------------------------------------
class Thing
{
public:
typedef ::std::vector< volatile Thing * > ThingPool;
static volatile Thing & GetIt( void ) { return *s_thing; }
static void Init( unsigned int value );
static void Destroy( void );
static volatile Thing * GetFromPool( unsigned int index );
static bool MakePool( unsigned int count );
static unsigned int GetPoolSize( void );
static void DestroyPool( void );
void Print( unsigned int value, unsigned int index, unsigned int startSize ) const volatile;
void Print( unsigned int value, unsigned int index, unsigned int startSize ) const;
unsigned int GetValue( void ) const volatile { return m_value; }
unsigned int GetValue( void ) const { return m_value; }
void SetValue( unsigned int value ) volatile;
void SetValue( unsigned int value ) { m_value = value; }
inline volatile SleepMutex & GetMutex( void ) volatile { return m_mutex; }
inline const volatile SleepMutex & GetMutex( void ) const volatile { return m_mutex; }
private:
explicit Thing( unsigned int value );
~Thing( void );
Thing( void );
Thing( const Thing & );
Thing & operator = ( const Thing & );
static volatile Thing * s_thing;
static ThingPool s_pool;
static TestResults s_results;
mutable volatile SleepMutex m_mutex;
unsigned int m_value;
};
typedef ::std::vector< Thing * > UnsafeThingPool;
// ----------------------------------------------------------------------------
template < class Thingy >
class Unlocker
{
public:
explicit inline Unlocker( volatile Thingy * thing ) : m_thing( thing ) {}
inline Unlocker( const Unlocker & that ) : m_thing( that.m_thing )
{
const_cast< Unlocker & >( that ).m_thing = NULL;
}
inline ~Unlocker( void ) { Unlock(); }
inline void Unlock( void )
{
if ( NULL != m_thing )
m_thing->UnlockHierarchy();
}
private:
volatile Thingy * m_thing;
};
// ----------------------------------------------------------------------------
class LevelThing
{
public:
typedef Unlocker< LevelThing > Unlocker;
static volatile LevelThing * GetFromPool( unsigned int index );
static bool MakePool( unsigned int count );
static void DestroyPool( void );
Unlocker LockHierarchy( void ) volatile;
void UnlockHierarchy( void ) volatile;
void SetValue( unsigned int value ) volatile;
void SetValue( unsigned int value );
inline unsigned int GetValue( void ) const volatile { return m_value; }
inline unsigned int GetValue( void ) const { return m_value; }
bool DoValuesMatch( unsigned int value ) const volatile;
bool DoValuesMatch( unsigned int value ) const;
inline volatile ::Loki::LevelMutexInfo & GetMutex( void ) volatile { return m_mutex; }
inline const volatile ::Loki::LevelMutexInfo & GetMutex( void ) const volatile { return m_mutex; }
private:
typedef ::std::vector< volatile LevelThing * > LevelThingPool;
LevelThing( unsigned int level, unsigned int place );
~LevelThing( void );
LevelThing( void );
LevelThing( const LevelThing & );
LevelThing & operator = ( const LevelThing & );
static LevelThingPool s_pool;
mutable volatile SleepMutex m_mutex;
const unsigned int m_place;
unsigned int m_value;
};
// ----------------------------------------------------------------------------
class SomeThing
{
public:
SomeThing( unsigned int level, unsigned int place );
~SomeThing( void );
inline unsigned int GetLevel( void ) const volatile { return m_level; }
void SetValue( unsigned int value ) volatile;
void SetValue( unsigned int value );
inline unsigned int GetValue( void ) const volatile { return m_value; }
inline unsigned int GetValue( void ) const { return m_value; }
inline volatile ::Loki::LevelMutexInfo & GetMutex( void ) volatile { return m_mutex; }
inline const volatile ::Loki::LevelMutexInfo & GetMutex( void ) const volatile { return m_mutex; }
private:
SomeThing( void );
SomeThing( const SomeThing & );
SomeThing & operator = ( const SomeThing & );
mutable volatile SleepMutex m_mutex;
const unsigned int m_place;
const unsigned int m_level;
unsigned int m_value;
};
typedef ::std::vector< volatile SomeThing * > SomeThingPool;
typedef SomeThingPool::iterator SomeThingPoolIter;
// ----------------------------------------------------------------------------
class ManyThingsPool
{
public:
explicit ManyThingsPool( unsigned int level, unsigned int count );
~ManyThingsPool( void );
volatile SomeThing * GetFromPool( unsigned int index );
unsigned int GetCount( void ) const;
private:
ManyThingsPool( void );
ManyThingsPool( const ManyThingsPool & );
ManyThingsPool & operator = ( const ManyThingsPool & );
SomeThingPool m_pool;
};
// ----------------------------------------------------------------------------
class MultiLevelPool
{
public:
static void MakePool( unsigned int count, unsigned int thingCount );
static void DestroyPool( void );
static ManyThingsPool * GetFromPool( unsigned int index );
static unsigned int GetCount( void );
private:
typedef ::std::vector< ManyThingsPool * > MultiThingPool;
~MultiLevelPool( void );
MultiLevelPool( void );
MultiLevelPool( const MultiLevelPool & );
MultiLevelPool & operator = ( const MultiLevelPool & );
static MultiThingPool s_pool;
};
// ----------------------------------------------------------------------------
void CheckForMatchingValues( unsigned int & failCount, unsigned int & testCount,
unsigned int value, const SomeThingPool & pool );
void CheckForMatchingValues( unsigned int & failCount, unsigned int & testCount,
unsigned int value, const SomeThingPool & pool, bool locked );
void MakePool( SomeThingPool & pool );
void LockThese( SomeThingPool & pool );
void UnlockThese( SomeThingPool & pool );
unsigned int CountLockedByThisThread( const SomeThingPool & pool );
// ----------------------------------------------------------------------------
#endif

View file

@ -1,390 +1,390 @@
////////////////////////////////////////////////////////////////////////////////
//
// Part of LevelMutex test program for The Loki Library
// Copyright (c) 2008 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 notice appear in supporting documentation.
//
// The author makes no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
//
////////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
#include "ThreadPool.hpp"
#include <assert.h>
#include <process.h>
using namespace ::std;
// define nullptr even though new compilers will have this keyword just so we
// have a consistent and easy way of identifying which uses of 0 mean null.
#define nullptr 0
#if defined( DEBUG ) || defined( _DEBUG )
#define LOKI_DEBUG_CODE( code ) code;
#else
#define LOKI_DEBUG_CODE( code ) ;
#endif
volatile Thread * Thread::s_thread = nullptr;
// ----------------------------------------------------------------------------
Thread::Thread( volatile ThreadPool * owner ) :
m_thread(),
m_func( nullptr ),
m_parm( nullptr ),
m_status( Thread::Idle ),
m_stop( false ),
m_owner( owner )
{
assert( IsValid( owner ) );
}
// ----------------------------------------------------------------------------
Thread::Thread( volatile ThreadPool * owner, CallFunction function,
void * parm ) :
m_thread(),
m_func( function ),
m_parm( parm ),
m_status( Thread::Starting ),
m_stop( false ),
m_owner( owner )
{
assert( IsValid( owner ) );
assert( nullptr != m_func );
}
// ----------------------------------------------------------------------------
Thread::~Thread( void )
{
assert( IsValid( m_owner ) );
assert( Thread::Dead == m_status );
assert( nullptr == m_func );
assert( nullptr == m_parm );
assert( m_stop );
}
// ----------------------------------------------------------------------------
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.
#if defined( _MSC_VER )
::SleepEx( 1, true );
#else
::sleep( 1 );
#endif
}
return true;
}
// ----------------------------------------------------------------------------
bool Thread::IsValid( const volatile ThreadPool * owner ) const volatile
{
assert( nullptr != this );
assert( nullptr != m_owner );
assert( nullptr != owner );
assert( m_owner == owner );
switch ( m_status )
{
case Thread::Dead:
case Thread::Idle:
assert( nullptr == m_func );
assert( nullptr == m_parm );
break;
case Thread::Active:
case Thread::Starting:
assert( nullptr != m_func );
break;
default:
assert( false );
break;
}
return true;
}
// ----------------------------------------------------------------------------
ThreadPool::ThreadPool( unsigned int threadCount ) :
m_threads()
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
if ( 0 < threadCount )
Create( threadCount );
}
// ----------------------------------------------------------------------------
ThreadPool::~ThreadPool( void )
{
assert( IsValid() );
ThreadsIter end( m_threads.end() );
for ( ThreadsIter it( m_threads.begin() ); it != end; ++it )
{
try
{
volatile Thread * thread = *it;
assert( nullptr != thread );
thread->m_stop = true;
thread->WaitForThread();
if ( thread->GetStatus() == Thread::Idle )
thread->m_status = Thread::Dead;
LokiThreadJoin( thread->m_thread );
*it = nullptr;
delete thread;
}
catch ( ... )
{
}
}
}
// ----------------------------------------------------------------------------
unsigned int ThreadPool::Create( unsigned int threadCount ) volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
const unsigned int countNow = GetCount( Thread::Idle );
if ( threadCount <= countNow )
return threadCount;
const unsigned int totalCount = pThis->m_threads.size();
const unsigned int howManyToAdd = threadCount - countNow;
if ( pThis->m_threads.capacity() <= howManyToAdd )
pThis->m_threads.reserve( totalCount + howManyToAdd );
for ( unsigned int ii = 0; ii < howManyToAdd; ++ii )
{
#if defined( _MSC_VER )
volatile Thread * thread = new Thread( this );
#else
Thread * thread = new Thread( this );
#endif
pThis->m_threads.push_back( thread );
Thread * pThread = const_cast< Thread * >( thread );
void * p = reinterpret_cast< void * >( pThread );
// Call thread creation policy?
LokiThreadCreate( &thread->m_thread, nullptr, TopFunction, p );
}
return howManyToAdd;
}
// ----------------------------------------------------------------------------
unsigned int ThreadPool::GetCount( void ) const volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
const unsigned int count = pThis->m_threads.size();
return count;
}
// ----------------------------------------------------------------------------
unsigned int ThreadPool::GetCount( Thread::Status status ) const volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
const unsigned int totalCount = pThis->m_threads.size();
unsigned int count = 0;
for ( size_t ii = 0; ii < totalCount; ii++ )
{
const volatile Thread * thread = pThis->m_threads.at( ii );
assert( nullptr != thread );
if ( thread->GetStatus() == status )
++count;
}
return count;
}
// ----------------------------------------------------------------------------
volatile const Thread * ThreadPool::GetThread( unsigned int index ) const volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
if ( pThis->m_threads.size() <= index )
return nullptr;
volatile const Thread * thread = pThis->m_threads[ index ];
return thread;
}
// ----------------------------------------------------------------------------
void ThreadPool::Join( unsigned int index ) const volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
if ( pThis->m_threads.size() <= index )
return;
volatile Thread * thread = pThis->m_threads[ index ];
thread->WaitForThread();
}
// ----------------------------------------------------------------------------
void ThreadPool::JoinAll( void ) volatile const
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
for ( size_t ii = 0; ii < pThis->m_threads.size(); ii++ )
{
volatile Thread * thread = pThis->m_threads.at( ii );
assert( nullptr != thread );
thread->WaitForThread();
}
}
// ----------------------------------------------------------------------------
volatile Thread * ThreadPool::Start( CallFunction function, void * parm ) volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
if ( nullptr == function )
return nullptr;
#if defined( _MSC_VER )
volatile Thread * thread = nullptr;
#else
Thread * thread = nullptr;
#endif
bool foundOne = false;
for ( size_t ii = 0; ii < pThis->m_threads.size(); ii++ )
{
#if defined( _MSC_VER )
thread = pThis->m_threads.at( ii );
#else
thread = const_cast< Thread * >( pThis->m_threads.at( ii ) );
#endif
assert( nullptr != thread );
if ( Thread::Idle == thread->m_status )
{
foundOne = true;
break;
}
}
if ( foundOne )
{
thread->m_func = function;
thread->m_parm = parm;
thread->m_status = Thread::Starting;
}
else
{
// Did not find an idle thread, so start a new one.
thread = new Thread( this, function, parm );
pThis->m_threads.push_back( thread );
Thread * pThread = const_cast< Thread * >( thread );
void * p = reinterpret_cast< void * >( pThread );
// Call to thread creation policy?
LokiThreadCreate( &thread->m_thread, nullptr, TopFunction, p );
}
return thread;
}
// ----------------------------------------------------------------------------
#if defined( _MSC_VER )
unsigned int ThreadPool::TopFunction( void * p )
#else
void * ThreadPool::TopFunction( void * p )
#endif
{
assert( nullptr != p );
volatile Thread * thread = reinterpret_cast< volatile Thread * >( p );
Thread::SetCurrentThread( thread );
while ( ( thread->m_status != Thread::Dead ) && ( !thread->m_stop ) )
{
// Call the thread's WaitPolicy here?
#if defined( _MSC_VER )
::SleepEx( 1, true );
#else
::sleep( 1 );
#endif
if ( thread->m_status == Thread::Starting )
{
try
{
assert( nullptr != thread->m_func );
thread->m_status = Thread::Active;
thread->m_func( thread->m_parm );
}
catch ( ... )
{
// What to do in case of exception?
// Call an exception policy?
}
thread->m_status = Thread::Idle;
thread->m_func = nullptr;
thread->m_parm = nullptr;
}
}
#if defined( _MSC_VER )
return 0;
#else
return nullptr;
#endif
}
// ----------------------------------------------------------------------------
bool ThreadPool::IsValid( void ) const volatile
{
assert( nullptr != this );
ThreadPool * pThis = const_cast< ThreadPool * >( this );
ThreadsCIter end( pThis->m_threads.end() );
for ( ThreadsCIter it( pThis->m_threads.begin() ); it != end; ++it )
{
const volatile Thread * thread = *it;
assert( nullptr != thread );
assert( thread->IsValid( this ) );
}
return true;
}
// ----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
//
// Part of LevelMutex test program for The Loki Library
// Copyright (c) 2008 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 notice appear in supporting documentation.
//
// The author makes no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
//
////////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
#include "ThreadPool.hpp"
#include <assert.h>
#include <process.h>
using namespace ::std;
// define nullptr even though new compilers will have this keyword just so we
// have a consistent and easy way of identifying which uses of 0 mean null.
#define nullptr 0
#if defined( DEBUG ) || defined( _DEBUG )
#define LOKI_DEBUG_CODE( code ) code;
#else
#define LOKI_DEBUG_CODE( code ) ;
#endif
volatile Thread * Thread::s_thread = nullptr;
// ----------------------------------------------------------------------------
Thread::Thread( volatile ThreadPool * owner ) :
m_thread(),
m_func( nullptr ),
m_parm( nullptr ),
m_status( Thread::Idle ),
m_stop( false ),
m_owner( owner )
{
assert( IsValid( owner ) );
}
// ----------------------------------------------------------------------------
Thread::Thread( volatile ThreadPool * owner, CallFunction function,
void * parm ) :
m_thread(),
m_func( function ),
m_parm( parm ),
m_status( Thread::Starting ),
m_stop( false ),
m_owner( owner )
{
assert( IsValid( owner ) );
assert( nullptr != m_func );
}
// ----------------------------------------------------------------------------
Thread::~Thread( void )
{
assert( IsValid( m_owner ) );
assert( Thread::Dead == m_status );
assert( nullptr == m_func );
assert( nullptr == m_parm );
assert( m_stop );
}
// ----------------------------------------------------------------------------
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.
#if defined( _MSC_VER )
::SleepEx( 1, true );
#else
::sleep( 1 );
#endif
}
return true;
}
// ----------------------------------------------------------------------------
bool Thread::IsValid( const volatile ThreadPool * owner ) const volatile
{
assert( nullptr != this );
assert( nullptr != m_owner );
assert( nullptr != owner );
assert( m_owner == owner );
switch ( m_status )
{
case Thread::Dead:
case Thread::Idle:
assert( nullptr == m_func );
assert( nullptr == m_parm );
break;
case Thread::Active:
case Thread::Starting:
assert( nullptr != m_func );
break;
default:
assert( false );
break;
}
return true;
}
// ----------------------------------------------------------------------------
ThreadPool::ThreadPool( unsigned int threadCount ) :
m_threads()
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
if ( 0 < threadCount )
Create( threadCount );
}
// ----------------------------------------------------------------------------
ThreadPool::~ThreadPool( void )
{
assert( IsValid() );
ThreadsIter end( m_threads.end() );
for ( ThreadsIter it( m_threads.begin() ); it != end; ++it )
{
try
{
volatile Thread * thread = *it;
assert( nullptr != thread );
thread->m_stop = true;
thread->WaitForThread();
if ( thread->GetStatus() == Thread::Idle )
thread->m_status = Thread::Dead;
LokiThreadJoin( thread->m_thread );
*it = nullptr;
delete thread;
}
catch ( ... )
{
}
}
}
// ----------------------------------------------------------------------------
unsigned int ThreadPool::Create( unsigned int threadCount ) volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
const unsigned int countNow = GetCount( Thread::Idle );
if ( threadCount <= countNow )
return threadCount;
const unsigned int totalCount = pThis->m_threads.size();
const unsigned int howManyToAdd = threadCount - countNow;
if ( pThis->m_threads.capacity() <= howManyToAdd )
pThis->m_threads.reserve( totalCount + howManyToAdd );
for ( unsigned int ii = 0; ii < howManyToAdd; ++ii )
{
#if defined( _MSC_VER )
volatile Thread * thread = new Thread( this );
#else
Thread * thread = new Thread( this );
#endif
pThis->m_threads.push_back( thread );
Thread * pThread = const_cast< Thread * >( thread );
void * p = reinterpret_cast< void * >( pThread );
// Call thread creation policy?
LokiThreadCreate( &thread->m_thread, nullptr, TopFunction, p );
}
return howManyToAdd;
}
// ----------------------------------------------------------------------------
unsigned int ThreadPool::GetCount( void ) const volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
const unsigned int count = pThis->m_threads.size();
return count;
}
// ----------------------------------------------------------------------------
unsigned int ThreadPool::GetCount( Thread::Status status ) const volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
const unsigned int totalCount = pThis->m_threads.size();
unsigned int count = 0;
for ( size_t ii = 0; ii < totalCount; ii++ )
{
const volatile Thread * thread = pThis->m_threads.at( ii );
assert( nullptr != thread );
if ( thread->GetStatus() == status )
++count;
}
return count;
}
// ----------------------------------------------------------------------------
volatile const Thread * ThreadPool::GetThread( unsigned int index ) const volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
if ( pThis->m_threads.size() <= index )
return nullptr;
volatile const Thread * thread = pThis->m_threads[ index ];
return thread;
}
// ----------------------------------------------------------------------------
void ThreadPool::Join( unsigned int index ) const volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
if ( pThis->m_threads.size() <= index )
return;
volatile Thread * thread = pThis->m_threads[ index ];
thread->WaitForThread();
}
// ----------------------------------------------------------------------------
void ThreadPool::JoinAll( void ) volatile const
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
for ( size_t ii = 0; ii < pThis->m_threads.size(); ii++ )
{
volatile Thread * thread = pThis->m_threads.at( ii );
assert( nullptr != thread );
thread->WaitForThread();
}
}
// ----------------------------------------------------------------------------
volatile Thread * ThreadPool::Start( CallFunction function, void * parm ) volatile
{
assert( IsValid() );
LOKI_DEBUG_CODE( Checker checker( this ); (void)checker; )
ThreadPool * pThis = const_cast< ThreadPool * >( this );
if ( nullptr == function )
return nullptr;
#if defined( _MSC_VER )
volatile Thread * thread = nullptr;
#else
Thread * thread = nullptr;
#endif
bool foundOne = false;
for ( size_t ii = 0; ii < pThis->m_threads.size(); ii++ )
{
#if defined( _MSC_VER )
thread = pThis->m_threads.at( ii );
#else
thread = const_cast< Thread * >( pThis->m_threads.at( ii ) );
#endif
assert( nullptr != thread );
if ( Thread::Idle == thread->m_status )
{
foundOne = true;
break;
}
}
if ( foundOne )
{
thread->m_func = function;
thread->m_parm = parm;
thread->m_status = Thread::Starting;
}
else
{
// Did not find an idle thread, so start a new one.
thread = new Thread( this, function, parm );
pThis->m_threads.push_back( thread );
Thread * pThread = const_cast< Thread * >( thread );
void * p = reinterpret_cast< void * >( pThread );
// Call to thread creation policy?
LokiThreadCreate( &thread->m_thread, nullptr, TopFunction, p );
}
return thread;
}
// ----------------------------------------------------------------------------
#if defined( _MSC_VER )
unsigned int ThreadPool::TopFunction( void * p )
#else
void * ThreadPool::TopFunction( void * p )
#endif
{
assert( nullptr != p );
volatile Thread * thread = reinterpret_cast< volatile Thread * >( p );
Thread::SetCurrentThread( thread );
while ( ( thread->m_status != Thread::Dead ) && ( !thread->m_stop ) )
{
// Call the thread's WaitPolicy here?
#if defined( _MSC_VER )
::SleepEx( 1, true );
#else
::sleep( 1 );
#endif
if ( thread->m_status == Thread::Starting )
{
try
{
assert( nullptr != thread->m_func );
thread->m_status = Thread::Active;
thread->m_func( thread->m_parm );
}
catch ( ... )
{
// What to do in case of exception?
// Call an exception policy?
}
thread->m_status = Thread::Idle;
thread->m_func = nullptr;
thread->m_parm = nullptr;
}
}
#if defined( _MSC_VER )
return 0;
#else
return nullptr;
#endif
}
// ----------------------------------------------------------------------------
bool ThreadPool::IsValid( void ) const volatile
{
assert( nullptr != this );
ThreadPool * pThis = const_cast< ThreadPool * >( this );
ThreadsCIter end( pThis->m_threads.end() );
for ( ThreadsCIter it( pThis->m_threads.begin() ); it != end; ++it )
{
const volatile Thread * thread = *it;
assert( nullptr != thread );
assert( thread->IsValid( this ) );
}
return true;
}
// ----------------------------------------------------------------------------

View file

@ -1,226 +1,226 @@
////////////////////////////////////////////////////////////////////////////////
//
// Part of LevelMutex test program for The Loki Library
// Copyright (c) 2008 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 notice appear in supporting documentation.
//
// The author makes no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
//
////////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
#include <vector>
// ----------------------------------------------------------------------------
typedef void * ( * CallFunction )( void * );
#if defined( _MSC_VER )
#include <Windows.h>
#define LokiThread HANDLE
#define LokiThreadCreate( handle, attr, func, arg ) \
(int)(( * handle = ( HANDLE ) _beginthreadex ( nullptr, 0, func, arg, 0, nullptr ) ) == nullptr )
#define LokiThreadJoin( thread ) \
((::WaitForSingleObject((thread),INFINITE)!=WAIT_OBJECT_0) || !CloseHandle(thread))
#else
#include <pthread.h>
typedef void * ( * CallFunction )( void * );
#define LokiThread pthread_t
#define LokiThreadCreate( handle, attr, func, arg ) \
pthread_create( handle, attr, func, arg )
#define LokiThreadJoin( thread ) pthread_join( thread, NULL )
#endif
/** @par thread_local Keyword
The mutexes require compilers to provide thread local storage - meaning each
thread gets its own copy of the data. The next version of C++ will have a
new keyword, thread_local for that purpose. Some existing compilers already
provide thread local storage using different syntax, so these lines use
thread_local to mimic that syntax. If your compiler provides thread local
storage but using different syntax besides "thread_local", you may want to
modify these lines. If your compiler does not support thread local storage,
you can't use LevelMutex.
*/
#ifndef LOKI_THREAD_LOCAL
#if defined( _MSC_VER )
#if ( _MSC_VER >= 1300 )
#define LOKI_THREAD_LOCAL __declspec( thread )
#else
#error "Only Visual Studio versions 7.0 and after supported."
#endif
#elif ( __GNUC__ )
#define LOKI_THREAD_LOCAL __thread
#else
#warning "Check if your compiler provides thread local storage."
#define LOKI_THREAD_LOCAL thread_local
#endif
#endif
class ThreadPool;
class Thread
{
public:
enum Status
{
Dead = 0,
Idle,
Starting,
Active
};
inline static volatile Thread * GetCurrentThread( void )
{
return s_thread;
}
bool WaitForThread( void ) volatile;
inline CallFunction GetFunction( void ) const volatile { return m_func; }
inline void * GetParameter( void ) const volatile { return m_parm; }
inline Status GetStatus( void ) const volatile { return m_status; }
inline const volatile ThreadPool * GetThreadPool( void ) const volatile
{
return m_owner;
}
inline volatile ThreadPool * GetThreadPool( void ) volatile
{
return m_owner;
}
private:
friend class ThreadPool;
inline static void SetCurrentThread( volatile Thread * thread )
{
s_thread = thread;
}
explicit Thread( volatile ThreadPool * owner );
Thread( volatile ThreadPool * owner, CallFunction function, void * parm );
~Thread( void );
/// Default constructor not implemented.
Thread( void );
/// not implemented.
Thread( const Thread & );
/// not implemented.
Thread & operator = ( const Thread & );
bool IsValid( const volatile ThreadPool * owner ) const volatile;
static LOKI_THREAD_LOCAL volatile Thread * s_thread;
LokiThread m_thread;
CallFunction m_func;
void * m_parm;
Status m_status;
bool m_stop;
volatile ThreadPool * const m_owner;
};
class ThreadPool
{
public:
explicit ThreadPool( unsigned int threadCount = 0 );
~ThreadPool( void );
unsigned int Create( unsigned int threadCount ) volatile;
volatile Thread * Start( CallFunction function, void * parm ) volatile;
void Join( unsigned int index ) const volatile;
void JoinAll( void ) const volatile;
unsigned int GetCount( void ) const volatile;
unsigned int GetCount( Thread::Status status ) const volatile;
volatile const Thread * GetThread( unsigned int index ) const volatile;
inline volatile Thread * GetThread( unsigned int index ) volatile
{
return const_cast< volatile Thread * >(
const_cast< const volatile ThreadPool * >( this )->
GetThread( index ) );
}
private:
/** @class Checker
Performs validity check on ThreadPool to insure no class invariants were
violated inside any member function. This class only gets used in debug
builds, and any instance of it gets optimized away in a release build. A
checker is created inside many of member functions so that it's destructor
gets called when the function exits.
*/
class Checker
{
public:
inline explicit Checker( const volatile ThreadPool * mutex ) :
m_pool( mutex ) {}
inline ~Checker( void ) { m_pool->IsValid(); }
private:
Checker( void );
Checker( const Checker & );
Checker & operator = ( const Checker & );
const volatile ThreadPool * m_pool;
};
/// Copy-constructor is not implemented.
ThreadPool( const ThreadPool & );
/// Copy-assignment operator is not implemented.
ThreadPool & operator = ( const ThreadPool & );
#if defined( _MSC_VER )
static unsigned int __stdcall TopFunction( void * p );
#else
static void * TopFunction( void * p );
#endif
/** Returns true if no class invariant broken, otherwise asserts. This function
only gets called in debug builds.
*/
bool IsValid( void ) const volatile;
typedef ::std::vector< volatile Thread * > Threads;
typedef Threads::iterator ThreadsIter;
typedef Threads::const_iterator ThreadsCIter;
Threads m_threads;
};
// ----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
//
// Part of LevelMutex test program for The Loki Library
// Copyright (c) 2008 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 notice appear in supporting documentation.
//
// The author makes no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
//
////////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
#include <vector>
// ----------------------------------------------------------------------------
typedef void * ( * CallFunction )( void * );
#if defined( _MSC_VER )
#include <Windows.h>
#define LokiThread HANDLE
#define LokiThreadCreate( handle, attr, func, arg ) \
(int)(( * handle = ( HANDLE ) _beginthreadex ( nullptr, 0, func, arg, 0, nullptr ) ) == nullptr )
#define LokiThreadJoin( thread ) \
((::WaitForSingleObject((thread),INFINITE)!=WAIT_OBJECT_0) || !CloseHandle(thread))
#else
#include <pthread.h>
typedef void * ( * CallFunction )( void * );
#define LokiThread pthread_t
#define LokiThreadCreate( handle, attr, func, arg ) \
pthread_create( handle, attr, func, arg )
#define LokiThreadJoin( thread ) pthread_join( thread, NULL )
#endif
/** @par thread_local Keyword
The mutexes require compilers to provide thread local storage - meaning each
thread gets its own copy of the data. The next version of C++ will have a
new keyword, thread_local for that purpose. Some existing compilers already
provide thread local storage using different syntax, so these lines use
thread_local to mimic that syntax. If your compiler provides thread local
storage but using different syntax besides "thread_local", you may want to
modify these lines. If your compiler does not support thread local storage,
you can't use LevelMutex.
*/
#ifndef LOKI_THREAD_LOCAL
#if defined( _MSC_VER )
#if ( _MSC_VER >= 1300 )
#define LOKI_THREAD_LOCAL __declspec( thread )
#else
#error "Only Visual Studio versions 7.0 and after supported."
#endif
#elif ( __GNUC__ )
#define LOKI_THREAD_LOCAL __thread
#else
#warning "Check if your compiler provides thread local storage."
#define LOKI_THREAD_LOCAL thread_local
#endif
#endif
class ThreadPool;
class Thread
{
public:
enum Status
{
Dead = 0,
Idle,
Starting,
Active
};
inline static volatile Thread * GetCurrentThread( void )
{
return s_thread;
}
bool WaitForThread( void ) volatile;
inline CallFunction GetFunction( void ) const volatile { return m_func; }
inline void * GetParameter( void ) const volatile { return m_parm; }
inline Status GetStatus( void ) const volatile { return m_status; }
inline const volatile ThreadPool * GetThreadPool( void ) const volatile
{
return m_owner;
}
inline volatile ThreadPool * GetThreadPool( void ) volatile
{
return m_owner;
}
private:
friend class ThreadPool;
inline static void SetCurrentThread( volatile Thread * thread )
{
s_thread = thread;
}
explicit Thread( volatile ThreadPool * owner );
Thread( volatile ThreadPool * owner, CallFunction function, void * parm );
~Thread( void );
/// Default constructor not implemented.
Thread( void );
/// not implemented.
Thread( const Thread & );
/// not implemented.
Thread & operator = ( const Thread & );
bool IsValid( const volatile ThreadPool * owner ) const volatile;
static LOKI_THREAD_LOCAL volatile Thread * s_thread;
LokiThread m_thread;
CallFunction m_func;
void * m_parm;
Status m_status;
bool m_stop;
volatile ThreadPool * const m_owner;
};
class ThreadPool
{
public:
explicit ThreadPool( unsigned int threadCount = 0 );
~ThreadPool( void );
unsigned int Create( unsigned int threadCount ) volatile;
volatile Thread * Start( CallFunction function, void * parm ) volatile;
void Join( unsigned int index ) const volatile;
void JoinAll( void ) const volatile;
unsigned int GetCount( void ) const volatile;
unsigned int GetCount( Thread::Status status ) const volatile;
volatile const Thread * GetThread( unsigned int index ) const volatile;
inline volatile Thread * GetThread( unsigned int index ) volatile
{
return const_cast< volatile Thread * >(
const_cast< const volatile ThreadPool * >( this )->
GetThread( index ) );
}
private:
/** @class Checker
Performs validity check on ThreadPool to insure no class invariants were
violated inside any member function. This class only gets used in debug
builds, and any instance of it gets optimized away in a release build. A
checker is created inside many of member functions so that it's destructor
gets called when the function exits.
*/
class Checker
{
public:
inline explicit Checker( const volatile ThreadPool * mutex ) :
m_pool( mutex ) {}
inline ~Checker( void ) { m_pool->IsValid(); }
private:
Checker( void );
Checker( const Checker & );
Checker & operator = ( const Checker & );
const volatile ThreadPool * m_pool;
};
/// Copy-constructor is not implemented.
ThreadPool( const ThreadPool & );
/// Copy-assignment operator is not implemented.
ThreadPool & operator = ( const ThreadPool & );
#if defined( _MSC_VER )
static unsigned int __stdcall TopFunction( void * p );
#else
static void * TopFunction( void * p );
#endif
/** Returns true if no class invariant broken, otherwise asserts. This function
only gets called in debug builds.
*/
bool IsValid( void ) const volatile;
typedef ::std::vector< volatile Thread * > Threads;
typedef Threads::iterator ThreadsIter;
typedef Threads::const_iterator ThreadsCIter;
Threads m_threads;
};
// ----------------------------------------------------------------------------

File diff suppressed because it is too large Load diff