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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/synchronization/read_write_lock.h"
#include <stdlib.h>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/platform_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace subtle {
// Basic test to make sure that *Acquire()/*Release() don't crash.
class BasicReadWriteLockTestThread : public PlatformThread::Delegate {
public:
explicit BasicReadWriteLockTestThread(ReadWriteLock* lock)
: lock_(lock), acquired_(0) {}
void ThreadMain() override {
for (int i = 0; i < 10; i++) {
AutoReadLock locker(*lock_);
acquired_++;
}
for (int i = 0; i < 10; i++) {
AutoWriteLock locker(*lock_);
acquired_++;
PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20));
}
}
int acquired() const { return acquired_; }
private:
ReadWriteLock* lock_;
int acquired_;
DISALLOW_COPY_AND_ASSIGN(BasicReadWriteLockTestThread);
};
TEST(ReadWriteLockTest, Basic) {
ReadWriteLock lock;
BasicReadWriteLockTestThread thread(&lock);
PlatformThreadHandle handle;
ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
int acquired = 0;
for (int i = 0; i < 5; i++) {
AutoReadLock locker(lock);
acquired++;
}
for (int i = 0; i < 10; i++) {
AutoWriteLock locker(lock);
acquired++;
PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20));
}
for (int i = 0; i < 5; i++) {
AutoReadLock locker(lock);
acquired++;
}
PlatformThread::Join(handle);
EXPECT_EQ(20, acquired);
EXPECT_GE(20, thread.acquired());
}
// Tests that reader locks allow multiple simultaneous reader acquisitions.
class ReaderReadWriteLockTestThread : public PlatformThread::Delegate {
public:
ReaderReadWriteLockTestThread(ReadWriteLock* lock) : lock_(lock) {}
void ThreadMain() override {
AutoReadLock locker(*lock_);
did_acquire_ = true;
}
bool did_acquire() const { return did_acquire_; }
private:
ReadWriteLock* lock_;
bool did_acquire_ = false;
DISALLOW_COPY_AND_ASSIGN(ReaderReadWriteLockTestThread);
};
TEST(ReadWriteLockTest, ReaderTwoThreads) {
ReadWriteLock lock;
AutoReadLock auto_lock(lock);
ReaderReadWriteLockTestThread thread(&lock);
PlatformThreadHandle handle;
ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
PlatformThread::Join(handle);
EXPECT_TRUE(thread.did_acquire());
}
// Tests that writer locks exclude reader locks.
class ReadAndWriteReadWriteLockTestThread : public PlatformThread::Delegate {
public:
ReadAndWriteReadWriteLockTestThread(ReadWriteLock* lock, int* value)
: lock_(lock),
value_(value),
event_(WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED) {}
void ThreadMain() override {
AutoWriteLock locker(*lock_);
(*value_)++;
event_.Signal();
}
void Wait() {
event_.Wait();
}
private:
ReadWriteLock* lock_;
int* value_;
WaitableEvent event_;
DISALLOW_COPY_AND_ASSIGN(ReadAndWriteReadWriteLockTestThread);
};
TEST(ReadWriteLockTest, ReadAndWriteThreads) {
ReadWriteLock lock;
int value = 0;
ReadAndWriteReadWriteLockTestThread thread(&lock, &value);
PlatformThreadHandle handle;
{
AutoReadLock read_locker(lock);
ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
PlatformThread::Sleep(TimeDelta::FromMilliseconds(10));
// |value| should be unchanged since we hold a reader lock.
EXPECT_EQ(0, value);
}
thread.Wait();
// After releasing our reader lock, the thread can acquire a write lock and
// change |value|.
EXPECT_EQ(1, value);
PlatformThread::Join(handle);
}
// Tests that writer locks actually exclude.
class WriterReadWriteLockTestThread : public PlatformThread::Delegate {
public:
WriterReadWriteLockTestThread(ReadWriteLock* lock, int* value)
: lock_(lock), value_(value) {}
// Static helper which can also be called from the main thread.
static void DoStuff(ReadWriteLock* lock, int* value) {
for (int i = 0; i < 40; i++) {
AutoWriteLock locker(*lock);
int v = *value;
PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 10));
*value = v + 1;
}
}
void ThreadMain() override { DoStuff(lock_, value_); }
private:
ReadWriteLock* lock_;
int* value_;
DISALLOW_COPY_AND_ASSIGN(WriterReadWriteLockTestThread);
};
TEST(ReadWriteLockTest, MutexTwoThreads) {
ReadWriteLock lock;
int value = 0;
WriterReadWriteLockTestThread thread(&lock, &value);
PlatformThreadHandle handle;
ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
WriterReadWriteLockTestThread::DoStuff(&lock, &value);
PlatformThread::Join(handle);
EXPECT_EQ(2 * 40, value);
}
TEST(ReadWriteLockTest, MutexFourThreads) {
ReadWriteLock lock;
int value = 0;
WriterReadWriteLockTestThread thread1(&lock, &value);
WriterReadWriteLockTestThread thread2(&lock, &value);
WriterReadWriteLockTestThread thread3(&lock, &value);
PlatformThreadHandle handle1;
PlatformThreadHandle handle2;
PlatformThreadHandle handle3;
ASSERT_TRUE(PlatformThread::Create(0, &thread1, &handle1));
ASSERT_TRUE(PlatformThread::Create(0, &thread2, &handle2));
ASSERT_TRUE(PlatformThread::Create(0, &thread3, &handle3));
WriterReadWriteLockTestThread::DoStuff(&lock, &value);
PlatformThread::Join(handle1);
PlatformThread::Join(handle2);
PlatformThread::Join(handle3);
EXPECT_EQ(4 * 40, value);
}
} // namespace subtle
} // namespace base
|