Commit 56e16fcf by FritzFlorian

Fix: Spinlock test triggers thread sanitizer.

parent 0599cfdc
Pipeline #1101 passed with stages
in 2 minutes 12 seconds
image: "matrim/cmake-examples:3.10.3" image: "flofritz/ci-benchmark-hpx-embb"
stages: stages:
- build - build
......
...@@ -11,10 +11,12 @@ namespace pls { ...@@ -11,10 +11,12 @@ namespace pls {
this_thread::yield(); this_thread::yield();
} }
} }
std::atomic_thread_fence(std::memory_order_seq_cst);
} }
void spin_lock::unlock() { void spin_lock::unlock() {
flag_.clear(std::memory_order_release); flag_.clear(std::memory_order_release);
std::atomic_thread_fence(std::memory_order_seq_cst);
} }
} }
} }
......
...@@ -33,23 +33,25 @@ TEST_CASE( "thread state", "[internal/base/thread.h]") { ...@@ -33,23 +33,25 @@ TEST_CASE( "thread state", "[internal/base/thread.h]") {
REQUIRE(base_tests_local_value_two == vector<int>{1, 2}); REQUIRE(base_tests_local_value_two == vector<int>{1, 2});
} }
int base_tests_shared_counter;
TEST_CASE( "spinlock protects concurrent counter", "[internal/base/spinlock.h]") { TEST_CASE( "spinlock protects concurrent counter", "[internal/base/spinlock.h]") {
constexpr int num_iterations = 100000; constexpr int num_iterations = 1000000;
int shared_counter = 0; base_tests_shared_counter = 0;
spin_lock lock{}; spin_lock lock{};
SECTION( "lock can be used by itself" ) { SECTION( "lock can be used by itself" ) {
auto t1 = start_thread([&]() { auto t1 = start_thread([&]() {
for (int i = 0; i < num_iterations; i++) { for (int i = 0; i < num_iterations; i++) {
lock.lock(); lock.lock();
shared_counter++; base_tests_shared_counter++;
lock.unlock(); lock.unlock();
} }
}); });
auto t2 = start_thread([&]() { auto t2 = start_thread([&]() {
for (int i = 0; i < num_iterations; i++) { for (int i = 0; i < num_iterations; i++) {
lock.lock(); lock.lock();
shared_counter--; base_tests_shared_counter--;
lock.unlock(); lock.unlock();
} }
}); });
...@@ -57,26 +59,26 @@ TEST_CASE( "spinlock protects concurrent counter", "[internal/base/spinlock.h]") ...@@ -57,26 +59,26 @@ TEST_CASE( "spinlock protects concurrent counter", "[internal/base/spinlock.h]")
t1.join(); t1.join();
t2.join(); t2.join();
REQUIRE(shared_counter == 0); REQUIRE(base_tests_shared_counter == 0);
} }
SECTION( "lock can be used with std::lock_guard" ) { SECTION( "lock can be used with std::lock_guard" ) {
auto t1 = start_thread([&]() { auto t1 = start_thread([&]() {
for (int i = 0; i < num_iterations; i++) { for (int i = 0; i < num_iterations; i++) {
std::lock_guard<spin_lock> my_lock{lock}; std::lock_guard<spin_lock> my_lock{lock};
shared_counter++; base_tests_shared_counter++;
} }
}); });
auto t2 = start_thread([&]() { auto t2 = start_thread([&]() {
for (int i = 0; i < num_iterations; i++) { for (int i = 0; i < num_iterations; i++) {
std::lock_guard<spin_lock> my_lock{lock}; std::lock_guard<spin_lock> my_lock{lock};
shared_counter--; base_tests_shared_counter--;
} }
}); });
t1.join(); t1.join();
t2.join(); t2.join();
REQUIRE(shared_counter == 0); REQUIRE(base_tests_shared_counter == 0);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment