blob: 7e5da3f81466731fc76aa29ab53fee352796074b [file] [log] [blame]
[email protected]501f9682012-07-16 21:45:271// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]4324e612011-12-01 00:01:382// 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"
[email protected]501f9682012-07-16 21:45:2711#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
[email protected]4324e612011-12-01 00:01:3812#include "testing/gtest/include/gtest/gtest.h"
13
14namespace base {
15
16// Basic test to make sure that basic operation works correctly.
17
18struct TestData {
19 unsigned a, b, c;
20};
21
22class BasicSeqLockTestThread : public PlatformThread::Delegate {
23 public:
24 BasicSeqLockTestThread() {}
25
26 void Init(
[email protected]501f9682012-07-16 21:45:2727 content::GamepadSeqLock* seqlock,
28 TestData* data,
[email protected]4324e612011-12-01 00:01:3829 base::subtle::Atomic32* ready) {
30 seqlock_ = seqlock;
[email protected]501f9682012-07-16 21:45:2731 data_ = data;
[email protected]4324e612011-12-01 00:01:3832 ready_ = ready;
33 }
34 virtual void ThreadMain() {
35 while (AtomicRefCountIsZero(ready_)) {
36 PlatformThread::YieldCurrentThread();
37 }
38
[email protected]f7b06a52011-12-01 04:29:5539 for (unsigned i = 0; i < 1000; ++i) {
[email protected]4324e612011-12-01 00:01:3840 TestData copy;
[email protected]501f9682012-07-16 21:45:2741 base::subtle::Atomic32 version;
42 do {
43 version = seqlock_->ReadBegin();
44 copy = *data_;
45 } while (seqlock_->ReadRetry(version));
46
[email protected]4324e612011-12-01 00:01:3847 EXPECT_EQ(copy.a + 100, copy.b);
48 EXPECT_EQ(copy.c, copy.b + copy.a);
49 }
50
51 AtomicRefCountDec(ready_);
52 }
53
54 private:
[email protected]501f9682012-07-16 21:45:2755 content::GamepadSeqLock* seqlock_;
56 TestData* data_;
[email protected]4324e612011-12-01 00:01:3857 base::AtomicRefCount* ready_;
58
59 DISALLOW_COPY_AND_ASSIGN(BasicSeqLockTestThread);
60};
61
62TEST(GamepadSeqLockTest, ManyThreads) {
[email protected]501f9682012-07-16 21:45:2763 content::GamepadSeqLock seqlock;
[email protected]4324e612011-12-01 00:01:3864 TestData data = { 0, 0, 0 };
65 base::AtomicRefCount ready = 0;
66
[email protected]501f9682012-07-16 21:45:2767 ANNOTATE_BENIGN_RACE_SIZED(&data, sizeof(data), "Racey reads are discarded");
68
[email protected]f7b06a52011-12-01 04:29:5569 static const unsigned kNumReaderThreads = 10;
[email protected]4324e612011-12-01 00:01:3870 BasicSeqLockTestThread threads[kNumReaderThreads];
71 PlatformThreadHandle handles[kNumReaderThreads];
72
73 for (unsigned i = 0; i < kNumReaderThreads; ++i)
[email protected]501f9682012-07-16 21:45:2774 threads[i].Init(&seqlock, &data, &ready);
[email protected]4324e612011-12-01 00:01:3875 for (unsigned i = 0; i < kNumReaderThreads; ++i)
76 ASSERT_TRUE(PlatformThread::Create(0, &threads[i], &handles[i]));
77
78 // The main thread is the writer, and the spawned are readers.
79 unsigned counter = 0;
80 for (;;) {
[email protected]501f9682012-07-16 21:45:2781 seqlock.WriteBegin();
[email protected]4324e612011-12-01 00:01:3882 data.a = counter++;
83 data.b = data.a + 100;
84 data.c = data.b + data.a;
[email protected]501f9682012-07-16 21:45:2785 seqlock.WriteEnd();
[email protected]4324e612011-12-01 00:01:3886
87 if (counter == 1)
88 base::AtomicRefCountIncN(&ready, kNumReaderThreads);
89
90 if (AtomicRefCountIsZero(&ready))
91 break;
92 }
93
94 for (unsigned i = 0; i < kNumReaderThreads; ++i)
95 PlatformThread::Join(handles[i]);
96}
97
98} // namespace base