-
Notifications
You must be signed in to change notification settings - Fork 3
/
mutex_test.cc
145 lines (130 loc) · 3.33 KB
/
mutex_test.cc
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
// Copyright 2007 Google Inc. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// Author: [email protected] (Nigel D'souza)
#include "gtagsunit.h"
#include "mutex.h"
#include "thread.h"
namespace {
TEST(MutexTest, TryLockTest) {
{
gtags::Mutex m;
EXPECT_TRUE(m.TryLock());
m.Unlock();
}
{
gtags::Mutex m;
m.Lock();
EXPECT_FALSE(m.TryLock());
m.Unlock();
}
}
TEST(MutexTest, UnlockTest) {
{
gtags::Mutex m;
EXPECT_TRUE(m.TryLock());
m.Unlock();
}
{
gtags::Mutex m;
m.Lock();
m.Unlock();
EXPECT_TRUE(m.TryLock());
m.Unlock();
}
{
gtags::Mutex m;
m.Unlock();
m.Unlock();
m.Unlock();
EXPECT_TRUE(m.TryLock());
EXPECT_FALSE(m.TryLock());
m.Unlock();
}
}
class IncrementThread : public gtags::Thread {
public:
IncrementThread(int *x, int count, gtags::Mutex *m) :
Thread(true), x_(*x), count_(count), m_(m) {}
protected:
virtual void Run() {
for (int i = 0; i < count_; ++i) {
if (m_) m_->Lock();
Increment();
if (m_) m_->Unlock();
}
}
virtual void Increment() = 0;
int &x_;
int count_;
gtags::Mutex *m_;
};
class IncrementThreadSlow : public IncrementThread {
public:
IncrementThreadSlow(int *x, int count, gtags::Mutex *m)
: IncrementThread(x, count, m) {}
protected:
virtual void Increment() {
const int kSlowFactor = 10000;
int y = x_;
y++;
for (int i = 0; i < kSlowFactor; ++i) {
EXPECT_GT(i * y, -1); // prevent optimization of method to ++x_
}
x_ = y;
}
};
class IncrementThreadFast : public IncrementThread {
public:
IncrementThreadFast(int *x, int count, gtags::Mutex *s)
: IncrementThread(x, count, s) {}
protected:
virtual void Increment() {
++x_;
}
};
TEST(MutexTest, ProtectionTest) {
const int kInitialCount = 1;
const int kCountIncrementFactor = 10;
const int kMaximumCount = 1000;
int x = 0;
int count = kInitialCount;
// Find the count for which we detect thread-unsafe behaviour due to
// unprotected access (overlapping writes to x).
for (; x == 2 * count && count <= kMaximumCount;
count *= kCountIncrementFactor) {
x = 0;
IncrementThreadSlow its(&x, count, NULL);
IncrementThreadFast itf(&x, count, NULL);
its.Start();
itf.Start();
its.Join();
itf.Join();
}
LOG(INFO) << "Finished unprotected with count=" << count;
EXPECT_LT(x, 2 * count);
// Repeat again with thread-safe behaviour.
x = 0;
gtags::Mutex m;
IncrementThreadSlow its(&x, count, &m);
IncrementThreadFast itf(&x, count, &m);
its.Start();
itf.Start();
its.Join();
itf.Join();
EXPECT_EQ(x, 2 * count);
}
} // namespace