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