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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3552 - (hide annotations) (download)
Tue Aug 6 13:18:59 2019 UTC (4 years, 8 months ago) by schoenebeck
File size: 3124 byte(s)
* Test cases: Fixed thread tests segfaulting on Linux.

1 schoenebeck 57 #include "MutexTest.h"
2    
3     #include <iostream>
4    
5     CPPUNIT_TEST_SUITE_REGISTRATION(MutexTest);
6    
7 schoenebeck 211 using namespace std;
8 schoenebeck 57
9 schoenebeck 211
10 schoenebeck 57 // ConcurrentThread
11    
12 schoenebeck 3548 MutexTest::ConcurrentThread::ConcurrentThread() : Thread(false, false, 0, -4),
13     mutex(Mutex::RECURSIVE)
14     {
15 schoenebeck 57 resource = 0;
16     }
17    
18     int MutexTest::ConcurrentThread::Main() {
19     mutex.Lock();
20     resource++;
21     mutex.Unlock();
22 schoenebeck 3552 return 0;
23 schoenebeck 57 }
24    
25    
26 schoenebeck 63 // DummyThread
27    
28 schoenebeck 3548 MutexTest::DummyThread::DummyThread() : Thread(false, false, 0, -4),
29     mutex(Mutex::RECURSIVE)
30     {
31 schoenebeck 63 resource = 0;
32     }
33    
34     int MutexTest::DummyThread::Main() {
35     mutex.Lock();
36     mutex.Lock();
37     resource++;
38     mutex.Unlock();
39 schoenebeck 3552 return 0;
40 schoenebeck 63 }
41    
42    
43 schoenebeck 57 // MutexTest
44    
45 schoenebeck 211 void MutexTest::printTestSuiteName() {
46     cout << "\b \nRunning Mutex Tests: " << flush;
47     }
48    
49 schoenebeck 57 void MutexTest::setUp() {
50     }
51    
52     void MutexTest::tearDown() {
53     }
54    
55 schoenebeck 63 // Check with only one thread (thus no concurrency) if locking and unlocking the Mutex works without getting the thread to be locked falsely.
56 schoenebeck 57 void MutexTest::testLockAndUnlockBySingleThread() {
57     ConcurrentThread t;
58     t.StartThread();
59     usleep(200000); // wait 200ms
60     CPPUNIT_ASSERT(t.resource == 1);
61     }
62    
63 schoenebeck 63 // Now check with two concurrent threads if one thread can block the other one by using the Mutex.
64 schoenebeck 57 void MutexTest::testLock() {
65     ConcurrentThread t;
66     t.mutex.Lock();
67     t.SignalStartThread();
68     usleep(400000); // wait 400ms
69     CPPUNIT_ASSERT(t.resource == 0);
70     t.mutex.Unlock();
71     }
72    
73 schoenebeck 63 // Now check if the blocked thread get unblocked when thread that owns the Mutex unlocks the Mutex again.
74 schoenebeck 57 void MutexTest::testUnlock() {
75 schoenebeck 63 ConcurrentThread t;
76     t.mutex.Lock();
77     t.SignalStartThread();
78     usleep(400000); // wait 400ms
79     t.mutex.Unlock();
80     usleep(400000); // wait 400ms
81     CPPUNIT_ASSERT(t.resource == 1);
82 schoenebeck 57 }
83    
84 schoenebeck 63 // Check if the same thread can lock the Mutex twice in a row without unlocking it and without getting blocked itself.
85 schoenebeck 57 void MutexTest::testDoubleLock() {
86 schoenebeck 63 DummyThread t;
87     t.SignalStartThread();
88     usleep(200000); // wait 200ms
89     CPPUNIT_ASSERT(t.resource == 1);
90     doubleLockSucceeded = (t.resource == 1);
91 schoenebeck 57 }
92 schoenebeck 63
93     // Check if the previous tests namely 'testLock()' and 'testUnlock()' still work with double locking previously
94     void MutexTest::testDoubleLockStillBlocksConcurrentThread() {
95     // check if testDoubleLock() succeeds, otherwise this test doesnt make sense anyway
96     doubleLockSucceeded = false;
97     testDoubleLock();
98     if (doubleLockSucceeded) {
99     ConcurrentThread t;
100     t.mutex.Lock();
101     t.mutex.Lock();
102     t.SignalStartThread();
103     usleep(400000); // wait 400ms
104 schoenebeck 3548 bool success = (t.resource == 0);
105     CPPUNIT_ASSERT(success);
106 schoenebeck 63 if (success) {
107 schoenebeck 3548 t.mutex.Unlock();
108 schoenebeck 63 usleep(200000); // wait 200ms
109 schoenebeck 3548 success = (t.resource == 0); // thread should should still be blocked, since recursive mutex
110     CPPUNIT_ASSERT(success);
111 schoenebeck 63 }
112 schoenebeck 3548 if (success) {
113     t.mutex.Unlock();
114     usleep(200000); // wait 200ms
115     CPPUNIT_ASSERT(t.resource == 1); // unlock count matches previous lock count, so thread should have run through now
116     }
117     } else {
118     CPPUNIT_ASSERT(false);
119 schoenebeck 63 }
120     }

  ViewVC Help
Powered by ViewVC