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

test/TestSemaphore.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/threading/Event.h>
00020 #include <spl/Int32.h>
00021 #include <spl/threading/InterlockCounter.h>
00022 #include <spl/threading/Mutex.h>
00023 #include <spl/threading/Semaphore.h>
00024 #include <spl/threading/ThreadStartDelegate.h>
00025 
00026 #ifdef DEBUG
00027 
00028 class SemaphoreTester
00029 {
00030 private:
00031         volatile bool m_running;
00032         Event m_waitForIt;
00033         InterlockCounter m_count;
00034         Semaphore m_semaphore;
00035 
00036         ThreadStartDelegate<SemaphoreTester> m_thread1;
00037         ThreadStartDelegate<SemaphoreTester> m_thread2;
00038         ThreadStartDelegate<SemaphoreTester> m_thread3;
00039         ThreadStartDelegate<SemaphoreTester> m_thread4;
00040 
00041 public:
00042         inline SemaphoreTester()
00043         :       m_thread1(),
00044                 m_thread2(),
00045                 m_thread3(),
00046                 m_thread4(),
00047                 m_count(),
00048                 m_waitForIt(),
00049                 m_semaphore("TST", 3),
00050                 m_running(true)
00051         {
00052                 m_thread1.Set(this, &SemaphoreTester::RunMe);
00053                 m_thread2.Set(this, &SemaphoreTester::RunMe);
00054                 m_thread3.Set(this, &SemaphoreTester::RunMe);
00055                 m_thread4.Set(this, &SemaphoreTester::RunMe);
00056 
00057                 m_thread1.Start();
00058                 m_thread2.Start();
00059                 m_thread3.Start();
00060                 m_thread4.Start();
00061         }
00062 
00063         inline ~SemaphoreTester()
00064         {
00065         }
00066 
00067         inline int32 Count()
00068         {
00069                 return m_count.Get();
00070         }
00071 
00072         inline void Stop()
00073         {
00074                 m_running = false;
00075                 m_waitForIt.Notify();
00076 
00077                 m_thread1.Join(3000);
00078                 m_thread2.Join(3000);
00079                 m_thread3.Join(3000);
00080                 m_thread4.Join(5000);
00081         }
00082 
00083 private:
00084         void RunMe()
00085         {
00086                 m_semaphore.Lock();
00087 
00088                 m_count += 1;
00089 
00090                 if (m_running)
00091                 {
00092                         m_waitForIt.Wait();
00093                 }
00094 
00095                 m_semaphore.Unlock();
00096         }
00097 };
00098 
00099 static void _TestSemaphore1()
00100 {
00101         DateTime now = DateTime::Now();
00102 
00103         SemaphoreTester tst;
00104 
00105         while (tst.Count() < 3 && now.AddSeconds(40) < DateTime::Now())
00106         {
00107                 Thread::Sleep(1000);
00108         }
00109 
00110         StringPtr msg = String("Should be three threads running ").Cat(Int32::ToString(tst.Count())); 
00111         UNIT_ASSERT(msg->GetChars(), tst.Count() == 3);
00112 
00113         tst.Stop();
00114 
00115         UNIT_ASSERT("Should be four threads ran", tst.Count() == 4);
00116 
00117         Log::SWriteOkFail( "Semaphore test" );
00118 }
00119 
00120 void _TestSemaphore()
00121 {
00122         _TestSemaphore1();
00123         DEBUG_CLEAR_MEM_CHECK_POINTS();
00124         DEBUG_DUMP_MEM_LEAKS();
00125 }
00126 
00127 #endif
00128