OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stdlib.h> | |
6 | |
7 #include "content/common/gamepad_seqlock.h" | |
8 #include "base/threading/platform_thread.h" | |
9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace base { | |
13 | |
14 // Basic test to make sure that basic operation works correctly. | |
15 | |
16 struct TestData { | |
17 unsigned a, b, c; | |
18 }; | |
19 | |
20 class BasicSeqLockTestThread : public PlatformThread::Delegate { | |
21 public: | |
22 BasicSeqLockTestThread() {} | |
23 | |
24 void Init( | |
25 gamepad::GamepadSeqLock* seqlock, | |
26 TestData* data, | |
27 base::subtle::Atomic32* ready) { | |
28 seqlock_ = seqlock; | |
29 data_ = data; | |
30 ready_ = ready; | |
31 } | |
32 virtual void ThreadMain() { | |
33 while (base::subtle::Acquire_Load(ready_) == 0) {} | |
34 | |
35 for (unsigned i = 0; i < 10000; ++i) { | |
36 TestData copy; | |
37 base::subtle::Atomic32 version; | |
38 do { | |
39 version = seqlock_->ReadBegin(); | |
40 copy = *data_; | |
41 } while (seqlock_->ReadRetry(version)); | |
42 | |
43 EXPECT_EQ(copy.a + 100, copy.b); | |
44 EXPECT_EQ(copy.c, copy.b + copy.a); | |
45 } | |
46 | |
47 base::subtle::Barrier_AtomicIncrement(ready_, -1); | |
48 } | |
49 | |
50 private: | |
51 gamepad::GamepadSeqLock* seqlock_; | |
52 TestData* data_; | |
53 base::subtle::Atomic32* ready_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(BasicSeqLockTestThread); | |
56 }; | |
57 | |
58 TEST(GamepadSeqLockTest, ManyThreads) { | |
59 gamepad::GamepadSeqLock seqlock; | |
60 TestData data = { 0, 0, 0 }; | |
61 base::subtle::Atomic32 ready = 0; | |
62 | |
63 ANNOTATE_BENIGN_RACE_SIZED(&data, sizeof(data), "Racey reads are discarded"); | |
64 | |
65 static const unsigned kNumReaderThreads = 100; | |
66 BasicSeqLockTestThread threads[kNumReaderThreads]; | |
67 PlatformThreadHandle handles[kNumReaderThreads]; | |
68 | |
69 for (unsigned i = 0; i < kNumReaderThreads; ++i) | |
70 threads[i].Init(&seqlock, &data, &ready); | |
71 for (unsigned i = 0; i < kNumReaderThreads; ++i) | |
72 ASSERT_TRUE(PlatformThread::Create(0, &threads[i], &handles[i])); | |
73 | |
74 // The main thread is the writer, and the spawned are readers. | |
75 unsigned counter = 0; | |
76 for (;;) { | |
77 seqlock.WriteBegin(); | |
78 data.a = counter++; | |
79 data.b = data.a + 100; | |
80 data.c = data.b + data.a; | |
81 seqlock.WriteEnd(); | |
82 | |
83 if (counter == 1) | |
84 base::subtle::Release_Store(&ready, kNumReaderThreads); | |
darin (slow to review)
2011/11/30 18:34:37
Would it be possible to implement |ready| using at
scottmg
2011/11/30 19:43:31
Better, thanks. Done.
| |
85 | |
86 if (base::subtle::Acquire_Load(&ready) == 0) | |
87 break; | |
88 } | |
89 | |
90 for (unsigned i = 0; i < kNumReaderThreads; ++i) | |
91 PlatformThread::Join(handles[i]); | |
92 } | |
93 | |
94 } // namespace base | |
OLD | NEW |