9 |
|
|
10 |
// ConcurrentThread |
// ConcurrentThread |
11 |
|
|
12 |
MutexTest::ConcurrentThread::ConcurrentThread() : Thread(false, false, 0, -4) { |
MutexTest::ConcurrentThread::ConcurrentThread() : Thread(false, false, 0, -4), |
13 |
|
mutex(Mutex::RECURSIVE) |
14 |
|
{ |
15 |
resource = 0; |
resource = 0; |
16 |
} |
} |
17 |
|
|
24 |
|
|
25 |
// DummyThread |
// DummyThread |
26 |
|
|
27 |
MutexTest::DummyThread::DummyThread() : Thread(false, false, 0, -4) { |
MutexTest::DummyThread::DummyThread() : Thread(false, false, 0, -4), |
28 |
|
mutex(Mutex::RECURSIVE) |
29 |
|
{ |
30 |
resource = 0; |
resource = 0; |
31 |
} |
} |
32 |
|
|
90 |
|
|
91 |
// Check if the previous tests namely 'testLock()' and 'testUnlock()' still work with double locking previously |
// Check if the previous tests namely 'testLock()' and 'testUnlock()' still work with double locking previously |
92 |
void MutexTest::testDoubleLockStillBlocksConcurrentThread() { |
void MutexTest::testDoubleLockStillBlocksConcurrentThread() { |
|
bool success = false; |
|
93 |
// check if testDoubleLock() succeeds, otherwise this test doesnt make sense anyway |
// check if testDoubleLock() succeeds, otherwise this test doesnt make sense anyway |
94 |
doubleLockSucceeded = false; |
doubleLockSucceeded = false; |
95 |
testDoubleLock(); |
testDoubleLock(); |
99 |
t.mutex.Lock(); |
t.mutex.Lock(); |
100 |
t.SignalStartThread(); |
t.SignalStartThread(); |
101 |
usleep(400000); // wait 400ms |
usleep(400000); // wait 400ms |
102 |
success = (t.resource == 0); |
bool success = (t.resource == 0); |
103 |
t.mutex.Unlock(); |
CPPUNIT_ASSERT(success); |
104 |
|
if (success) { |
105 |
|
t.mutex.Unlock(); |
106 |
|
usleep(200000); // wait 200ms |
107 |
|
success = (t.resource == 0); // thread should should still be blocked, since recursive mutex |
108 |
|
CPPUNIT_ASSERT(success); |
109 |
|
} |
110 |
if (success) { |
if (success) { |
111 |
|
t.mutex.Unlock(); |
112 |
usleep(200000); // wait 200ms |
usleep(200000); // wait 200ms |
113 |
success = (t.resource == 1); |
CPPUNIT_ASSERT(t.resource == 1); // unlock count matches previous lock count, so thread should have run through now |
114 |
} |
} |
115 |
|
} else { |
116 |
|
CPPUNIT_ASSERT(false); |
117 |
} |
} |
|
CPPUNIT_ASSERT(success); |
|
118 |
} |
} |