chenBright commented on code in PR #3752:
URL: https://github.com/apache/brpc/pull/3752#discussion_r4663870946
##########
test/atomicops_unittest.cc:
##########
@@ -210,32 +261,254 @@ static void TestLoad() {
EXPECT_EQ(kVal2, butil::subtle::Release_Load(&value));
}
+template <class AtomicType>
+static void TestMultiThreadedContention() {
+ constexpr int kNumThreads = 8;
+ constexpr int kIterations = 2000;
+
+ volatile AtomicType nobarrier_inc = 0;
+ volatile AtomicType barrier_inc = 0;
+ volatile AtomicType acq_cas_counter = 0;
+ volatile AtomicType rel_cas_counter = 0;
+ volatile AtomicType spinlock = 0;
+ AtomicType protected_counter = 0;
+
+ std::vector<std::thread> threads;
+ threads.reserve(kNumThreads);
+ for (int t = 0; t < kNumThreads; ++t) {
+ threads.emplace_back([&]() {
+ for (int i = 0; i < kIterations; ++i) {
+ butil::subtle::NoBarrier_AtomicIncrement(&nobarrier_inc, 1);
+ butil::subtle::Barrier_AtomicIncrement(&barrier_inc, 1);
+
+ // Contended Acquire_CompareAndSwap loop.
+ AtomicType old_val = butil::subtle::NoBarrier_Load(&acq_cas_counter);
+ while (true) {
+ AtomicType prev = butil::subtle::Acquire_CompareAndSwap(
+ &acq_cas_counter, old_val, old_val + 1);
+ if (prev == old_val) {
+ break;
+ }
+ old_val = prev;
+ }
+
+ // Contended Release_CompareAndSwap loop.
+ old_val = butil::subtle::NoBarrier_Load(&rel_cas_counter);
+ while (true) {
+ AtomicType prev = butil::subtle::Release_CompareAndSwap(
+ &rel_cas_counter, old_val, old_val + 1);
+ if (prev == old_val) {
+ break;
+ }
+ old_val = prev;
+ }
+
+ // Acquire_CompareAndSwap / Release_Store spinlock protecting
non-atomic data.
+ while (butil::subtle::Acquire_CompareAndSwap(&spinlock, 0, 1) != 0) {
+ }
+ ++protected_counter;
+ butil::subtle::Release_Store(&spinlock, 0);
Review Comment:
On Linux x86-64, when only `-fsanitize=thread` is passed, this test uses
inline assembly CAS and `volatile` unlocking. TSan may not see the lock handoff
between threads and may consequently report a race on `++protected_counter`.
##########
test/atomicops_unittest.cc:
##########
@@ -210,32 +261,254 @@ static void TestLoad() {
EXPECT_EQ(kVal2, butil::subtle::Release_Load(&value));
}
+template <class AtomicType>
+static void TestMultiThreadedContention() {
+ constexpr int kNumThreads = 8;
+ constexpr int kIterations = 2000;
+
+ volatile AtomicType nobarrier_inc = 0;
+ volatile AtomicType barrier_inc = 0;
+ volatile AtomicType acq_cas_counter = 0;
+ volatile AtomicType rel_cas_counter = 0;
+ volatile AtomicType spinlock = 0;
+ AtomicType protected_counter = 0;
+
+ std::vector<std::thread> threads;
+ threads.reserve(kNumThreads);
+ for (int t = 0; t < kNumThreads; ++t) {
+ threads.emplace_back([&]() {
+ for (int i = 0; i < kIterations; ++i) {
+ butil::subtle::NoBarrier_AtomicIncrement(&nobarrier_inc, 1);
+ butil::subtle::Barrier_AtomicIncrement(&barrier_inc, 1);
+
+ // Contended Acquire_CompareAndSwap loop.
+ AtomicType old_val = butil::subtle::NoBarrier_Load(&acq_cas_counter);
+ while (true) {
+ AtomicType prev = butil::subtle::Acquire_CompareAndSwap(
+ &acq_cas_counter, old_val, old_val + 1);
+ if (prev == old_val) {
+ break;
+ }
+ old_val = prev;
+ }
+
+ // Contended Release_CompareAndSwap loop.
+ old_val = butil::subtle::NoBarrier_Load(&rel_cas_counter);
+ while (true) {
+ AtomicType prev = butil::subtle::Release_CompareAndSwap(
+ &rel_cas_counter, old_val, old_val + 1);
+ if (prev == old_val) {
+ break;
+ }
+ old_val = prev;
+ }
+
+ // Acquire_CompareAndSwap / Release_Store spinlock protecting
non-atomic data.
+ while (butil::subtle::Acquire_CompareAndSwap(&spinlock, 0, 1) != 0) {
+ }
+ ++protected_counter;
+ butil::subtle::Release_Store(&spinlock, 0);
+ }
+ });
+ }
+
+ for (auto& th : threads) {
+ th.join();
+ }
+
+ const AtomicType kExpected = kNumThreads * kIterations;
+ EXPECT_EQ(kExpected, butil::subtle::NoBarrier_Load(&nobarrier_inc));
+ EXPECT_EQ(kExpected, butil::subtle::Acquire_Load(&barrier_inc));
+ EXPECT_EQ(kExpected, butil::subtle::Acquire_Load(&acq_cas_counter));
+ EXPECT_EQ(kExpected, butil::subtle::Acquire_Load(&rel_cas_counter));
+ EXPECT_EQ(kExpected, protected_counter);
+}
+
+template <class AtomicType>
+static void TestAcquireReleaseVisibility() {
+ constexpr int kHandoffs = 2000;
+ volatile AtomicType turn = 0;
+ uint64_t payload_a = 0;
+ uint64_t payload_b = 0;
+
+ std::thread producer([&]() {
+ for (int i = 1; i <= kHandoffs; ++i) {
+ AtomicType t = butil::subtle::Acquire_Load(&turn);
+ while (t == 1) {
+ t = butil::subtle::Acquire_Load(&turn);
+ }
+ if (t < 0) {
+ break;
+ }
+ payload_a = static_cast<uint64_t>(i) * 0x9e3779b97f4a7c15ULL;
+ payload_b = ~payload_a;
+ if ((i & 1) == 0) {
+ EXPECT_EQ(static_cast<AtomicType>(0),
+ butil::subtle::Release_CompareAndSwap(&turn, 0, 1));
+ } else {
+ butil::subtle::Release_Store(&turn, 1);
+ }
+ }
+ });
+
+ for (int i = 1; i <= kHandoffs; ++i) {
+ while (butil::subtle::Acquire_Load(&turn) != 1) {
+ }
Review Comment:
If the final CAS returns `0` but fails to write `1`, the producer's
`EXPECT_EQ` will pass and the producer will exit, while the consumer will
remain stuck here and never reach the existing `-1` termination path.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]