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