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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3548 - (show annotations) (download)
Wed Jul 31 09:35:10 2019 UTC (4 years, 8 months ago) by schoenebeck
File size: 3096 byte(s)
* Fixed compiler error in Pool.h.
* Fixed compiler errors in test cases.
* Updated README for how to compile & run test cases.
* Updated test case
  MutexTest::testDoubleLockStillBlocksConcurrentThread() to latest
  expected behaviour of the Mutex class implementation (recursive
  mutex type).
* Bumped version (2.1.1.svn1).

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

  ViewVC Help
Powered by ViewVC