blob: 7742466e2bd8aaa8f67e90c881e4c6eedbd9fc3f (
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
|
// **********************************************************************
//
// Copyright (c) 2001
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************
#include <IceUtil/IceUtil.h>
#include <RecMutexTest.h>
#include <TestCommon.h>
using namespace std;
using namespace IceUtil;
static const string recMutexTestName("recursive mutex");
class RecMutexTestThread : public Thread
{
public:
RecMutexTestThread(RecMutex& m) :
_mutex(m),
_trylock(false)
{
}
virtual void run()
{
try
{
RecMutex::TryLock lock(_mutex);
test(false);
}
catch(const ThreadLockedException&)
{
// Expected
}
_trylock = true;
_trylockCond.signal();
RecMutex::Lock lock(_mutex);
}
void
waitTrylock()
{
Mutex::Lock lock(_trylockMutex);
while(!_trylock)
{
_trylockCond.wait(lock);
}
}
private:
RecMutex& _mutex;
bool _trylock;
//
// Use native Condition variable here, not Monitor.
//
Cond _trylockCond;
Mutex _trylockMutex;
};
typedef Handle<RecMutexTestThread> RecMutexTestThreadPtr;
RecMutexTest::RecMutexTest() :
TestBase(recMutexTestName)
{
}
void
RecMutexTest::run()
{
RecMutex mutex;
RecMutexTestThreadPtr t;
ThreadControl control;
{
RecMutex::Lock lock(mutex);
// TEST: lock twice
RecMutex::Lock lock2(mutex);
// TEST: TryLock
RecMutex::TryLock lock3(mutex);
// TEST: Start thread, try to acquire the mutex.
t = new RecMutexTestThread(mutex);
control = t->start();
// TEST: Wait until the trylock has been tested.
t->waitTrylock();
}
//
// TEST: Once the recursive mutex has been released, the thread
// should acquire the mutex and then terminate.
//
control.join();
}
|