/[svn]/linuxsampler/trunk/src/testcases/MutexTest.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/testcases/MutexTest.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 62 by schoenebeck, Sun May 2 17:45:43 2004 UTC revision 63 by schoenebeck, Tue May 4 18:52:24 2004 UTC
# Line 18  int MutexTest::ConcurrentThread::Main() Line 18  int MutexTest::ConcurrentThread::Main()
18  }  }
19    
20    
21    // DummyThread
22    
23    MutexTest::DummyThread::DummyThread() : Thread(false, 0, -4) {
24        resource = 0;
25    }
26    
27    int MutexTest::DummyThread::Main() {
28        mutex.Lock();
29        mutex.Lock();
30        resource++;
31        mutex.Unlock();
32    }
33    
34    
35  // MutexTest  // MutexTest
36    
37  void MutexTest::setUp() {  void MutexTest::setUp() {
# Line 26  void MutexTest::setUp() { Line 40  void MutexTest::setUp() {
40  void MutexTest::tearDown() {  void MutexTest::tearDown() {
41  }  }
42    
43    // Check with only one thread (thus no concurrency) if locking and unlocking the Mutex works without getting the thread to be locked falsely.
44  void MutexTest::testLockAndUnlockBySingleThread() {  void MutexTest::testLockAndUnlockBySingleThread() {
45      ConcurrentThread t;      ConcurrentThread t;
46      t.StartThread();      t.StartThread();
# Line 33  void MutexTest::testLockAndUnlockBySingl Line 48  void MutexTest::testLockAndUnlockBySingl
48      CPPUNIT_ASSERT(t.resource == 1);      CPPUNIT_ASSERT(t.resource == 1);
49  }  }
50    
51    // Now check with two concurrent threads if one thread can block the other one by using the Mutex.
52  void MutexTest::testLock() {  void MutexTest::testLock() {
53      ConcurrentThread t;      ConcurrentThread t;
54      t.mutex.Lock();      t.mutex.Lock();
# Line 42  void MutexTest::testLock() { Line 58  void MutexTest::testLock() {
58      t.mutex.Unlock();      t.mutex.Unlock();
59  }  }
60    
61    // Now check if the blocked thread get unblocked when thread that owns the Mutex unlocks the Mutex again.
62  void MutexTest::testUnlock() {  void MutexTest::testUnlock() {
63      CPPUNIT_ASSERT(true);      ConcurrentThread t;
64        t.mutex.Lock();
65        t.SignalStartThread();
66        usleep(400000); // wait 400ms
67        t.mutex.Unlock();
68        usleep(400000); // wait 400ms
69        CPPUNIT_ASSERT(t.resource == 1);
70  }  }
71    
72    // Check if the same thread can lock the Mutex twice in a row without unlocking it and without getting blocked itself.
73  void MutexTest::testDoubleLock() {  void MutexTest::testDoubleLock() {
74      CPPUNIT_ASSERT(true);      DummyThread t;
75        t.SignalStartThread();
76        usleep(200000); // wait 200ms
77        CPPUNIT_ASSERT(t.resource == 1);
78        doubleLockSucceeded = (t.resource == 1);
79    }
80    
81    // Check if the previous tests namely 'testLock()' and 'testUnlock()' still work with double locking previously
82    void MutexTest::testDoubleLockStillBlocksConcurrentThread() {
83      bool success = false;
84      // check if testDoubleLock() succeeds, otherwise this test doesnt make sense anyway
85      doubleLockSucceeded = false;
86      testDoubleLock();
87      if (doubleLockSucceeded) {
88          ConcurrentThread t;
89          t.mutex.Lock();
90          t.mutex.Lock();
91          t.SignalStartThread();
92          usleep(400000); // wait 400ms
93          success = (t.resource == 0);
94          t.mutex.Unlock();
95    
96          if (success) {
97              usleep(200000); // wait 200ms
98              success = (t.resource == 1);
99          }
100      }
101      CPPUNIT_ASSERT(success);
102  }  }

Legend:
Removed from v.62  
changed lines
  Added in v.63

  ViewVC Help
Powered by ViewVC