| OLD | NEW |
| 1 // Copyright (c) 2011 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" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 base::subtle::Atomic32* ready) { | 29 base::subtle::Atomic32* ready) { |
| 30 seqlock_ = seqlock; | 30 seqlock_ = seqlock; |
| 31 data_ = data; | 31 data_ = data; |
| 32 ready_ = ready; | 32 ready_ = ready; |
| 33 } | 33 } |
| 34 virtual void ThreadMain() { | 34 virtual void ThreadMain() { |
| 35 while (AtomicRefCountIsZero(ready_)) { | 35 while (AtomicRefCountIsZero(ready_)) { |
| 36 PlatformThread::YieldCurrentThread(); | 36 PlatformThread::YieldCurrentThread(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 for (unsigned i = 0; i < 10000; ++i) { | 39 for (unsigned i = 0; i < 1000; ++i) { |
| 40 TestData copy; | 40 TestData copy; |
| 41 base::subtle::Atomic32 version; | 41 base::subtle::Atomic32 version; |
| 42 do { | 42 do { |
| 43 version = seqlock_->ReadBegin(); | 43 version = seqlock_->ReadBegin(); |
| 44 copy = *data_; | 44 copy = *data_; |
| 45 } while (seqlock_->ReadRetry(version)); | 45 } while (seqlock_->ReadRetry(version)); |
| 46 | 46 |
| 47 EXPECT_EQ(copy.a + 100, copy.b); | 47 EXPECT_EQ(copy.a + 100, copy.b); |
| 48 EXPECT_EQ(copy.c, copy.b + copy.a); | 48 EXPECT_EQ(copy.c, copy.b + copy.a); |
| 49 } | 49 } |
| 50 | 50 |
| 51 AtomicRefCountDec(ready_); | 51 AtomicRefCountDec(ready_); |
| 52 } | 52 } |
| 53 | 53 |
| 54 private: | 54 private: |
| 55 content::GamepadSeqLock* seqlock_; | 55 content::GamepadSeqLock* seqlock_; |
| 56 TestData* data_; | 56 TestData* data_; |
| 57 base::AtomicRefCount* ready_; | 57 base::AtomicRefCount* ready_; |
| 58 | 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(BasicSeqLockTestThread); | 59 DISALLOW_COPY_AND_ASSIGN(BasicSeqLockTestThread); |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 TEST(GamepadSeqLockTest, ManyThreads) { | 62 TEST(GamepadSeqLockTest, ManyThreads) { |
| 63 content::GamepadSeqLock seqlock; | 63 content::GamepadSeqLock seqlock; |
| 64 TestData data = { 0, 0, 0 }; | 64 TestData data = { 0, 0, 0 }; |
| 65 base::AtomicRefCount ready = 0; | 65 base::AtomicRefCount ready = 0; |
| 66 | 66 |
| 67 ANNOTATE_BENIGN_RACE_SIZED(&data, sizeof(data), "Racey reads are discarded"); | 67 ANNOTATE_BENIGN_RACE_SIZED(&data, sizeof(data), "Racey reads are discarded"); |
| 68 | 68 |
| 69 static const unsigned kNumReaderThreads = 100; | 69 static const unsigned kNumReaderThreads = 10; |
| 70 BasicSeqLockTestThread threads[kNumReaderThreads]; | 70 BasicSeqLockTestThread threads[kNumReaderThreads]; |
| 71 PlatformThreadHandle handles[kNumReaderThreads]; | 71 PlatformThreadHandle handles[kNumReaderThreads]; |
| 72 | 72 |
| 73 for (unsigned i = 0; i < kNumReaderThreads; ++i) | 73 for (unsigned i = 0; i < kNumReaderThreads; ++i) |
| 74 threads[i].Init(&seqlock, &data, &ready); | 74 threads[i].Init(&seqlock, &data, &ready); |
| 75 for (unsigned i = 0; i < kNumReaderThreads; ++i) | 75 for (unsigned i = 0; i < kNumReaderThreads; ++i) |
| 76 ASSERT_TRUE(PlatformThread::Create(0, &threads[i], &handles[i])); | 76 ASSERT_TRUE(PlatformThread::Create(0, &threads[i], &handles[i])); |
| 77 | 77 |
| 78 // The main thread is the writer, and the spawned are readers. | 78 // The main thread is the writer, and the spawned are readers. |
| 79 unsigned counter = 0; | 79 unsigned counter = 0; |
| 80 for (;;) { | 80 for (;;) { |
| 81 seqlock.WriteBegin(); | 81 seqlock.WriteBegin(); |
| 82 data.a = counter++; | 82 data.a = counter++; |
| 83 data.b = data.a + 100; | 83 data.b = data.a + 100; |
| 84 data.c = data.b + data.a; | 84 data.c = data.b + data.a; |
| 85 seqlock.WriteEnd(); | 85 seqlock.WriteEnd(); |
| 86 | 86 |
| 87 if (counter == 1) | 87 if (counter == 1) |
| 88 base::AtomicRefCountIncN(&ready, kNumReaderThreads); | 88 base::AtomicRefCountIncN(&ready, kNumReaderThreads); |
| 89 | 89 |
| 90 if (AtomicRefCountIsZero(&ready)) | 90 if (AtomicRefCountIsZero(&ready)) |
| 91 break; | 91 break; |
| 92 } | 92 } |
| 93 | 93 |
| 94 for (unsigned i = 0; i < kNumReaderThreads; ++i) | 94 for (unsigned i = 0; i < kNumReaderThreads; ++i) |
| 95 PlatformThread::Join(handles[i]); | 95 PlatformThread::Join(handles[i]); |
| 96 } | 96 } |
| 97 | 97 |
| 98 } // namespace base | 98 } // namespace base |
| OLD | NEW |