blob: 653772604cea5932e14026d4aeb4e0b35d8e0fea (
plain) (
blame)
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
|
#include <library/cpp/testing/gtest/gtest.h>
#include <library/cpp/yt/threading/rw_spin_lock.h>
#include <util/thread/pool.h>
#include <latch>
#include <thread>
namespace NYT::NThreading {
namespace {
////////////////////////////////////////////////////////////////////////////////
TEST(TReaderWriterSpinLockTest, WriterPriority)
{
int readerThreads = 10;
std::latch latch(readerThreads + 1);
std::atomic<size_t> finishedCount = {0};
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);
}
////////////////////////////////////////////////////////////////////////////////
} // namespace
} // namespace NYT::NConcurrency
|