diff options
author | HÃ¥vard Pettersen <3535158+havardpe@users.noreply.github.com> | 2022-02-16 10:23:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-16 10:23:58 +0100 |
commit | 6f99bd502132cd378124a40060ac1d74d54f5e92 (patch) | |
tree | 8abe79c7387eb73c718ed100efba88be4a776a11 /vespalib | |
parent | 2fdad2c948e849adb2074fb207f54de3b9612ebd (diff) | |
parent | 2ed0a06dca5953d3a56b32f89b353f28db5b163e (diff) |
Merge pull request #21209 from vespa-engine/vekterli/well-defined-race-cond-checking-in-spinlock-test
Use atomic refs to make spin lock race condition testing well-defined
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/spin_lock/spin_lock_test.cpp | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/vespalib/src/tests/spin_lock/spin_lock_test.cpp b/vespalib/src/tests/spin_lock/spin_lock_test.cpp index 54ad354f584..847b01247e7 100644 --- a/vespalib/src/tests/spin_lock/spin_lock_test.cpp +++ b/vespalib/src/tests/spin_lock/spin_lock_test.cpp @@ -19,21 +19,30 @@ struct DummyLock { //----------------------------------------------------------------------------- +template <typename T> +constexpr void relaxed_store(T& lhs, T v) noexcept { + std::atomic_ref<T>(lhs).store(v, std::memory_order_relaxed); +} +template <typename T> +constexpr T relaxed_load(const T& a) noexcept { + return std::atomic_ref<const T>(a).load(std::memory_order_relaxed); +} + struct MyState { static constexpr size_t SZ = 5; std::array<size_t,SZ> state = {0,0,0,0,0}; void update() { std::array<size_t,SZ> tmp; for (size_t i = 0; i < SZ; ++i) { - tmp[i] = state[i]; + relaxed_store(tmp[i], relaxed_load(state[i])); } for (size_t i = 0; i < SZ; ++i) { - state[i] = tmp[i] + 1; + relaxed_store(state[i], relaxed_load(tmp[i]) + 1); } } bool check(size_t expect) const { - for (size_t value: state) { - if (value != expect) { + for (const auto& value: state) { + if (relaxed_load(value) != expect) { return false; } } |