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 <set> | 11 #include <set> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "webrtc/base/criticalsection.h" | 14 #include "webrtc/base/criticalsection.h" |
15 #include "webrtc/base/event.h" | 15 #include "webrtc/base/event.h" |
16 #include "webrtc/base/gunit.h" | 16 #include "webrtc/base/gunit.h" |
| 17 #include "webrtc/base/scoped_ptr.h" |
17 #include "webrtc/base/scopedptrcollection.h" | 18 #include "webrtc/base/scopedptrcollection.h" |
18 #include "webrtc/base/thread.h" | 19 #include "webrtc/base/thread.h" |
19 #include "webrtc/test/testsupport/gtest_disable.h" | 20 #include "webrtc/test/testsupport/gtest_disable.h" |
20 | 21 |
21 namespace rtc { | 22 namespace rtc { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 const int kLongTime = 10000; // 10 seconds | 26 const int kLongTime = 10000; // 10 seconds |
26 const int kNumThreads = 16; | 27 const int kNumThreads = 16; |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 EXPECT_EQ(1, AtomicOps::Increment(&value)); | 214 EXPECT_EQ(1, AtomicOps::Increment(&value)); |
214 EXPECT_EQ(1, value); | 215 EXPECT_EQ(1, value); |
215 EXPECT_EQ(2, AtomicOps::Increment(&value)); | 216 EXPECT_EQ(2, AtomicOps::Increment(&value)); |
216 EXPECT_EQ(2, value); | 217 EXPECT_EQ(2, value); |
217 EXPECT_EQ(1, AtomicOps::Decrement(&value)); | 218 EXPECT_EQ(1, AtomicOps::Decrement(&value)); |
218 EXPECT_EQ(1, value); | 219 EXPECT_EQ(1, value); |
219 EXPECT_EQ(0, AtomicOps::Decrement(&value)); | 220 EXPECT_EQ(0, AtomicOps::Decrement(&value)); |
220 EXPECT_EQ(0, value); | 221 EXPECT_EQ(0, value); |
221 } | 222 } |
222 | 223 |
| 224 TEST(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 |
223 TEST(AtomicOpsTest, Increment) { | 246 TEST(AtomicOpsTest, Increment) { |
224 // Create and start lots of threads. | 247 // Create and start lots of threads. |
225 AtomicOpRunner<IncrementOp, UniqueValueVerifier> runner(0); | 248 AtomicOpRunner<IncrementOp, UniqueValueVerifier> runner(0); |
226 ScopedPtrCollection<Thread> threads; | 249 ScopedPtrCollection<Thread> threads; |
227 StartThreads(&threads, &runner); | 250 StartThreads(&threads, &runner); |
228 runner.SetExpectedThreadCount(kNumThreads); | 251 runner.SetExpectedThreadCount(kNumThreads); |
229 | 252 |
230 // Release the hounds! | 253 // Release the hounds! |
231 EXPECT_TRUE(runner.Run()); | 254 EXPECT_TRUE(runner.Run()); |
232 EXPECT_EQ(kOperationsToRun * kNumThreads, runner.shared_value()); | 255 EXPECT_EQ(kOperationsToRun * kNumThreads, runner.shared_value()); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 EXPECT_FALSE(cs.IsLocked()); | 315 EXPECT_FALSE(cs.IsLocked()); |
293 if (!cs.TryEnter()) | 316 if (!cs.TryEnter()) |
294 FAIL(); | 317 FAIL(); |
295 EXPECT_TRUE(cs.IsLocked()); | 318 EXPECT_TRUE(cs.IsLocked()); |
296 cs.Leave(); | 319 cs.Leave(); |
297 EXPECT_FALSE(cs.IsLocked()); | 320 EXPECT_FALSE(cs.IsLocked()); |
298 } | 321 } |
299 #endif | 322 #endif |
300 | 323 |
301 } // namespace rtc | 324 } // namespace rtc |
OLD | NEW |