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


##########
test/atomicops_unittest.cc:
##########
@@ -210,32 +260,184 @@ 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;
+      butil::subtle::Release_Store(&turn, 1);

Review Comment:
   This handoff path verifies `Release_Store` and `Acquire_Load`. The existing 
release CAS is used only in the counting loop, and removing release ordering 
from its success path would not change the final count.



##########
src/butil/atomicops_internals_arm64_gcc.h:
##########
@@ -21,148 +15,72 @@ namespace butil {
 namespace subtle {
 
 inline void MemoryBarrier() {
-  __asm__ __volatile__ ("dmb ish" ::: "memory");  // NOLINT
+  __atomic_thread_fence(__ATOMIC_SEQ_CST);
 }
 
-// NoBarrier versions of the operation include "memory" in the clobber list.
-// This is not required for direct usage of the NoBarrier versions of the
-// operations. However this is required for correctness when they are used as
-// part of the Acquire or Release versions, to ensure that nothing from outside
-// the call is reordered between the operation and the memory barrier. This 
does
-// not change the code generated, so has no or minimal impact on the
-// NoBarrier operations.
-
 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
                                          Atomic32 old_value,
                                          Atomic32 new_value) {
-  Atomic32 prev;
-  int32_t temp;
-
-  __asm__ __volatile__ (  // NOLINT
-    "0:                                    \n\t"
-    "ldxr %w[prev], %[ptr]                 \n\t"  // Load the previous value.
-    "cmp %w[prev], %w[old_value]           \n\t"
-    "bne 1f                                \n\t"
-    "stxr %w[temp], %w[new_value], %[ptr]  \n\t"  // Try to store the new 
value.
-    "cbnz %w[temp], 0b                     \n\t"  // Retry if it did not work.
-    "1:                                    \n\t"
-    : [prev]"=&r" (prev),
-      [temp]"=&r" (temp),
-      [ptr]"+Q" (*ptr)
-    : [old_value]"IJr" (old_value),
-      [new_value]"r" (new_value)
-    : "cc", "memory"
-  );  // NOLINT
-
-  return prev;
+  __atomic_compare_exchange_n(ptr, &old_value, new_value, false,
+                              __ATOMIC_RELAXED, __ATOMIC_RELAXED);
+  return old_value;
 }
 
 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
                                          Atomic32 new_value) {
-  Atomic32 result;
-  int32_t temp;
-
-  __asm__ __volatile__ (  // NOLINT
-    "0:                                    \n\t"
-    "ldxr %w[result], %[ptr]               \n\t"  // Load the previous value.
-    "stxr %w[temp], %w[new_value], %[ptr]  \n\t"  // Try to store the new 
value.
-    "cbnz %w[temp], 0b                     \n\t"  // Retry if it did not work.
-    : [result]"=&r" (result),
-      [temp]"=&r" (temp),
-      [ptr]"+Q" (*ptr)
-    : [new_value]"r" (new_value)
-    : "memory"
-  );  // NOLINT
-
-  return result;
+  return __atomic_exchange_n(ptr, new_value, __ATOMIC_RELAXED);
 }
 
 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
                                           Atomic32 increment) {
-  Atomic32 result;
-  int32_t temp;
-
-  __asm__ __volatile__ (  // NOLINT
-    "0:                                       \n\t"
-    "ldxr %w[result], %[ptr]                  \n\t"  // Load the previous 
value.
-    "add %w[result], %w[result], %w[increment]\n\t"
-    "stxr %w[temp], %w[result], %[ptr]        \n\t"  // Try to store the 
result.
-    "cbnz %w[temp], 0b                        \n\t"  // Retry on failure.
-    : [result]"=&r" (result),
-      [temp]"=&r" (temp),
-      [ptr]"+Q" (*ptr)
-    : [increment]"IJr" (increment)
-    : "memory"
-  );  // NOLINT
-
-  return result;
+  return __atomic_add_fetch(ptr, increment, __ATOMIC_RELAXED);
 }
 
 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
                                         Atomic32 increment) {
-  MemoryBarrier();
-  Atomic32 result = NoBarrier_AtomicIncrement(ptr, increment);
-  MemoryBarrier();
-
-  return result;
+  return __atomic_add_fetch(ptr, increment, __ATOMIC_ACQ_REL);
 }
 
 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
                                        Atomic32 old_value,
                                        Atomic32 new_value) {
-  Atomic32 prev = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
-  MemoryBarrier();
-
-  return prev;
+  __atomic_compare_exchange_n(ptr, &old_value, new_value, false,
+                              __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE);
+  return old_value;
 }
 
 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
                                        Atomic32 old_value,
                                        Atomic32 new_value) {
-  MemoryBarrier();
-  Atomic32 prev = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
-
-  return prev;
+  __atomic_compare_exchange_n(ptr, &old_value, new_value, false,
+                              __ATOMIC_RELEASE, __ATOMIC_RELAXED);

Review Comment:
   When the comparison fails, this only performs a relaxed read; the 
`__ATOMIC_RELEASE` success ordering does not provide ordering on the failure 
path. If two threads each write their own flag and then perform a CAS on the 
other thread's flag, both may now read the initial value.



##########
test/atomicops_unittest.cc:
##########
@@ -210,32 +260,184 @@ 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;
+      butil::subtle::Release_Store(&turn, 1);
+    }
+  });
+
+  for (int i = 1; i <= kHandoffs; ++i) {
+    while (butil::subtle::Acquire_Load(&turn) != 1) {
+    }
+    const uint64_t obs_a = payload_a;
+    const uint64_t obs_b = payload_b;
+    const uint64_t expected_a =
+        static_cast<uint64_t>(i) * 0x9e3779b97f4a7c15ULL;
+    const uint64_t expected_b = ~expected_a;
+    EXPECT_EQ(static_cast<AtomicType>(1), butil::subtle::Release_Load(&turn));
+    if (obs_a != expected_a || obs_b != expected_b) {
+      butil::subtle::Release_Store(&turn, -1);
+      EXPECT_EQ(expected_a, obs_a);
+      EXPECT_EQ(expected_b, obs_b);
+      break;
+    }
+    if ((i & 1) == 0) {
+      butil::subtle::Acquire_Store(&turn, 0);
+    } else {
+      butil::subtle::Release_Store(&turn, 0);

Review Comment:
   On even rounds, `Acquire_Store` writes `0` back with a relaxed store. This 
relies on the fence in the preceding `Release_Load` to order the payload read, 
but Clang TSan does not record synchronization edges for standalone fences, so 
it may report the next round's payload overwrite as a race.



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