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
|
#include <library/cpp/testing/gtest/gtest.h>
#include <library/cpp/yt/threading/rw_spin_lock.h>
#include <util/thread/pool.h>
#include <latch>
namespace NYT::NThreading {
namespace {
////////////////////////////////////////////////////////////////////////////////
TEST(TReaderWriterSpinLockTest, WriterPriority)
{
int readerThreads = 10;
std::latch latch(readerThreads + 1);
std::atomic<size_t> finishedCount = {0};
YT_DECLARE_SPIN_LOCK(TReaderWriterSpinLock, lock);
volatile std::atomic<ui32> x = {0};
auto readerTask = [&latch, &lock, &finishedCount, &x] () {
latch.arrive_and_wait();
while (true) {
{
auto guard = ReaderGuard(lock);
// do some stuff
for (ui32 i = 0; i < 10'000u; ++i) {
x.fetch_add(i);
}
}
if (finishedCount.fetch_add(1) > 20'000) {
break;
}
}
};
auto readerPool = CreateThreadPool(readerThreads);
for (int i = 0; i < readerThreads; ++i) {
readerPool->SafeAddFunc(readerTask);
}
latch.arrive_and_wait();
while (finishedCount.load() == 0);
auto guard = WriterGuard(lock);
EXPECT_LE(finishedCount.load(), 1'000u);
DoNotOptimizeAway(x);
}
TEST(TReaderWriterSpinLockDeathTest, ReaderReentrance)
{
YT_DECLARE_SPIN_LOCK(TReaderWriterSpinLock, lock);
EXPECT_DEBUG_DEATH({
auto guard1 = ReaderGuard(lock);
auto guard2 = ReaderGuard(lock);
}, "two acquisitions in one thread");
}
TEST(TReaderWriterSpinLockDeathTest, MixedReentrance)
{
YT_DECLARE_SPIN_LOCK(NDetail::TCheckedReaderWriterSpinLock, lock);
EXPECT_DEATH({
auto guard1 = ReaderGuard(lock);
auto guard2 = WriterGuard(lock);
}, "two acquisitions in one thread");
}
TEST(TReaderWriterSpinLockDeathTest, TryReaderReentrance)
{
YT_DECLARE_SPIN_LOCK(TReaderWriterSpinLock, lock);
EXPECT_DEBUG_DEATH({
auto guard = ReaderGuard(lock);
lock.TryAcquireReader();
}, "two acquisitions in one thread");
}
TEST(TReaderWriterSpinLockDeathTest, TryWriterReentrance)
{
YT_DECLARE_SPIN_LOCK(TReaderWriterSpinLock, lock);
EXPECT_DEBUG_DEATH({
auto guard = WriterGuard(lock);
lock.TryAcquireWriter();
}, "two acquisitions in one thread");
}
TEST(TReaderWriterSpinLockDeathTest, ReleaseUnacquiredLock)
{
YT_DECLARE_SPIN_LOCK(TReaderWriterSpinLock, lock);
EXPECT_DEBUG_DEATH({
lock.ReleaseReader();
}, "has never been acquired");
}
////////////////////////////////////////////////////////////////////////////////
} // namespace
} // namespace NYT::NThreading
|