diff options
| author | pavook <[email protected]> | 2025-04-10 15:42:18 +0300 |
|---|---|---|
| committer | pavook <[email protected]> | 2025-04-10 15:57:08 +0300 |
| commit | e4ca0cae001c7a0cc845bca18daff015123a1294 (patch) | |
| tree | 2c97822eea81dca4b8294a10c818101e65acc34f /library/cpp/yt/threading/unittests/rw_spin_lock_ut.cpp | |
| parent | 649e2ccd071e7d216ad38e2dec8f3bee0918a392 (diff) | |
YT-24537: Prioritize writers in TReaderWriterSpinLock
commit_hash:94fee5363799655628bd7e2c144a7869a9d89002
Diffstat (limited to 'library/cpp/yt/threading/unittests/rw_spin_lock_ut.cpp')
| -rw-r--r-- | library/cpp/yt/threading/unittests/rw_spin_lock_ut.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/library/cpp/yt/threading/unittests/rw_spin_lock_ut.cpp b/library/cpp/yt/threading/unittests/rw_spin_lock_ut.cpp new file mode 100644 index 00000000000..653772604ce --- /dev/null +++ b/library/cpp/yt/threading/unittests/rw_spin_lock_ut.cpp @@ -0,0 +1,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 |
