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

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

Issue 8689011: Renderer reading side of gamepad (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: review fixes, some renaming Created 9 years 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
OLDNEW
(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 "content/common/gamepad_seqlock.h"
6
7 #include <stdlib.h>
8
9 #include "base/atomic_ref_count.h"
10 #include "base/threading/platform_thread.h"
11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace base {
15
16 // Basic test to make sure that basic operation works correctly.
17
18 struct TestData {
19 unsigned a, b, c;
20 };
21
22 class BasicSeqLockTestThread : public PlatformThread::Delegate {
23 public:
24 BasicSeqLockTestThread() {}
25
26 void Init(
27 content::GamepadSeqLock* seqlock,
28 TestData* data,
29 base::subtle::Atomic32* ready) {
30 seqlock_ = seqlock;
31 data_ = data;
32 ready_ = ready;
33 }
34 virtual void ThreadMain() {
35 while (AtomicRefCountIsZero(ready_)) {}
darin (slow to review) 2011/11/30 21:06:22 Should this call PlatformThread::YieldCurrentThrea
scottmg 2011/11/30 21:22:39 Not necessary to be correct, but no reason to burn
36
37 for (unsigned i = 0; i < 10000; ++i) {
38 TestData copy;
39 base::subtle::Atomic32 version;
40 do {
41 version = seqlock_->ReadBegin();
42 copy = *data_;
43 } while (seqlock_->ReadRetry(version));
44
45 EXPECT_EQ(copy.a + 100, copy.b);
46 EXPECT_EQ(copy.c, copy.b + copy.a);
47 }
48
49 AtomicRefCountDec(ready_);
50 }
51
52 private:
53 content::GamepadSeqLock* seqlock_;
54 TestData* data_;
55 base::AtomicRefCount* ready_;
56
57 DISALLOW_COPY_AND_ASSIGN(BasicSeqLockTestThread);
58 };
59
60 TEST(GamepadSeqLockTest, ManyThreads) {
61 content::GamepadSeqLock seqlock;
62 TestData data = { 0, 0, 0 };
63 base::AtomicRefCount ready = 0;
64
65 ANNOTATE_BENIGN_RACE_SIZED(&data, sizeof(data), "Racey reads are discarded");
66
67 static const unsigned kNumReaderThreads = 100;
68 BasicSeqLockTestThread threads[kNumReaderThreads];
69 PlatformThreadHandle handles[kNumReaderThreads];
70
71 for (unsigned i = 0; i < kNumReaderThreads; ++i)
72 threads[i].Init(&seqlock, &data, &ready);
73 for (unsigned i = 0; i < kNumReaderThreads; ++i)
74 ASSERT_TRUE(PlatformThread::Create(0, &threads[i], &handles[i]));
75
76 // The main thread is the writer, and the spawned are readers.
77 unsigned counter = 0;
78 for (;;) {
79 seqlock.WriteBegin();
80 data.a = counter++;
81 data.b = data.a + 100;
82 data.c = data.b + data.a;
83 seqlock.WriteEnd();
84
85 if (counter == 1)
86 base::AtomicRefCountIncN(&ready, kNumReaderThreads);
87
88 if (AtomicRefCountIsZero(&ready))
89 break;
90 }
91
92 for (unsigned i = 0; i < kNumReaderThreads; ++i)
93 PlatformThread::Join(handles[i]);
94 }
95
96 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698