summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/util
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@online.no>2022-02-22 16:11:41 +0100
committerTor Egge <Tor.Egge@online.no>2022-02-22 16:12:19 +0100
commit9ede962d03ef929c2aca691eae281a9f65d177d4 (patch)
tree3c6b50fe09bf4d1a88d14b6f9f2da0d94e9d7852 /vespalib/src/tests/util
parentf859f58e988c61b0c16f4ed4eb9aaa5daca1661d (diff)
Fix race conditions in vespalib::GenerationHandler discovered by thread sanitizer.
Diffstat (limited to 'vespalib/src/tests/util')
-rw-r--r--vespalib/src/tests/util/generationhandler/generationhandler_test.cpp8
-rw-r--r--vespalib/src/tests/util/generationhandler_stress/generation_handler_stress_test.cpp6
2 files changed, 7 insertions, 7 deletions
diff --git a/vespalib/src/tests/util/generationhandler/generationhandler_test.cpp b/vespalib/src/tests/util/generationhandler/generationhandler_test.cpp
index 07805a6942c..f269fe729fa 100644
--- a/vespalib/src/tests/util/generationhandler/generationhandler_test.cpp
+++ b/vespalib/src/tests/util/generationhandler/generationhandler_test.cpp
@@ -97,18 +97,18 @@ Test::requireThatTheFirstUsedGenerationIsCorrect()
{
GenGuard g1 = gh.takeGuard();
gh.incGeneration();
- EXPECT_EQUAL(true, gh.hasReaders());
+ EXPECT_EQUAL(1u, gh.getGenerationRefCount());
EXPECT_EQUAL(1u, gh.getFirstUsedGeneration());
}
EXPECT_EQUAL(1u, gh.getFirstUsedGeneration());
gh.updateFirstUsedGeneration(); // Only writer should call this
- EXPECT_EQUAL(false, gh.hasReaders());
+ EXPECT_EQUAL(0u, gh.getGenerationRefCount());
EXPECT_EQUAL(2u, gh.getFirstUsedGeneration());
{
GenGuard g1 = gh.takeGuard();
gh.incGeneration();
gh.incGeneration();
- EXPECT_EQUAL(true, gh.hasReaders());
+ EXPECT_EQUAL(1u, gh.getGenerationRefCount());
EXPECT_EQUAL(2u, gh.getFirstUsedGeneration());
{
GenGuard g2 = gh.takeGuard();
@@ -117,7 +117,7 @@ Test::requireThatTheFirstUsedGenerationIsCorrect()
}
EXPECT_EQUAL(2u, gh.getFirstUsedGeneration());
gh.updateFirstUsedGeneration(); // Only writer should call this
- EXPECT_EQUAL(false, gh.hasReaders());
+ EXPECT_EQUAL(0u, gh.getGenerationRefCount());
EXPECT_EQUAL(4u, gh.getFirstUsedGeneration());
}
diff --git a/vespalib/src/tests/util/generationhandler_stress/generation_handler_stress_test.cpp b/vespalib/src/tests/util/generationhandler_stress/generation_handler_stress_test.cpp
index 8cea96e0f68..fa2c525b518 100644
--- a/vespalib/src/tests/util/generationhandler_stress/generation_handler_stress_test.cpp
+++ b/vespalib/src/tests/util/generationhandler_stress/generation_handler_stress_test.cpp
@@ -13,7 +13,7 @@ using vespalib::ThreadStackExecutor;
struct WorkContext
{
- uint64_t _generation;
+ std::atomic<uint64_t> _generation;
WorkContext() noexcept
: _generation(0)
@@ -84,7 +84,7 @@ Fixture::readWork(const WorkContext &context)
for (i = 0; i < cnt && _stopRead.load() == 0; ++i) {
auto guard = _generationHandler.takeGuard();
- auto generation = context._generation;
+ auto generation = context._generation.load(std::memory_order_relaxed);
EXPECT_GREATER_EQUAL(generation, guard.getGeneration());
}
_doneReadWork += i;
@@ -96,7 +96,7 @@ void
Fixture::writeWork(uint32_t cnt, WorkContext &context)
{
for (uint32_t i = 0; i < cnt; ++i) {
- context._generation = _generationHandler.getNextGeneration();
+ context._generation.store(_generationHandler.getNextGeneration(), std::memory_order_relaxed);
_generationHandler.incGeneration();
}
_doneWriteWork += cnt;