blob: 85ef20dc409ae0e1bdaca5d476502e6ec5fac5f2 [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"
Peter Boström455a2522015-12-18 17:00:25 +010017#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include "webrtc/base/scopedptrcollection.h"
19#include "webrtc/base/thread.h"
henrike@webrtc.orgfded02c2014-09-19 13:10:10 +000020#include "webrtc/test/testsupport/gtest_disable.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
22namespace rtc {
23
24namespace {
25
26const int kLongTime = 10000; // 10 seconds
27const int kNumThreads = 16;
28const int kOperationsToRun = 1000;
29
Jiayang Liubef8d2d2015-03-26 14:38:46 -070030class UniqueValueVerifier {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031 public:
Jiayang Liubef8d2d2015-03-26 14:38:46 -070032 void Verify(const std::vector<int>& values) {
33 for (size_t i = 0; i < values.size(); ++i) {
34 std::pair<std::set<int>::iterator, bool> result =
35 all_values_.insert(values[i]);
36 // Each value should only be taken by one thread, so if this value
37 // has already been added, something went wrong.
38 EXPECT_TRUE(result.second)
39 << " Thread=" << Thread::Current() << " value=" << values[i];
40 }
41 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042
Jiayang Liubef8d2d2015-03-26 14:38:46 -070043 void Finalize() {}
44
45 private:
46 std::set<int> all_values_;
47};
48
49class CompareAndSwapVerifier {
50 public:
51 CompareAndSwapVerifier() : zero_count_(0) {}
52
53 void Verify(const std::vector<int>& values) {
54 for (auto v : values) {
55 if (v == 0) {
56 EXPECT_EQ(0, zero_count_) << "Thread=" << Thread::Current();
57 ++zero_count_;
58 } else {
59 EXPECT_EQ(1, v) << " Thread=" << Thread::Current();
60 }
61 }
62 }
63
64 void Finalize() {
65 EXPECT_EQ(1, zero_count_);
66 }
67 private:
68 int zero_count_;
69};
70
71class RunnerBase : public MessageHandler {
72 public:
73 explicit RunnerBase(int value)
74 : threads_active_(0),
75 start_event_(true, false),
76 done_event_(true, false),
77 shared_value_(value) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078
79 bool Run() {
80 // Signal all threads to start.
81 start_event_.Set();
82
83 // Wait for all threads to finish.
84 return done_event_.Wait(kLongTime);
85 }
86
87 void SetExpectedThreadCount(int count) {
88 threads_active_ = count;
89 }
90
Jiayang Liubef8d2d2015-03-26 14:38:46 -070091 int shared_value() const { return shared_value_; }
92
93 protected:
94 // Derived classes must override OnMessage, and call BeforeStart and AfterEnd
95 // at the beginning and the end of OnMessage respectively.
96 void BeforeStart() {
97 ASSERT_TRUE(start_event_.Wait(kLongTime));
98 }
99
100 // Returns true if all threads have finished.
101 bool AfterEnd() {
102 if (AtomicOps::Decrement(&threads_active_) == 0) {
103 done_event_.Set();
104 return true;
105 }
106 return false;
107 }
108
109 int threads_active_;
110 Event start_event_;
111 Event done_event_;
112 int shared_value_;
113};
114
115class LOCKABLE CriticalSectionLock {
116 public:
117 void Lock() EXCLUSIVE_LOCK_FUNCTION() {
118 cs_.Enter();
119 }
120 void Unlock() UNLOCK_FUNCTION() {
121 cs_.Leave();
122 }
123
124 private:
125 CriticalSection cs_;
126};
127
128template <class Lock>
129class LockRunner : public RunnerBase {
130 public:
131 LockRunner() : RunnerBase(0) {}
132
133 void OnMessage(Message* msg) override {
134 BeforeStart();
135
136 lock_.Lock();
137
138 EXPECT_EQ(0, shared_value_);
139 int old = shared_value_;
140
141 // Use a loop to increase the chance of race.
142 for (int i = 0; i < kOperationsToRun; ++i) {
143 ++shared_value_;
144 }
145 EXPECT_EQ(old + kOperationsToRun, shared_value_);
146 shared_value_ = 0;
147
148 lock_.Unlock();
149
150 AfterEnd();
151 }
152
153 private:
154 Lock lock_;
155};
156
157template <class Op, class Verifier>
158class AtomicOpRunner : public RunnerBase {
159 public:
160 explicit AtomicOpRunner(int initial_value) : RunnerBase(initial_value) {}
161
162 void OnMessage(Message* msg) override {
163 BeforeStart();
164
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165 std::vector<int> values;
166 values.reserve(kOperationsToRun);
167
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700168 // Generate a bunch of values by updating shared_value_ atomically.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169 for (int i = 0; i < kOperationsToRun; ++i) {
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700170 values.push_back(Op::AtomicOp(&shared_value_));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171 }
172
173 { // Add them all to the set.
174 CritScope cs(&all_values_crit_);
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700175 verifier_.Verify(values);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176 }
177
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700178 if (AfterEnd()) {
179 verifier_.Finalize();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000180 }
181 }
182
183 private:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000184 CriticalSection all_values_crit_;
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700185 Verifier verifier_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000186};
187
188struct IncrementOp {
189 static int AtomicOp(int* i) { return AtomicOps::Increment(i); }
190};
191
192struct DecrementOp {
193 static int AtomicOp(int* i) { return AtomicOps::Decrement(i); }
194};
195
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700196struct CompareAndSwapOp {
197 static int AtomicOp(int* i) { return AtomicOps::CompareAndSwap(i, 0, 1); }
198};
199
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000200void StartThreads(ScopedPtrCollection<Thread>* threads,
201 MessageHandler* handler) {
202 for (int i = 0; i < kNumThreads; ++i) {
203 Thread* thread = new Thread();
204 thread->Start();
205 thread->Post(handler);
206 threads->PushBack(thread);
207 }
208}
209
210} // namespace
211
212TEST(AtomicOpsTest, Simple) {
213 int value = 0;
214 EXPECT_EQ(1, AtomicOps::Increment(&value));
215 EXPECT_EQ(1, value);
216 EXPECT_EQ(2, AtomicOps::Increment(&value));
217 EXPECT_EQ(2, value);
218 EXPECT_EQ(1, AtomicOps::Decrement(&value));
219 EXPECT_EQ(1, value);
220 EXPECT_EQ(0, AtomicOps::Decrement(&value));
221 EXPECT_EQ(0, value);
222}
223
Peter Boström455a2522015-12-18 17:00:25 +0100224TEST(AtomicOpsTest, SimplePtr) {
225 class Foo {};
226 Foo* volatile foo = nullptr;
227 scoped_ptr<Foo> a(new Foo());
228 scoped_ptr<Foo> b(new Foo());
229 // Reading the initial value should work as expected.
230 EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == nullptr);
231 // Setting using compare and swap should work.
232 EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr(
233 &foo, static_cast<Foo*>(nullptr), a.get()) == nullptr);
234 EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == a.get());
235 // Setting another value but with the wrong previous pointer should fail
236 // (remain a).
237 EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr(
238 &foo, static_cast<Foo*>(nullptr), b.get()) == a.get());
239 EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == a.get());
240 // Replacing a with b should work.
241 EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr(&foo, a.get(), b.get()) ==
242 a.get());
243 EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == b.get());
244}
245
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000246TEST(AtomicOpsTest, Increment) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000247 // Create and start lots of threads.
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700248 AtomicOpRunner<IncrementOp, UniqueValueVerifier> runner(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000249 ScopedPtrCollection<Thread> threads;
250 StartThreads(&threads, &runner);
251 runner.SetExpectedThreadCount(kNumThreads);
252
253 // Release the hounds!
254 EXPECT_TRUE(runner.Run());
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700255 EXPECT_EQ(kOperationsToRun * kNumThreads, runner.shared_value());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000256}
257
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000258TEST(AtomicOpsTest, Decrement) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259 // Create and start lots of threads.
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700260 AtomicOpRunner<DecrementOp, UniqueValueVerifier> runner(
261 kOperationsToRun * kNumThreads);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262 ScopedPtrCollection<Thread> threads;
263 StartThreads(&threads, &runner);
264 runner.SetExpectedThreadCount(kNumThreads);
265
266 // Release the hounds!
267 EXPECT_TRUE(runner.Run());
Jiayang Liubef8d2d2015-03-26 14:38:46 -0700268 EXPECT_EQ(0, runner.shared_value());
269}
270
271TEST(AtomicOpsTest, CompareAndSwap) {
272 // Create and start lots of threads.
273 AtomicOpRunner<CompareAndSwapOp, CompareAndSwapVerifier> runner(0);
274 ScopedPtrCollection<Thread> threads;
275 StartThreads(&threads, &runner);
276 runner.SetExpectedThreadCount(kNumThreads);
277
278 // Release the hounds!
279 EXPECT_TRUE(runner.Run());
280 EXPECT_EQ(1, runner.shared_value());
281}
282
283TEST(GlobalLockTest, Basic) {
284 // Create and start lots of threads.
285 LockRunner<GlobalLock> runner;
286 ScopedPtrCollection<Thread> threads;
287 StartThreads(&threads, &runner);
288 runner.SetExpectedThreadCount(kNumThreads);
289
290 // Release the hounds!
291 EXPECT_TRUE(runner.Run());
292 EXPECT_EQ(0, runner.shared_value());
293}
294
295TEST(CriticalSectionTest, Basic) {
296 // Create and start lots of threads.
297 LockRunner<CriticalSectionLock> runner;
298 ScopedPtrCollection<Thread> threads;
299 StartThreads(&threads, &runner);
300 runner.SetExpectedThreadCount(kNumThreads);
301
302 // Release the hounds!
303 EXPECT_TRUE(runner.Run());
304 EXPECT_EQ(0, runner.shared_value());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000305}
306
Magnus Jedvert6bf10842015-04-23 11:37:55 +0200307#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
308TEST(CriticalSectionTest, IsLocked) {
309 // Simple single-threaded test of IsLocked.
310 CriticalSection cs;
311 EXPECT_FALSE(cs.IsLocked());
312 cs.Enter();
313 EXPECT_TRUE(cs.IsLocked());
314 cs.Leave();
315 EXPECT_FALSE(cs.IsLocked());
316 if (!cs.TryEnter())
317 FAIL();
318 EXPECT_TRUE(cs.IsLocked());
319 cs.Leave();
320 EXPECT_FALSE(cs.IsLocked());
321}
322#endif
323
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000324} // namespace rtc