blob: f4fa81dbd44a779a16e6e99f0bff7879cc622f13 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceUtil/IceUtil.h>
#include <MonitorRecMutexTest.h>
#include <TestCommon.h>
using namespace std;
using namespace IceUtil;
class MonitorRecMutexTestThread : public Thread
{
public:
MonitorRecMutexTestThread(Monitor<RecMutex>& m) :
_monitor(m),
_tryLock(false)
{
}
virtual void run()
{
Monitor<RecMutex>::TryLock tlock(_monitor);
test(!tlock.acquired());
{
Mutex::Lock lock(_tryLockMutex);
_tryLock = true;
}
_tryLockCond.signal();
Monitor<RecMutex>::Lock lock(_monitor);
}
void
waitTryLock()
{
Mutex::Lock lock(_tryLockMutex);
while(!_tryLock)
{
_tryLockCond.wait(lock);
}
}
private:
Monitor<RecMutex>& _monitor;
bool _tryLock;
//
// Use native Condition variable here, not Monitor.
//
Cond _tryLockCond;
Mutex _tryLockMutex;
};
typedef Handle<MonitorRecMutexTestThread> MonitorRecMutexTestThreadPtr;
class MonitorRecMutexTestThread2 : public Thread, public Monitor<RecMutex>
{
public:
MonitorRecMutexTestThread2(Monitor<RecMutex>& monitor) :
finished(false),
_monitor(monitor)
{
}
virtual void run()
{
Monitor<RecMutex>::Lock lock(_monitor);
_monitor.wait();
finished = true;
}
bool finished;
private:
Monitor<RecMutex>& _monitor;
};
typedef Handle<MonitorRecMutexTestThread2> MonitorRecMutexTestThread2Ptr;
static const string monitorRecMutexTestName("monitor<recmutex>");
MonitorRecMutexTest::MonitorRecMutexTest() :
TestBase(monitorRecMutexTestName)
{
}
void
MonitorRecMutexTest::run()
{
Monitor<RecMutex> monitor;
MonitorRecMutexTestThreadPtr t;
MonitorRecMutexTestThread2Ptr t2;
MonitorRecMutexTestThread2Ptr t3;
ThreadControl control;
ThreadControl control2;
{
Monitor<RecMutex>::Lock lock(monitor);
Monitor<RecMutex>::TryLock lock2(monitor);
test(lock2.acquired());
// TEST: TryLock
Monitor<RecMutex>::TryLock tlock(monitor);
test(tlock.acquired());
// TEST: Start thread, try to acquire the mutex.
t = new MonitorRecMutexTestThread(monitor);
control = t->start();
// TEST: Wait until the tryLock has been tested.
t->waitTryLock();
}
//
// TEST: Once the mutex has been released, the thread should
// acquire the mutex and then terminate.
//
control.join();
// TEST: notify() wakes one consumer.
t2 = new MonitorRecMutexTestThread2(monitor);
control = t2->start();
t3 = new MonitorRecMutexTestThread2(monitor);
control2 = t3->start();
// Give the thread time to start waiting.
ThreadControl::sleep(Time::seconds(1));
{
Monitor<RecMutex>::Lock lock(monitor);
monitor.notify();
}
// Give one thread time to terminate
ThreadControl::sleep(Time::seconds(1));
test((t2->finished && !t3->finished) || (t3->finished && !t2->finished));
{
Monitor<RecMutex>::Lock lock(monitor);
monitor.notify();
}
control.join();
control2.join();
// TEST: notifyAll() wakes one consumer.
t2 = new MonitorRecMutexTestThread2(monitor);
control = t2->start();
t3 = new MonitorRecMutexTestThread2(monitor);
control2 = t3->start();
// Give the threads time to start waiting.
ThreadControl::sleep(Time::seconds(1));
{
Monitor<RecMutex>::Lock lock(monitor);
monitor.notifyAll();
}
control.join();
control2.join();
// TEST: timedWait
{
Monitor<RecMutex>::Lock lock(monitor);
test(!monitor.timedWait(Time::milliSeconds(500)));
}
}
|