jimmystewpot commented on code in PR #3752:
URL: https://github.com/apache/brpc/pull/3752#discussion_r4671421716


##########
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:
   Fixed in 29130d51. Added compiler TSan detection (`__SANITIZE_THREAD__` / 
`__has_feature(thread_sanitizer)` alongside `THREAD_SANITIZER`) and explicitly 
`GTEST_SKIP()`ed `MultiThreadedContention` and `AcquireReleaseVisibility` when 
compiled with `-fsanitize=thread` without `-DTHREAD_SANITIZER` on non-ARM64 
backends that use inline assembly and `volatile` accesses (while keeping full 
TSan coverage active on ARM64). Also updated 
`ReleaseCompareAndSwapFailureOrdering` to use the same TSan guard.



##########
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:
   Fixed in 74fbc0fb. Added a `std::atomic<bool> producer_done` release/acquire 
exit signal and early-break on CAS mismatch so if the producer exits without 
publishing `turn = 1` (including when the final `Release_CompareAndSwap` 
returns `0` without storing `1`), the consumer records `ADD_FAILURE()` and 
exits cleanly instead of hanging.



-- 
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]

Reply via email to