1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/base/synchronization/one_writer_seqlock.h"
#include <stdlib.h>
#include <array>
#include <atomic>
#include "base/memory/raw_ptr.h"
#include "base/threading/platform_thread.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/base/dynamic_annotations.h"
namespace device {
// Basic test to make sure that basic operation works correctly.
struct TestData {
// Data copies larger than a cache line.
std::array<uint32_t, 32> buffer;
};
class BasicSeqLockTestThread : public base::PlatformThread::Delegate {
public:
BasicSeqLockTestThread() = default;
BasicSeqLockTestThread(const BasicSeqLockTestThread&) = delete;
BasicSeqLockTestThread& operator=(const BasicSeqLockTestThread&) = delete;
void Init(OneWriterSeqLock* seqlock,
TestData* data,
std::atomic<int>* ready) {
seqlock_ = seqlock;
data_ = data;
ready_ = ready;
}
void ThreadMain() override {
while (!*ready_) {
base::PlatformThread::YieldCurrentThread();
}
for (unsigned i = 0; i < 1000; ++i) {
TestData copy;
base::subtle::Atomic32 version;
do {
version = seqlock_->ReadBegin();
OneWriterSeqLock::AtomicReaderMemcpy(©, data_.get(),
sizeof(TestData));
} while (seqlock_->ReadRetry(version));
for (unsigned j = 1; j < 32; ++j)
EXPECT_EQ(copy.buffer[j], copy.buffer[0] + copy.buffer[j - 1]);
}
--(*ready_);
}
private:
raw_ptr<OneWriterSeqLock> seqlock_;
raw_ptr<TestData> data_;
raw_ptr<std::atomic<int>> ready_;
};
class MaxRetriesSeqLockTestThread : public base::PlatformThread::Delegate {
public:
MaxRetriesSeqLockTestThread() = default;
MaxRetriesSeqLockTestThread(const MaxRetriesSeqLockTestThread&) = delete;
MaxRetriesSeqLockTestThread& operator=(const MaxRetriesSeqLockTestThread&) =
delete;
void Init(OneWriterSeqLock* seqlock, std::atomic<int>* ready) {
seqlock_ = seqlock;
ready_ = ready;
}
void ThreadMain() override {
while (!*ready_) {
base::PlatformThread::YieldCurrentThread();
}
for (unsigned i = 0; i < 10; ++i) {
base::subtle::Atomic32 version;
version = seqlock_->ReadBegin(100);
EXPECT_NE(version & 1, 0);
}
--*ready_;
}
private:
raw_ptr<OneWriterSeqLock> seqlock_;
raw_ptr<std::atomic<int>> ready_;
};
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_ManyThreads FLAKY_ManyThreads
#else
#define MAYBE_ManyThreads ManyThreads
#endif
TEST(OneWriterSeqLockTest, MAYBE_ManyThreads) {
OneWriterSeqLock seqlock;
TestData data;
std::atomic<int> ready(0);
ABSL_ANNOTATE_BENIGN_RACE_SIZED(&data, sizeof(data), "Racey reads are discarded");
static const unsigned kNumReaderThreads = 10;
std::array<BasicSeqLockTestThread, kNumReaderThreads> threads;
std::array<base::PlatformThreadHandle, kNumReaderThreads> handles;
for (uint32_t i = 0; i < kNumReaderThreads; ++i)
threads[i].Init(&seqlock, &data, &ready);
for (uint32_t i = 0; i < kNumReaderThreads; ++i)
ASSERT_TRUE(base::PlatformThread::Create(0, &threads[i], &handles[i]));
// The main thread is the writer, and the spawned are readers.
uint32_t counter = 0;
for (;;) {
TestData new_data;
new_data.buffer[0] = counter++;
for (unsigned i = 1; i < 32; ++i) {
new_data.buffer[i] = new_data.buffer[0] + new_data.buffer[i - 1];
}
seqlock.WriteBegin();
OneWriterSeqLock::AtomicWriterMemcpy(&data, &new_data, sizeof(TestData));
seqlock.WriteEnd();
if (counter == 1)
ready += kNumReaderThreads;
if (!ready)
break;
}
for (unsigned i = 0; i < kNumReaderThreads; ++i)
base::PlatformThread::Join(handles[i]);
}
TEST(OneWriterSeqLockTest, MaxRetries) {
OneWriterSeqLock seqlock;
std::atomic<int> ready(0);
static const unsigned kNumReaderThreads = 3;
std::array<MaxRetriesSeqLockTestThread, kNumReaderThreads> threads;
std::array<base::PlatformThreadHandle, kNumReaderThreads> handles;
for (uint32_t i = 0; i < kNumReaderThreads; ++i)
threads[i].Init(&seqlock, &ready);
for (uint32_t i = 0; i < kNumReaderThreads; ++i)
ASSERT_TRUE(base::PlatformThread::Create(0, &threads[i], &handles[i]));
// The main thread is the writer, and the spawned are readers.
seqlock.WriteBegin();
ready += kNumReaderThreads;
while (ready) {
base::PlatformThread::YieldCurrentThread();
}
seqlock.WriteEnd();
for (unsigned i = 0; i < kNumReaderThreads; ++i)
base::PlatformThread::Join(handles[i]);
}
} // namespace device
|