Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: content/common/gamepad_seqlock_unittest.cc

Issue 10785024: Revert 146792 - Lock-free GamepadSeqLock (try 2) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/common/gamepad_seqlock.cc ('k') | content/content_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/gamepad_seqlock.h" 5 #include "content/common/gamepad_seqlock.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include "base/atomic_ref_count.h" 9 #include "base/atomic_ref_count.h"
10 #include "base/threading/platform_thread.h" 10 #include "base/threading/platform_thread.h"
11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 namespace base { 14 namespace base {
14 15
15 // Basic test to make sure that basic operation works correctly. 16 // Basic test to make sure that basic operation works correctly.
16 17
17 struct TestData { 18 struct TestData {
18 unsigned a, b, c; 19 unsigned a, b, c;
19 }; 20 };
20 21
21 class BasicSeqLockTestThread : public PlatformThread::Delegate { 22 class BasicSeqLockTestThread : public PlatformThread::Delegate {
22 public: 23 public:
23 BasicSeqLockTestThread() {} 24 BasicSeqLockTestThread() {}
24 25
25 void Init( 26 void Init(
26 content::GamepadSeqLock<TestData>* seqlock, 27 content::GamepadSeqLock* seqlock,
28 TestData* data,
27 base::subtle::Atomic32* ready) { 29 base::subtle::Atomic32* ready) {
28 seqlock_ = seqlock; 30 seqlock_ = seqlock;
31 data_ = data;
29 ready_ = ready; 32 ready_ = ready;
30 } 33 }
31 virtual void ThreadMain() { 34 virtual void ThreadMain() {
32 while (AtomicRefCountIsZero(ready_)) { 35 while (AtomicRefCountIsZero(ready_)) {
33 PlatformThread::YieldCurrentThread(); 36 PlatformThread::YieldCurrentThread();
34 } 37 }
35 38
36 for (unsigned i = 0; i < 1000; ++i) { 39 for (unsigned i = 0; i < 1000; ++i) {
37 TestData copy; 40 TestData copy;
38 seqlock_->ReadTo(&copy); 41 base::subtle::Atomic32 version;
42 do {
43 version = seqlock_->ReadBegin();
44 copy = *data_;
45 } while (seqlock_->ReadRetry(version));
46
39 EXPECT_EQ(copy.a + 100, copy.b); 47 EXPECT_EQ(copy.a + 100, copy.b);
40 EXPECT_EQ(copy.c, copy.b + copy.a); 48 EXPECT_EQ(copy.c, copy.b + copy.a);
41 } 49 }
42 50
43 AtomicRefCountDec(ready_); 51 AtomicRefCountDec(ready_);
44 } 52 }
45 53
46 private: 54 private:
47 content::GamepadSeqLock<TestData>* seqlock_; 55 content::GamepadSeqLock* seqlock_;
56 TestData* data_;
48 base::AtomicRefCount* ready_; 57 base::AtomicRefCount* ready_;
49 58
50 DISALLOW_COPY_AND_ASSIGN(BasicSeqLockTestThread); 59 DISALLOW_COPY_AND_ASSIGN(BasicSeqLockTestThread);
51 }; 60 };
52 61
53 TEST(GamepadSeqLockTest, ManyThreads) { 62 TEST(GamepadSeqLockTest, ManyThreads) {
54 content::GamepadSeqLock<TestData> seqlock; 63 content::GamepadSeqLock seqlock;
55 TestData data = { 0, 0, 0 }; 64 TestData data = { 0, 0, 0 };
56 base::AtomicRefCount ready = 0; 65 base::AtomicRefCount ready = 0;
57 66
67 ANNOTATE_BENIGN_RACE_SIZED(&data, sizeof(data), "Racey reads are discarded");
68
58 static const unsigned kNumReaderThreads = 10; 69 static const unsigned kNumReaderThreads = 10;
59 BasicSeqLockTestThread threads[kNumReaderThreads]; 70 BasicSeqLockTestThread threads[kNumReaderThreads];
60 PlatformThreadHandle handles[kNumReaderThreads]; 71 PlatformThreadHandle handles[kNumReaderThreads];
61 72
62 for (unsigned i = 0; i < kNumReaderThreads; ++i) 73 for (unsigned i = 0; i < kNumReaderThreads; ++i)
63 threads[i].Init(&seqlock, &ready); 74 threads[i].Init(&seqlock, &data, &ready);
64 for (unsigned i = 0; i < kNumReaderThreads; ++i) 75 for (unsigned i = 0; i < kNumReaderThreads; ++i)
65 ASSERT_TRUE(PlatformThread::Create(0, &threads[i], &handles[i])); 76 ASSERT_TRUE(PlatformThread::Create(0, &threads[i], &handles[i]));
66 77
67 // The main thread is the writer, and the spawned are readers. 78 // The main thread is the writer, and the spawned are readers.
68 unsigned counter = 0; 79 unsigned counter = 0;
69 for (;;) { 80 for (;;) {
81 seqlock.WriteBegin();
70 data.a = counter++; 82 data.a = counter++;
71 data.b = data.a + 100; 83 data.b = data.a + 100;
72 data.c = data.b + data.a; 84 data.c = data.b + data.a;
73 seqlock.Write(data); 85 seqlock.WriteEnd();
74 86
75 if (counter == 1) 87 if (counter == 1)
76 base::AtomicRefCountIncN(&ready, kNumReaderThreads); 88 base::AtomicRefCountIncN(&ready, kNumReaderThreads);
77 89
78 if (AtomicRefCountIsZero(&ready)) 90 if (AtomicRefCountIsZero(&ready))
79 break; 91 break;
80 } 92 }
81 93
82 for (unsigned i = 0; i < kNumReaderThreads; ++i) 94 for (unsigned i = 0; i < kNumReaderThreads; ++i)
83 PlatformThread::Join(handles[i]); 95 PlatformThread::Join(handles[i]);
84 } 96 }
85 97
86 } // namespace base 98 } // namespace base
OLDNEW
« no previous file with comments | « content/common/gamepad_seqlock.cc ('k') | content/content_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698