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

test/TestSharedLock.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/Log.h>
00019 #include <spl/math/Math.h>
00020 #include <spl/threading/Mutex.h>
00021 #include <spl/threading/RWLock.h>
00022 #include <spl/threading/Thread.h>
00023 
00024 #if defined(DEBUG) || defined(_DEBUG)
00025 
00026 class _SharedLockTestThread : public Thread
00027 {
00028 private:
00029         static volatile int m_sdata;
00030 
00031         volatile bool m_running;
00032         int m_loopcount;
00033         bool m_havelock;
00034         int m_mydata;
00035         RWLock *m_lock;
00036         bool m_reader;
00037 
00038         void Run();
00039 
00040 public:
00041         _SharedLockTestThread(RWLock *lock, bool reader, int loopcount);
00042         virtual ~_SharedLockTestThread();
00043 
00044         void Stop();
00045 };
00046 
00047 volatile int _SharedLockTestThread::m_sdata = 0;
00048 
00049 _SharedLockTestThread::_SharedLockTestThread(RWLock *lock, bool reader, int loopcount)
00050 : m_lock(lock), m_reader(reader), m_mydata(0), m_havelock(false), m_running(false), m_loopcount(loopcount)
00051 {
00052         Start();
00053 }
00054 
00055 _SharedLockTestThread::~_SharedLockTestThread()
00056 {
00057         m_running = false;
00058         Join(3000);
00059 }
00060 
00061 void _SharedLockTestThread::Stop()
00062 {
00063         m_running = false;
00064 }
00065 
00066 void _SharedLockTestThread::Run()
00067 {
00068         m_running = true;
00069         while ( (m_running && m_loopcount-- > 0) || m_havelock )
00070         {
00071                 if ( m_reader )
00072                 {
00073                         if ( m_havelock )
00074                         {
00075                                 UNIT_ASSERT("data changed", m_mydata == m_sdata);
00076                                 m_lock->UnlockRead();
00077                                 m_havelock = false;
00078                         }
00079                         else
00080                         {
00081                                 m_lock->LockRead();
00082                                 m_mydata = m_sdata;
00083                                 m_havelock = true;
00084                         }
00085                 }
00086                 else
00087                 {
00088                         if ( m_havelock )
00089                         {
00090                                 UNIT_ASSERT("data changed", m_mydata == m_sdata);
00091                                 m_lock->UnlockWrite();
00092                                 m_havelock = false;
00093                         }
00094                         else
00095                         {
00096                                 m_lock->LockWrite();
00097                                 m_sdata++;
00098                                 m_mydata = m_sdata;
00099                                 m_havelock = true;
00100                         }
00101                 }
00102                 Thread::Sleep((int)(Math::Random() * 50));
00103         }
00104 }
00105 
00106 static void _SharedLockTest1()
00107 {
00108         RWLock thelock;
00109         _SharedLockTestThread t1(&thelock, true, 10);
00110         t1.Join();
00111 
00112         DEBUG_CLEAR_MEM_CHECK_POINTS();
00113         UNIT_ASSERT_MEM_NOTED("RWLockTest1.1");
00114 
00115         Log::SWriteOkFail( "RWLockTest1" );
00116 }
00117 
00118 static void _SharedLockTest2()
00119 {
00120         RWLock thelock;
00121         _SharedLockTestThread t1(&thelock, true, 20);
00122         _SharedLockTestThread t2(&thelock, true, 20);
00123         t1.Join();
00124         t2.Join();
00125 
00126         DEBUG_CLEAR_MEM_CHECK_POINTS();
00127         UNIT_ASSERT_MEM_NOTED("RWLockTest2.1");
00128 
00129         Log::SWriteOkFail( "RWLockTest2" );
00130 }
00131 
00132 static void _SharedLockTest3()
00133 {
00134         RWLock thelock;
00135         _SharedLockTestThread t1(&thelock, false, 15);
00136         t1.Join();
00137 
00138         DEBUG_CLEAR_MEM_CHECK_POINTS();
00139         UNIT_ASSERT_MEM_NOTED("RWLockTest3.1");
00140 
00141         Log::SWriteOkFail( "RWLockTest3" );
00142 }
00143 
00144 static void _SharedLockTest4()
00145 {
00146         RWLock thelock;
00147         _SharedLockTestThread t1(&thelock, true, 40);
00148         _SharedLockTestThread t2(&thelock, false, 15);
00149         t1.Join();
00150         t2.Join();
00151 
00152         DEBUG_CLEAR_MEM_CHECK_POINTS();
00153         UNIT_ASSERT_MEM_NOTED("RWLockTest4.1");
00154 
00155         Log::SWriteOkFail( "RWLockTest4" );
00156 }
00157 
00158 static void _SharedLockTest5()
00159 {
00160         RWLock thelock;
00161         _SharedLockTestThread t1(&thelock, true, 40);
00162         _SharedLockTestThread t2(&thelock, false, 15);
00163         _SharedLockTestThread t3(&thelock, true, 40);
00164         t1.Join();
00165         t2.Join();
00166         t3.Join();
00167 
00168         DEBUG_CLEAR_MEM_CHECK_POINTS();
00169         UNIT_ASSERT_MEM_NOTED("RWTest5.1");
00170 
00171         Log::SWriteOkFail( "RWLockTest5" );
00172 }
00173 
00174 void _RWLockTest(  )
00175 {
00176         _SharedLockTest1();
00177         DEBUG_CLEAR_MEM_CHECK_POINTS();
00178         UNIT_ASSERT_MEM_NOTED("RWLockTest1");
00179 
00180         _SharedLockTest2();
00181         DEBUG_CLEAR_MEM_CHECK_POINTS();
00182         UNIT_ASSERT_MEM_NOTED("RWLockTest2");
00183 
00184         _SharedLockTest3();
00185         DEBUG_CLEAR_MEM_CHECK_POINTS();
00186         UNIT_ASSERT_MEM_NOTED("RWLockTest3");
00187 
00188         _SharedLockTest4();
00189         DEBUG_CLEAR_MEM_CHECK_POINTS();
00190         UNIT_ASSERT_MEM_NOTED("RWLockTest3");
00191 
00192         _SharedLockTest5();
00193         DEBUG_CLEAR_MEM_CHECK_POINTS();
00194         UNIT_ASSERT_MEM_NOTED("RWLockTest3");
00195 }
00196 
00197 #endif