blob: ff4fdef9486f2a19ca83450c9175d205039e55de [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <set>
12#include <vector>
13
14#include "webrtc/base/criticalsection.h"
15#include "webrtc/base/event.h"
16#include "webrtc/base/gunit.h"
17#include "webrtc/base/scopedptrcollection.h"
18#include "webrtc/base/thread.h"
henrike@webrtc.orgfded02c2014-09-19 13:10:10 +000019#include "webrtc/test/testsupport/gtest_disable.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
21namespace rtc {
22
23namespace {
24
25const int kLongTime = 10000; // 10 seconds
26const int kNumThreads = 16;
27const int kOperationsToRun = 1000;
28
Jiayang Liubef8d2d2015-03-26 14:38:46 -070029class UniqueValueVerifier {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030 public:
Jiayang Liubef8d2d2015-03-26 14:38:46 -070031 void Verify(const std::vector<int>& values) {
32 for (size_t i = 0; i < values.size(); ++i) {
33 std::pair<std::set<int>::iterator, bool> result =
34 all_values_.insert(values[i]);
35 // Each value should only be taken by one thread, so if this value
36 // has already been added, something went wrong.
37 EXPECT_TRUE(result.second)
38 << " Thread=" << Thread::Current() << " value=" << values[i];
39 }
40 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041
Jiayang Liubef8d2d2015-03-26 14:38:46 -070042 void Finalize() {}
43
44 private:
45 std::set<int> all_values_;
46};
47
48class CompareAndSwapVerifier {
49 public:
50 CompareAndSwapVerifier() : zero_count_(0) {}
51
52 void Verify(const std::vector<int>& values) {
53 for (auto v : values) {
54 if (v == 0) {
55 EXPECT_EQ(0, zero_count_) << "Thread=" << Thread::Current();
56 ++zero_count_;
57 } else {
58 EXPECT_EQ(1, v) << " Thread=" << Thread::Current();
59 }
60 }
61 }
62
63 void Finalize() {
64 EXPECT_EQ(1, zero_count_);
65 }
66 private:
67 int zero_count_;
68};
69
70class RunnerBase : public MessageHandler {
71 public:
72 explicit RunnerBase(int value)
73 : threads_active_(0),
74 start_event_(true, false),
75 done_event_(true, false),
76 shared_value_(value) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077
78 bool Run() {
79 // Signal all threads to start.
80 start_event_.Set();
81
82 // Wait for all threads to finish.
83 return done_event_.Wait(kLongTime);
84 }
85
86 void SetExpectedThreadCount(int count) {
87 threads_active_ = count;
88 }
89
Jiayang Liubef8d2d2015-03-26 14:38:46 -070090 int shared_value() const { return shared_value_; }
91
92 protected:
93 // Derived classes must override OnMessage, and call BeforeStart and AfterEnd
94 // at the beginning and the end of OnMessage respectively.
95 void BeforeStart() {
96 ASSERT_TRUE(start_event_.Wait(kLongTime));
97 }
98
99 // Returns true if all threads have finished.
100 bool AfterEnd() {
101 if (AtomicOps::Decrement(&threads_active_) == 0) {
102 done_event_.Set();
103 return true;
104 }
105 return false;
106 }
107
108 int threads_active_;
109 Event start_event_;
110 Event done_event_;
111 int shared_value_;
112};
113
114class LOCKABLE CriticalSectionLock {
115 public:
116 void Lock() EXCLUSIVE_LOCK_FUNCTION() {
117 cs_.Enter();
118 }
119 void Unlock() UNLOCK_FUNCTION() {
120 cs_.Leave();
121 }
122
123 private:
124 CriticalSection cs_;
125};
126
127template <class Lock>
128class LockRunner : public RunnerBase {
129 public:
130 LockRunner() : RunnerBase(0) {}
131
132 void OnMessage(Message* msg) override {
133 BeforeStart();
134
135 lock_.Lock();
136
137 EXPECT_EQ(0, shared_value_);
138 int old = shared_value_;
139
140 // Use a loop to increase the chance of race.
141 for (int i = 0; i < kOperationsToRun; ++i) {
142 ++shared_value_;
143 }
144 EXPECT_EQ(old + kOperationsToRun, shared_value_);
145 shared_value_ = 0;
146
147 lock_.Unlock();
148
149 AfterEnd();
150 }
151
152 private:
153 Lock lock_;
154};
155
156template <class Op, class Verifier>
157class AtomicOpRunner : public RunnerBase {
158 public:
159 explicit AtomicOpRunner(int initial_value) : RunnerBase(initial_value) {}
160
161 void OnMessage(Message* msg) override {
162 BeforeStart();
163
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000164 std::vector<int> values;
165 values.reserve(kOperationsToRun);
166
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700167 // Generate a bunch of values by updating shared_value_ atomically.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 for (int i = 0; i < kOperationsToRun; ++i) {
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700169 values.push_back(Op::AtomicOp(&shared_value_));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000170 }
171
172 { // Add them all to the set.
173 CritScope cs(&all_values_crit_);
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700174 verifier_.Verify(values);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175 }
176
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700177 if (AfterEnd()) {
178 verifier_.Finalize();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000179 }
180 }
181
182 private:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183 CriticalSection all_values_crit_;
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700184 Verifier verifier_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000185};
186
187struct IncrementOp {
188 static int AtomicOp(int* i) { return AtomicOps::Increment(i); }
189};
190
191struct DecrementOp {
192 static int AtomicOp(int* i) { return AtomicOps::Decrement(i); }
193};
194
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700195struct CompareAndSwapOp {
196 static int AtomicOp(int* i) { return AtomicOps::CompareAndSwap(i, 0, 1); }
197};
198
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199void StartThreads(ScopedPtrCollection<Thread>* threads,
200 MessageHandler* handler) {
201 for (int i = 0; i < kNumThreads; ++i) {
202 Thread* thread = new Thread();
203 thread->Start();
204 thread->Post(handler);
205 threads->PushBack(thread);
206 }
207}
208
209} // namespace
210
211TEST(AtomicOpsTest, Simple) {
212 int value = 0;
213 EXPECT_EQ(1, AtomicOps::Increment(&value));
214 EXPECT_EQ(1, value);
215 EXPECT_EQ(2, AtomicOps::Increment(&value));
216 EXPECT_EQ(2, value);
217 EXPECT_EQ(1, AtomicOps::Decrement(&value));
218 EXPECT_EQ(1, value);
219 EXPECT_EQ(0, AtomicOps::Decrement(&value));
220 EXPECT_EQ(0, value);
221}
222
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000223TEST(AtomicOpsTest, Increment) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000224 // Create and start lots of threads.
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700225 AtomicOpRunner<IncrementOp, UniqueValueVerifier> runner(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000226 ScopedPtrCollection<Thread> threads;
227 StartThreads(&threads, &runner);
228 runner.SetExpectedThreadCount(kNumThreads);
229
230 // Release the hounds!
231 EXPECT_TRUE(runner.Run());
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700232 EXPECT_EQ(kOperationsToRun * kNumThreads, runner.shared_value());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000233}
234
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000235TEST(AtomicOpsTest, Decrement) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236 // Create and start lots of threads.
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700237 AtomicOpRunner<DecrementOp, UniqueValueVerifier> runner(
238 kOperationsToRun * kNumThreads);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 ScopedPtrCollection<Thread> threads;
240 StartThreads(&threads, &runner);
241 runner.SetExpectedThreadCount(kNumThreads);
242
243 // Release the hounds!
244 EXPECT_TRUE(runner.Run());
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700245 EXPECT_EQ(0, runner.shared_value());
246}
247
248TEST(AtomicOpsTest, CompareAndSwap) {
249 // Create and start lots of threads.
250 AtomicOpRunner<CompareAndSwapOp, CompareAndSwapVerifier> runner(0);
251 ScopedPtrCollection<Thread> threads;
252 StartThreads(&threads, &runner);
253 runner.SetExpectedThreadCount(kNumThreads);
254
255 // Release the hounds!
256 EXPECT_TRUE(runner.Run());
257 EXPECT_EQ(1, runner.shared_value());
258}
259
260TEST(GlobalLockTest, Basic) {
261 // Create and start lots of threads.
262 LockRunner<GlobalLock> runner;
263 ScopedPtrCollection<Thread> threads;
264 StartThreads(&threads, &runner);
265 runner.SetExpectedThreadCount(kNumThreads);
266
267 // Release the hounds!
268 EXPECT_TRUE(runner.Run());
269 EXPECT_EQ(0, runner.shared_value());
270}
271
272TEST(CriticalSectionTest, Basic) {
273 // Create and start lots of threads.
274 LockRunner<CriticalSectionLock> runner;
275 ScopedPtrCollection<Thread> threads;
276 StartThreads(&threads, &runner);
277 runner.SetExpectedThreadCount(kNumThreads);
278
279 // Release the hounds!
280 EXPECT_TRUE(runner.Run());
281 EXPECT_EQ(0, runner.shared_value());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000282}
283
Magnus Jedvert6bf10842015-04-23 11:37:55 +0200284#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
285TEST(CriticalSectionTest, IsLocked) {
286 // Simple single-threaded test of IsLocked.
287 CriticalSection cs;
288 EXPECT_FALSE(cs.IsLocked());
289 cs.Enter();
290 EXPECT_TRUE(cs.IsLocked());
291 cs.Leave();
292 EXPECT_FALSE(cs.IsLocked());
293 if (!cs.TryEnter())
294 FAIL();
295 EXPECT_TRUE(cs.IsLocked());
296 cs.Leave();
297 EXPECT_FALSE(cs.IsLocked());
298}
299#endif
300
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000301} // namespace rtc