From 56e16fcfd01ea6e3be4f4b3ad9daf832c1e9e51d Mon Sep 17 00:00:00 2001 From: FritzFlorian Date: Wed, 20 Mar 2019 17:16:50 +0100 Subject: [PATCH] Fix: Spinlock test triggers thread sanitizer. --- .gitlab-ci.yml | 2 +- lib/pls/src/internal/base/spin_lock.cpp | 2 ++ test/base_tests.cpp | 18 ++++++++++-------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4754e0e..95f4b16 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,4 +1,4 @@ -image: "matrim/cmake-examples:3.10.3" +image: "flofritz/ci-benchmark-hpx-embb" stages: - build diff --git a/lib/pls/src/internal/base/spin_lock.cpp b/lib/pls/src/internal/base/spin_lock.cpp index 10f6978..99a2c1d 100644 --- a/lib/pls/src/internal/base/spin_lock.cpp +++ b/lib/pls/src/internal/base/spin_lock.cpp @@ -11,10 +11,12 @@ namespace pls { this_thread::yield(); } } + std::atomic_thread_fence(std::memory_order_seq_cst); } void spin_lock::unlock() { flag_.clear(std::memory_order_release); + std::atomic_thread_fence(std::memory_order_seq_cst); } } } diff --git a/test/base_tests.cpp b/test/base_tests.cpp index 7c7b990..d64614a 100644 --- a/test/base_tests.cpp +++ b/test/base_tests.cpp @@ -33,23 +33,25 @@ TEST_CASE( "thread state", "[internal/base/thread.h]") { REQUIRE(base_tests_local_value_two == vector{1, 2}); } +int base_tests_shared_counter; + TEST_CASE( "spinlock protects concurrent counter", "[internal/base/spinlock.h]") { - constexpr int num_iterations = 100000; - int shared_counter = 0; + constexpr int num_iterations = 1000000; + base_tests_shared_counter = 0; spin_lock lock{}; SECTION( "lock can be used by itself" ) { auto t1 = start_thread([&]() { for (int i = 0; i < num_iterations; i++) { lock.lock(); - shared_counter++; + base_tests_shared_counter++; lock.unlock(); } }); auto t2 = start_thread([&]() { for (int i = 0; i < num_iterations; i++) { lock.lock(); - shared_counter--; + base_tests_shared_counter--; lock.unlock(); } }); @@ -57,26 +59,26 @@ TEST_CASE( "spinlock protects concurrent counter", "[internal/base/spinlock.h]") t1.join(); t2.join(); - REQUIRE(shared_counter == 0); + REQUIRE(base_tests_shared_counter == 0); } SECTION( "lock can be used with std::lock_guard" ) { auto t1 = start_thread([&]() { for (int i = 0; i < num_iterations; i++) { std::lock_guard my_lock{lock}; - shared_counter++; + base_tests_shared_counter++; } }); auto t2 = start_thread([&]() { for (int i = 0; i < num_iterations; i++) { std::lock_guard my_lock{lock}; - shared_counter--; + base_tests_shared_counter--; } }); t1.join(); t2.join(); - REQUIRE(shared_counter == 0); + REQUIRE(base_tests_shared_counter == 0); } } -- libgit2 0.26.0