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/one_writer_seqlock.h" | 5 #include "device/generic_sensor/one_writer_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/macros.h" | 10 #include "base/macros.h" |
11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 | 17 |
18 // Basic test to make sure that basic operation works correctly. | 18 // Basic test to make sure that basic operation works correctly. |
19 | 19 |
20 struct TestData { | 20 struct TestData { |
21 unsigned a, b, c; | 21 unsigned a, b, c; |
22 }; | 22 }; |
23 | 23 |
24 class BasicSeqLockTestThread : public PlatformThread::Delegate { | 24 class BasicSeqLockTestThread : public PlatformThread::Delegate { |
25 public: | 25 public: |
26 BasicSeqLockTestThread() {} | 26 BasicSeqLockTestThread() {} |
27 | 27 |
28 void Init( | 28 void Init(device::OneWriterSeqLock* seqlock, |
29 content::OneWriterSeqLock* seqlock, | 29 TestData* data, |
30 TestData* data, | 30 base::subtle::Atomic32* ready) { |
31 base::subtle::Atomic32* ready) { | |
32 seqlock_ = seqlock; | 31 seqlock_ = seqlock; |
33 data_ = data; | 32 data_ = data; |
34 ready_ = ready; | 33 ready_ = ready; |
35 } | 34 } |
36 void ThreadMain() override { | 35 void ThreadMain() override { |
37 while (AtomicRefCountIsZero(ready_)) { | 36 while (AtomicRefCountIsZero(ready_)) { |
38 PlatformThread::YieldCurrentThread(); | 37 PlatformThread::YieldCurrentThread(); |
39 } | 38 } |
40 | 39 |
41 for (unsigned i = 0; i < 1000; ++i) { | 40 for (unsigned i = 0; i < 1000; ++i) { |
42 TestData copy; | 41 TestData copy; |
43 base::subtle::Atomic32 version; | 42 base::subtle::Atomic32 version; |
44 do { | 43 do { |
45 version = seqlock_->ReadBegin(); | 44 version = seqlock_->ReadBegin(); |
46 copy = *data_; | 45 copy = *data_; |
47 } while (seqlock_->ReadRetry(version)); | 46 } while (seqlock_->ReadRetry(version)); |
48 | 47 |
49 EXPECT_EQ(copy.a + 100, copy.b); | 48 EXPECT_EQ(copy.a + 100, copy.b); |
50 EXPECT_EQ(copy.c, copy.b + copy.a); | 49 EXPECT_EQ(copy.c, copy.b + copy.a); |
51 } | 50 } |
52 | 51 |
53 AtomicRefCountDec(ready_); | 52 AtomicRefCountDec(ready_); |
54 } | 53 } |
55 | 54 |
56 private: | 55 private: |
57 content::OneWriterSeqLock* seqlock_; | 56 device::OneWriterSeqLock* seqlock_; |
58 TestData* data_; | 57 TestData* data_; |
59 base::AtomicRefCount* ready_; | 58 base::AtomicRefCount* ready_; |
60 | 59 |
61 DISALLOW_COPY_AND_ASSIGN(BasicSeqLockTestThread); | 60 DISALLOW_COPY_AND_ASSIGN(BasicSeqLockTestThread); |
62 }; | 61 }; |
63 | 62 |
64 #if defined(OS_ANDROID) | 63 #if defined(OS_ANDROID) |
65 #define MAYBE_ManyThreads FLAKY_ManyThreads | 64 #define MAYBE_ManyThreads FLAKY_ManyThreads |
66 #else | 65 #else |
67 #define MAYBE_ManyThreads ManyThreads | 66 #define MAYBE_ManyThreads ManyThreads |
68 #endif | 67 #endif |
69 TEST(OneWriterSeqLockTest, MAYBE_ManyThreads) { | 68 TEST(OneWriterSeqLockTest, MAYBE_ManyThreads) { |
70 content::OneWriterSeqLock seqlock; | 69 device::OneWriterSeqLock seqlock; |
71 TestData data = { 0, 0, 0 }; | 70 TestData data = {0, 0, 0}; |
72 base::AtomicRefCount ready = 0; | 71 base::AtomicRefCount ready = 0; |
73 | 72 |
74 ANNOTATE_BENIGN_RACE_SIZED(&data, sizeof(data), "Racey reads are discarded"); | 73 ANNOTATE_BENIGN_RACE_SIZED(&data, sizeof(data), "Racey reads are discarded"); |
75 | 74 |
76 static const unsigned kNumReaderThreads = 10; | 75 static const unsigned kNumReaderThreads = 10; |
77 BasicSeqLockTestThread threads[kNumReaderThreads]; | 76 BasicSeqLockTestThread threads[kNumReaderThreads]; |
78 PlatformThreadHandle handles[kNumReaderThreads]; | 77 PlatformThreadHandle handles[kNumReaderThreads]; |
79 | 78 |
80 for (unsigned i = 0; i < kNumReaderThreads; ++i) | 79 for (unsigned i = 0; i < kNumReaderThreads; ++i) |
81 threads[i].Init(&seqlock, &data, &ready); | 80 threads[i].Init(&seqlock, &data, &ready); |
(...skipping 14 matching lines...) Expand all Loading... |
96 | 95 |
97 if (AtomicRefCountIsZero(&ready)) | 96 if (AtomicRefCountIsZero(&ready)) |
98 break; | 97 break; |
99 } | 98 } |
100 | 99 |
101 for (unsigned i = 0; i < kNumReaderThreads; ++i) | 100 for (unsigned i = 0; i < kNumReaderThreads; ++i) |
102 PlatformThread::Join(handles[i]); | 101 PlatformThread::Join(handles[i]); |
103 } | 102 } |
104 | 103 |
105 } // namespace base | 104 } // namespace base |
OLD | NEW |