OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <set> | 12 #include <set> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "webrtc/base/arraysize.h" | 15 #include "webrtc/base/arraysize.h" |
16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
17 #include "webrtc/base/criticalsection.h" | 17 #include "webrtc/base/criticalsection.h" |
18 #include "webrtc/base/event.h" | 18 #include "webrtc/base/event.h" |
19 #include "webrtc/base/gunit.h" | 19 #include "webrtc/base/gunit.h" |
20 #include "webrtc/base/platform_thread.h" | 20 #include "webrtc/base/platform_thread.h" |
21 #include "webrtc/base/scopedptrcollection.h" | |
22 #include "webrtc/base/thread.h" | 21 #include "webrtc/base/thread.h" |
23 | 22 |
24 namespace rtc { | 23 namespace rtc { |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 const int kLongTime = 10000; // 10 seconds | 27 const int kLongTime = 10000; // 10 seconds |
29 const int kNumThreads = 16; | 28 const int kNumThreads = 16; |
30 const int kOperationsToRun = 1000; | 29 const int kOperationsToRun = 1000; |
31 | 30 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 }; | 191 }; |
193 | 192 |
194 struct DecrementOp { | 193 struct DecrementOp { |
195 static int AtomicOp(int* i) { return AtomicOps::Decrement(i); } | 194 static int AtomicOp(int* i) { return AtomicOps::Decrement(i); } |
196 }; | 195 }; |
197 | 196 |
198 struct CompareAndSwapOp { | 197 struct CompareAndSwapOp { |
199 static int AtomicOp(int* i) { return AtomicOps::CompareAndSwap(i, 0, 1); } | 198 static int AtomicOp(int* i) { return AtomicOps::CompareAndSwap(i, 0, 1); } |
200 }; | 199 }; |
201 | 200 |
202 void StartThreads(ScopedPtrCollection<Thread>* threads, | 201 void StartThreads(std::vector<std::unique_ptr<Thread>>* threads, |
203 MessageHandler* handler) { | 202 MessageHandler* handler) { |
204 for (int i = 0; i < kNumThreads; ++i) { | 203 for (int i = 0; i < kNumThreads; ++i) { |
205 Thread* thread = new Thread(); | 204 std::unique_ptr<Thread> thread(new Thread()); |
206 thread->Start(); | 205 thread->Start(); |
207 thread->Post(RTC_FROM_HERE, handler); | 206 thread->Post(RTC_FROM_HERE, handler); |
208 threads->PushBack(thread); | 207 threads->push_back(std::move(thread)); |
209 } | 208 } |
210 } | 209 } |
211 | 210 |
212 } // namespace | 211 } // namespace |
213 | 212 |
214 TEST(AtomicOpsTest, Simple) { | 213 TEST(AtomicOpsTest, Simple) { |
215 int value = 0; | 214 int value = 0; |
216 EXPECT_EQ(1, AtomicOps::Increment(&value)); | 215 EXPECT_EQ(1, AtomicOps::Increment(&value)); |
217 EXPECT_EQ(1, value); | 216 EXPECT_EQ(1, value); |
218 EXPECT_EQ(2, AtomicOps::Increment(&value)); | 217 EXPECT_EQ(2, AtomicOps::Increment(&value)); |
(...skipping 22 matching lines...) Expand all Loading... |
241 EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == a.get()); | 240 EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == a.get()); |
242 // Replacing a with b should work. | 241 // Replacing a with b should work. |
243 EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr(&foo, a.get(), b.get()) == | 242 EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr(&foo, a.get(), b.get()) == |
244 a.get()); | 243 a.get()); |
245 EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == b.get()); | 244 EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == b.get()); |
246 } | 245 } |
247 | 246 |
248 TEST(AtomicOpsTest, Increment) { | 247 TEST(AtomicOpsTest, Increment) { |
249 // Create and start lots of threads. | 248 // Create and start lots of threads. |
250 AtomicOpRunner<IncrementOp, UniqueValueVerifier> runner(0); | 249 AtomicOpRunner<IncrementOp, UniqueValueVerifier> runner(0); |
251 ScopedPtrCollection<Thread> threads; | 250 std::vector<std::unique_ptr<Thread>> threads; |
252 StartThreads(&threads, &runner); | 251 StartThreads(&threads, &runner); |
253 runner.SetExpectedThreadCount(kNumThreads); | 252 runner.SetExpectedThreadCount(kNumThreads); |
254 | 253 |
255 // Release the hounds! | 254 // Release the hounds! |
256 EXPECT_TRUE(runner.Run()); | 255 EXPECT_TRUE(runner.Run()); |
257 EXPECT_EQ(kOperationsToRun * kNumThreads, runner.shared_value()); | 256 EXPECT_EQ(kOperationsToRun * kNumThreads, runner.shared_value()); |
258 } | 257 } |
259 | 258 |
260 TEST(AtomicOpsTest, Decrement) { | 259 TEST(AtomicOpsTest, Decrement) { |
261 // Create and start lots of threads. | 260 // Create and start lots of threads. |
262 AtomicOpRunner<DecrementOp, UniqueValueVerifier> runner( | 261 AtomicOpRunner<DecrementOp, UniqueValueVerifier> runner( |
263 kOperationsToRun * kNumThreads); | 262 kOperationsToRun * kNumThreads); |
264 ScopedPtrCollection<Thread> threads; | 263 std::vector<std::unique_ptr<Thread>> threads; |
265 StartThreads(&threads, &runner); | 264 StartThreads(&threads, &runner); |
266 runner.SetExpectedThreadCount(kNumThreads); | 265 runner.SetExpectedThreadCount(kNumThreads); |
267 | 266 |
268 // Release the hounds! | 267 // Release the hounds! |
269 EXPECT_TRUE(runner.Run()); | 268 EXPECT_TRUE(runner.Run()); |
270 EXPECT_EQ(0, runner.shared_value()); | 269 EXPECT_EQ(0, runner.shared_value()); |
271 } | 270 } |
272 | 271 |
273 TEST(AtomicOpsTest, CompareAndSwap) { | 272 TEST(AtomicOpsTest, CompareAndSwap) { |
274 // Create and start lots of threads. | 273 // Create and start lots of threads. |
275 AtomicOpRunner<CompareAndSwapOp, CompareAndSwapVerifier> runner(0); | 274 AtomicOpRunner<CompareAndSwapOp, CompareAndSwapVerifier> runner(0); |
276 ScopedPtrCollection<Thread> threads; | 275 std::vector<std::unique_ptr<Thread>> threads; |
277 StartThreads(&threads, &runner); | 276 StartThreads(&threads, &runner); |
278 runner.SetExpectedThreadCount(kNumThreads); | 277 runner.SetExpectedThreadCount(kNumThreads); |
279 | 278 |
280 // Release the hounds! | 279 // Release the hounds! |
281 EXPECT_TRUE(runner.Run()); | 280 EXPECT_TRUE(runner.Run()); |
282 EXPECT_EQ(1, runner.shared_value()); | 281 EXPECT_EQ(1, runner.shared_value()); |
283 } | 282 } |
284 | 283 |
285 TEST(GlobalLockTest, Basic) { | 284 TEST(GlobalLockTest, Basic) { |
286 // Create and start lots of threads. | 285 // Create and start lots of threads. |
287 LockRunner<GlobalLock> runner; | 286 LockRunner<GlobalLock> runner; |
288 ScopedPtrCollection<Thread> threads; | 287 std::vector<std::unique_ptr<Thread>> threads; |
289 StartThreads(&threads, &runner); | 288 StartThreads(&threads, &runner); |
290 runner.SetExpectedThreadCount(kNumThreads); | 289 runner.SetExpectedThreadCount(kNumThreads); |
291 | 290 |
292 // Release the hounds! | 291 // Release the hounds! |
293 EXPECT_TRUE(runner.Run()); | 292 EXPECT_TRUE(runner.Run()); |
294 EXPECT_EQ(0, runner.shared_value()); | 293 EXPECT_EQ(0, runner.shared_value()); |
295 } | 294 } |
296 | 295 |
297 TEST(CriticalSectionTest, Basic) { | 296 TEST(CriticalSectionTest, Basic) { |
298 // Create and start lots of threads. | 297 // Create and start lots of threads. |
299 LockRunner<CriticalSectionLock> runner; | 298 LockRunner<CriticalSectionLock> runner; |
300 ScopedPtrCollection<Thread> threads; | 299 std::vector<std::unique_ptr<Thread>> threads; |
301 StartThreads(&threads, &runner); | 300 StartThreads(&threads, &runner); |
302 runner.SetExpectedThreadCount(kNumThreads); | 301 runner.SetExpectedThreadCount(kNumThreads); |
303 | 302 |
304 // Release the hounds! | 303 // Release the hounds! |
305 EXPECT_TRUE(runner.Run()); | 304 EXPECT_TRUE(runner.Run()); |
306 EXPECT_EQ(0, runner.shared_value()); | 305 EXPECT_EQ(0, runner.shared_value()); |
307 } | 306 } |
308 | 307 |
309 class PerfTestData { | 308 class PerfTestData { |
310 public: | 309 public: |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 for (auto& t : threads) | 404 for (auto& t : threads) |
406 t.Start(&test_data, kThreadRepeats, 1); | 405 t.Start(&test_data, kThreadRepeats, 1); |
407 | 406 |
408 event.Wait(Event::kForever); | 407 event.Wait(Event::kForever); |
409 | 408 |
410 for (auto& t : threads) | 409 for (auto& t : threads) |
411 t.Stop(); | 410 t.Stop(); |
412 } | 411 } |
413 | 412 |
414 } // namespace rtc | 413 } // namespace rtc |
OLD | NEW |