00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/Debug.h>
00018 #include <spl/threading/Mutex.h>
00019 #include <spl/Log.h>
00020 #include <spl/math/Math.h>
00021 #include <spl/threading/Thread.h>
00022
00023 #ifdef DEBUG
00024
00025 class _TestMtxThread : public Thread
00026 {
00027 private:
00028 static volatile int g_val;
00029
00030 Mutex& m_mtx;
00031 volatile bool m_running;
00032 int m_trycount;
00033 int m_failcount;
00034
00035 public:
00036 _TestMtxThread(Mutex& mtx, int trycount);
00037 virtual ~_TestMtxThread();
00038
00039 void Run();
00040 inline void Stop() { m_running = false; }
00041 inline int Failures() { return m_failcount; }
00042 };
00043
00044 volatile int _TestMtxThread::g_val = 0;
00045
00046 _TestMtxThread::_TestMtxThread(Mutex& mtx, int trycount)
00047 : m_mtx(mtx), m_trycount(trycount), m_failcount(0)
00048 {
00049 Start();
00050 }
00051
00052 _TestMtxThread::~_TestMtxThread()
00053 {
00054 }
00055
00056 void _TestMtxThread::Run()
00057 {
00058 m_running = true;
00059 while ( m_running && m_trycount-- > 0 )
00060 {
00061 m_mtx.Lock();
00062 int val = Math::RandomRange(1000000);
00063 g_val = val;
00064 Thread::Sleep (100);
00065 if ( g_val != val )
00066 {
00067 m_failcount++;
00068 }
00069 m_mtx.Unlock();
00070 }
00071 }
00072
00073 void _TestMutex()
00074 {
00075 Math::InitRandom();
00076 Mutex mtx;
00077 _TestMtxThread t1(mtx, 100);
00078 _TestMtxThread t2(mtx, 100);
00079 _TestMtxThread t3(mtx, 100);
00080
00081 t1.Join();
00082 t2.Join();
00083 t3.Join();
00084
00085 UNIT_ASSERT("Mutex lock failures",
00086 t1.Failures() == 0 &&
00087 t2.Failures() == 0 &&
00088 t3.Failures() == 0);
00089
00090 Log::SWriteOkFail( "Mutex test 1" );
00091 }
00092
00093 #endif