• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

test/TestMutex.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
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