diff --git a/test/SafeFormat/SafeFormat.cbp b/test/SafeFormat/SafeFormat.cbp
index b530190..d87e841 100644
--- a/test/SafeFormat/SafeFormat.cbp
+++ b/test/SafeFormat/SafeFormat.cbp
@@ -6,23 +6,24 @@
-
-
+
+
-
-
-
+
+
+
-
+
+
-
-
+
+
@@ -30,31 +31,31 @@
-
-
+
+
-
+
-
-
+
+
-
-
+
+
-
+
-
-
+
+
@@ -62,14 +63,16 @@
-
-
+
+
-
+
+
+
diff --git a/test/SafeFormat/ThreadPool.cpp b/test/SafeFormat/ThreadPool.cpp
new file mode 100644
index 0000000..7843b4e
--- /dev/null
+++ b/test/SafeFormat/ThreadPool.cpp
@@ -0,0 +1,112 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// ThreadLocal test program for The Loki Library
+// Copyright (c) 2009 by Richard Sposato
+// The copyright on this file is protected under the terms of the MIT license.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+// ----------------------------------------------------------------------------
+
+#include "ThreadPool.hpp"
+
+#include
+
+// ----------------------------------------------------------------------------
+
+Thread::Thread( CallFunction func, void * parm )
+ : pthread_()
+ , func_( func )
+ , parm_( parm )
+{
+}
+
+// ----------------------------------------------------------------------------
+
+void Thread::AssignTask( CallFunction func, void * parm )
+{
+ func_ = func;
+ parm_ = parm;
+}
+
+// ----------------------------------------------------------------------------
+
+int Thread::Start( void )
+{
+ return LOKI_pthread_create( &pthread_, NULL, func_, parm_ );
+}
+
+// ----------------------------------------------------------------------------
+
+int Thread::WaitForThread( void ) const
+{
+ return LOKI_pthread_join( pthread_ );
+}
+
+// ----------------------------------------------------------------------------
+
+ThreadPool::ThreadPool( void ) : m_threads()
+{
+}
+
+// ----------------------------------------------------------------------------
+
+ThreadPool::~ThreadPool( void )
+{
+ for ( size_t ii = 0; ii < m_threads.size(); ++ii )
+ {
+ delete m_threads.at(ii);
+ }
+}
+
+// ----------------------------------------------------------------------------
+
+void ThreadPool::Create( size_t threadCount, Thread::CallFunction function )
+{
+ for( size_t ii = 0; ii < threadCount; ii++ )
+ {
+ ::std::stringstream buffer;
+ Thread * thread = new Thread( function,
+ reinterpret_cast< void * >( ii + 1 ) );
+ m_threads.push_back( thread );
+ }
+}
+
+// ----------------------------------------------------------------------------
+
+void ThreadPool::Start( void )
+{
+ for ( size_t ii = 0; ii < m_threads.size(); ii++ )
+ {
+ ::std::stringstream buffer;
+ m_threads.at( ii )->Start();
+ }
+}
+
+// ----------------------------------------------------------------------------
+
+void ThreadPool::Join( void ) const
+{
+ for ( size_t ii = 0; ii < m_threads.size(); ii++ )
+ m_threads.at( ii )->WaitForThread();
+}
+
+// ----------------------------------------------------------------------------
diff --git a/test/SafeFormat/ThreadPool.hpp b/test/SafeFormat/ThreadPool.hpp
new file mode 100644
index 0000000..f297ee6
--- /dev/null
+++ b/test/SafeFormat/ThreadPool.hpp
@@ -0,0 +1,119 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// ThreadLocal test program for The Loki Library
+// Copyright (c) 2009 by Richard Sposato
+// The copyright on this file is protected under the terms of the MIT license.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+
+// ----------------------------------------------------------------------------
+
+#include
+
+#if !defined( NULL )
+ #define NULL 0
+#endif
+
+// 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.
+#if !defined( nullptr )
+ #define nullptr NULL
+#endif
+
+#if defined(_WIN32)
+
+ #include
+ #include
+
+ typedef unsigned int ( WINAPI * ThreadFunction_ )( void * );
+
+ #define LOKI_pthread_t HANDLE
+
+ #define LOKI_pthread_create( handle, attr, func, arg ) \
+ ( int )( ( *handle = ( HANDLE ) _beginthreadex ( NULL, 0, ( ThreadFunction_ )func, arg, 0, NULL ) ) == NULL )
+
+ #define LOKI_pthread_join( thread ) \
+ ( ( WaitForSingleObject( ( thread ), INFINITE ) != WAIT_OBJECT_0 ) || !CloseHandle( thread ) )
+
+#else
+
+ #include
+
+ #define LOKI_pthread_t \
+ pthread_t
+ #define LOKI_pthread_create(handle,attr,func,arg) \
+ pthread_create(handle,attr,func,arg)
+ #define LOKI_pthread_join(thread) \
+ pthread_join(thread, NULL)
+
+#endif
+
+// ----------------------------------------------------------------------------
+
+class Thread
+{
+public:
+
+ typedef void * ( * CallFunction )( void * );
+
+ Thread( CallFunction func, void * parm );
+
+ void AssignTask( CallFunction func, void * parm );
+
+ int Start( void );
+
+ int WaitForThread( void ) const;
+
+private:
+
+ LOKI_pthread_t pthread_;
+
+ CallFunction func_;
+
+ void * parm_;
+
+};
+
+// ----------------------------------------------------------------------------
+
+class ThreadPool
+{
+public:
+
+ ThreadPool( void );
+
+ ~ThreadPool( void );
+
+ void Create( size_t threadCount, Thread::CallFunction function );
+
+ void Start( void );
+
+ void Join( void ) const;
+
+private:
+
+ typedef ::std::vector< Thread * > Threads;
+
+ Threads m_threads;
+};
+
+// ----------------------------------------------------------------------------
diff --git a/test/SafeFormat/main.cpp b/test/SafeFormat/main.cpp
index cd88693..e31efb9 100644
--- a/test/SafeFormat/main.cpp
+++ b/test/SafeFormat/main.cpp
@@ -20,6 +20,8 @@
#include
#include "../SmallObj/timer.h"
+#include "ThreadPool.hpp"
+
#if defined(_MSC_VER)
#if _MSC_VER >= 1400
@@ -146,7 +148,7 @@ void SpeedTest( unsigned int loop )
t.start();
for (int i=loop; i > 0; --i)
- Printf("Hey, %u frobnicators and %u twiddlicators\n")(i)(i);
+ ::Loki::Printf("Hey, %u frobnicators and %u twiddlicators\n")(i)(i);
t.stop();
int t_Printf = t.t();
@@ -354,7 +356,122 @@ void RandomTest( unsigned int loopCount )
}
}
- cout << endl << "Finished RandomTest" << endl;
+ cout << "Finished RandomTest" << endl;
+}
+
+// ----------------------------------------------------------------------------
+
+void * DoLokiPrintfLoop( void * p )
+{
+ const unsigned int threadIndex = reinterpret_cast< unsigned int >( p );
+
+ for ( unsigned int loop = 0; loop < 10; ++loop )
+ {
+ ::Loki::Printf( "Loop: [%u] Thread: [%u]\n" )( loop )( threadIndex );
+ }
+
+ return 0;
+}
+
+// ----------------------------------------------------------------------------
+
+void * DoLokiFPrintfLoop( void * p )
+{
+ const unsigned int threadIndex = reinterpret_cast< unsigned int >( p );
+
+ for ( unsigned int loop = 0; loop < 10; ++loop )
+ {
+ ::Loki::FPrintf( cout, "Loop: [%u] Thread: [%u]\n" )( loop )( threadIndex );
+ }
+
+ return 0;
+}
+
+// ----------------------------------------------------------------------------
+
+void * DoCoutLoop( void * p )
+{
+ const unsigned int threadIndex = reinterpret_cast< unsigned int >( p );
+
+ for ( unsigned int loop = 0; loop < 10; ++loop )
+ {
+ cout << "Loop: [" << loop << "] Thread: [" << threadIndex << "]\n";
+ }
+
+ return 0;
+}
+
+// ----------------------------------------------------------------------------
+
+void * DoStdOutLoop( void * p )
+{
+ const unsigned int threadIndex = reinterpret_cast< unsigned int >( p );
+
+ for ( unsigned int loop = 0; loop < 10; ++loop )
+ {
+ printf( "Loop: [%d] Thread: [%d]\n", loop, threadIndex );
+ }
+
+ return 0;
+}
+
+// ----------------------------------------------------------------------------
+
+void AtomicTest( void )
+{
+ char ender;
+
+ cout << "Starting Loki::Printf AtomicTest" << endl;
+ {
+ ThreadPool pool;
+ pool.Create( 20, &DoLokiPrintfLoop );
+ pool.Start();
+ pool.Join();
+ }
+ cout << "Finished Loki::Printf AtomicTest." << endl;
+ cout << "If the output lines up in neat columns, the test passed." << endl;
+ cout << "If the output is not in columns, then the test failed." << endl;
+ cout << "Press key to continue. ";
+ cin.get( ender );
+
+ cout << "Starting Loki::FPrintf AtomicTest" << endl;
+ {
+ ThreadPool pool;
+ pool.Create( 20, &DoLokiFPrintfLoop );
+ pool.Start();
+ pool.Join();
+ }
+ cout << "Finished Loki::FPrintf AtomicTest." << endl;
+ cout << "If the output lines up in neat columns, the test passed." << endl;
+ cout << "If the output is not in columns, then the test failed." << endl;
+ cout << "Press key to continue. ";
+ cin.get( ender );
+
+ cout << "Starting stdout AtomicTest" << endl;
+ {
+ ThreadPool pool;
+ pool.Create( 20, &DoStdOutLoop );
+ pool.Start();
+ pool.Join();
+ }
+ cout << "Finished stdout AtomicTest." << endl;
+ cout << "If the output lines up in neat columns, your compiler implements printf correctly." << endl;
+ cout << "If the output is not in columns, then your compiler implements printf incorrectly." << endl;
+ cout << "Press key to continue. ";
+ cin.get( ender );
+
+ cout << "Starting cout AtomicTest" << endl;
+ {
+ ThreadPool pool;
+ pool.Create( 20, &DoCoutLoop );
+ pool.Start();
+ pool.Join();
+ }
+ cout << "Finished cout AtomicTest." << endl;
+ cout << "If the output lines up in neat columns, your compiler implements cout correctly." << endl;
+ cout << "If the output is not in columns, then your compiler implements cout incorrectly." << endl;
+ cout << "Press key to continue. ";
+ cin.get( ender );
}
// ----------------------------------------------------------------------------
@@ -371,6 +488,7 @@ public:
inline bool DoSpeedTest( void ) const { return ( 0 < m_speedLoopCount ); }
inline bool DoRandomTest( void ) const { return ( 0 < m_randomLoopCount ); }
inline bool DoFormatTest( void ) const { return m_doFormatTest; }
+ inline bool DoAtomicTest( void ) const { return m_doAtomicTest; }
inline bool DoShowHelp( void ) const { return m_showHelp; }
void ShowHelp( void ) const;
@@ -380,6 +498,7 @@ private:
unsigned int m_speedLoopCount;
unsigned int m_randomLoopCount;
bool m_doFormatTest;
+ bool m_doAtomicTest;
bool m_showHelp;
const char * m_exeName;
};
@@ -390,6 +509,7 @@ CommandLineArgs::CommandLineArgs( unsigned int argc, const char * argv[] ) :
m_speedLoopCount( 0 ),
m_randomLoopCount( 0 ),
m_doFormatTest( false ),
+ m_doAtomicTest( false ),
m_showHelp( false ),
m_exeName( argv[0] )
{
@@ -416,6 +536,7 @@ CommandLineArgs::CommandLineArgs( unsigned int argc, const char * argv[] ) :
default: isValid = false; break;
case 'h': m_showHelp = true; break;
case 'f': m_doFormatTest = true; break;
+ case 'a': m_doAtomicTest = true; break;
case 'r':
{
@@ -458,6 +579,7 @@ void CommandLineArgs::ShowHelp( void ) const
cout << "Usage: " << m_exeName << " [-h] [-f] [-r:#] [-s:#]" << endl;
cout << " -h Show this help info and exit. Overrides all other options." << endl;
cout << " -f Run formatting tests." << endl;
+ cout << " -a Run atomic tests." << endl;
cout << " -r:# Run random tests for # of loops. # is a positive decimal value greater than 100." << endl;
cout << " -s:# Run speed tests for # of loops. # is a positive decimal value greater than 100." << endl;
}
@@ -487,6 +609,10 @@ int main( int argc, const char * argv[] )
{
FormatTest();
}
+ if ( args.DoAtomicTest() )
+ {
+ AtomicTest();
+ }
}
// ----------------------------------------------------------------------------