Go to the documentation of this file.00001
00012
00013
00014
00015
00016
00017 #ifndef Mutex_cpp
00018 #define Mutex_cpp
00019
00020 #include <iostream>
00021 #include <iomanip>
00022 #include <string>
00023 #include <stdio.h>
00024
00025 #include <time.h>
00026 #include <cppunit/ui/text/TestRunner.h>
00027 #include <cppunit/TextOutputter.h>
00028 #include <cppunit/extensions/TestFactoryRegistry.h>
00029 #include <cppunit/extensions/HelperMacros.h>
00030 #include <cppunit/TestAssert.h>
00031
00032 #include <coil/Mutex.h>
00033 #include <coil/Task.h>
00034 #include <coil/Time.h>
00035
00036
00041 namespace Mutex
00042 {
00043 class MutexTests
00044 : public CppUnit::TestFixture
00045 {
00046 CPPUNIT_TEST_SUITE(MutexTests);
00047
00048 CPPUNIT_TEST(test_lock);
00049 CPPUNIT_TEST(test_trylock);
00050 CPPUNIT_TEST_SUITE_END();
00051
00052 private:
00053 public:
00054 class TestTaskLock
00055 : public coil::Task
00056 {
00057 public:
00058 static long ShareCount;
00059 static coil::Mutex mtx;
00060 int svc(void)
00061 {
00062 clock_t start, end;
00063 long lc;
00064 long lbuf;
00065 for(lc=0; lc<100; lc++)
00066 {
00067 mtx.lock();
00068 lbuf = ShareCount;
00069
00070 start = clock();
00071 for(;;)
00072 {
00073 end = clock();
00074 if((double)(end-start)/CLOCKS_PER_SEC>0.000001)
00075 {
00076 break;
00077 }
00078 }
00079 lbuf++;
00080 ShareCount = lbuf;
00081 mtx.unlock();
00082 }
00083 return 0;
00084 }
00085
00086 long getShareCount()
00087 {
00088 return ShareCount;
00089 }
00090 void setShareCount(long lc)
00091 {
00092 mtx.lock();
00093 ShareCount = lc;
00094 mtx.unlock();
00095 }
00096 };
00097 class TestTaskTrylock
00098 : public coil::Task
00099 {
00100 public:
00101 static long ShareCount;
00102 static coil::Mutex mtx;
00103 int svc(void)
00104 {
00105 clock_t start, end;
00106 long lc;
00107 long lbuf;
00108 int iret;
00109 for(lc=0; lc<100; lc++)
00110 {
00111 for (;;)
00112 {
00113 iret = mtx.trylock();
00114 if(iret == false)
00115 {
00116 lbuf = ShareCount;
00117
00118 start = clock();
00119 for(;;)
00120 {
00121 end = clock();
00122 if((double)(end-start)/CLOCKS_PER_SEC>0.000001)
00123 {
00124 break;
00125 }
00126 }
00127 lbuf++;
00128 ShareCount = lbuf;
00129 break;
00130 }
00131 }
00132 mtx.unlock();
00133 }
00134 return 0;
00135 }
00136
00137 long getShareCount()
00138 {
00139 return ShareCount;
00140 }
00141 void setShareCount(long lc)
00142 {
00143 mtx.lock();
00144 ShareCount = lc;
00145 mtx.unlock();
00146 }
00147
00148 };
00152 MutexTests()
00153 {
00154 }
00155
00159 ~MutexTests()
00160 {
00161 }
00162
00166 virtual void setUp()
00167 {
00168 }
00169
00173 virtual void tearDown()
00174 {
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184 void test_lock()
00185 {
00186 long lc;
00187 char cstr[256];
00188 MutexTests::TestTaskLock tka;
00189 MutexTests::TestTaskLock tkb;
00190 tka.setShareCount(0);
00191
00192 tka.activate();
00193 tkb.activate();
00194 tka.wait();
00195 tkb.wait();
00196 lc = tka.getShareCount();
00197 sprintf( cstr, "count %ld" , lc );
00198
00199 CPPUNIT_ASSERT_MESSAGE(cstr , (lc == 200) );
00200
00201 }
00202
00203
00204
00205
00206
00207
00208 void test_trylock()
00209 {
00210 long lc;
00211 char cstr[256];
00212 MutexTests::TestTaskTrylock tka;
00213 MutexTests::TestTaskTrylock tkb;
00214 tka.setShareCount(0);
00215
00216 tka.activate();
00217 tkb.activate();
00218 tka.wait();
00219 tkb.wait();
00220 lc = tka.getShareCount();
00221 sprintf( cstr, "count %ld" , lc );
00222
00223 CPPUNIT_ASSERT_MESSAGE(cstr , (lc == 200) );
00224
00225 }
00226 };
00227 long MutexTests::TestTaskLock::ShareCount = 0L;
00228 coil::Mutex MutexTests::TestTaskLock::mtx ;
00229 long MutexTests::TestTaskTrylock::ShareCount = 0L;
00230 coil::Mutex MutexTests::TestTaskTrylock::mtx ;
00231 };
00232
00233
00234
00235
00236 CPPUNIT_TEST_SUITE_REGISTRATION(Mutex::MutexTests);
00237
00238 #ifdef LOCAL_MAIN
00239 int main(int argc, char* argv[])
00240 {
00241 CppUnit::TextUi::TestRunner runner;
00242 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00243 CppUnit::Outputter* outputter =
00244 new CppUnit::TextOutputter(&runner.result(), std::cout);
00245 runner.setOutputter(outputter);
00246 bool retcode = runner.run();
00247 return !retcode;
00248 }
00249 #endif // MAIN
00250 #endif // Mutex_cpp