[compiler-rt] 3ef766a - [scudo] Specify memory order while using atomic_compare_exchange

Chia-hung Duan via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 3 15:16:17 PDT 2023


Author: Chia-hung Duan
Date: 2023-08-03T22:15:13Z
New Revision: 3ef766addadd8324f58c0fda0301edcde2185cb3

URL: https://github.com/llvm/llvm-project/commit/3ef766addadd8324f58c0fda0301edcde2185cb3
DIFF: https://github.com/llvm/llvm-project/commit/3ef766addadd8324f58c0fda0301edcde2185cb3.diff

LOG: [scudo] Specify memory order while using atomic_compare_exchange

`atomic_compare_exchange` was using `_strong` and `memory_order_acquire`
by default. This is not aligned with general use, for example, in C++,
the default is `memory_order_seq_cst`. To reduce the ambiguity, make the
version and ordering explicitly.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D156952

Added: 
    

Modified: 
    compiler-rt/lib/scudo/standalone/atomic_helpers.h
    compiler-rt/lib/scudo/standalone/linux.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/atomic_helpers.h b/compiler-rt/lib/scudo/standalone/atomic_helpers.h
index d88f5d7be642e4..a68ffd16291c8e 100644
--- a/compiler-rt/lib/scudo/standalone/atomic_helpers.h
+++ b/compiler-rt/lib/scudo/standalone/atomic_helpers.h
@@ -133,10 +133,10 @@ inline void atomic_store_relaxed(volatile T *A, typename T::Type V) {
 }
 
 template <typename T>
-inline typename T::Type atomic_compare_exchange(volatile T *A,
-                                                typename T::Type Cmp,
-                                                typename T::Type Xchg) {
-  atomic_compare_exchange_strong(A, &Cmp, Xchg, memory_order_acquire);
+inline typename T::Type
+atomic_compare_exchange_strong(volatile T *A, typename T::Type Cmp,
+                               typename T::Type Xchg, memory_order MO) {
+  atomic_compare_exchange_strong(A, &Cmp, Xchg, MO);
   return Cmp;
 }
 

diff  --git a/compiler-rt/lib/scudo/standalone/linux.cpp b/compiler-rt/lib/scudo/standalone/linux.cpp
index e285d8a3d2d230..cb746b8eb04f89 100644
--- a/compiler-rt/lib/scudo/standalone/linux.cpp
+++ b/compiler-rt/lib/scudo/standalone/linux.cpp
@@ -104,12 +104,14 @@ enum State : u32 { Unlocked = 0, Locked = 1, Sleeping = 2 };
 }
 
 bool HybridMutex::tryLock() {
-  return atomic_compare_exchange(&M, Unlocked, Locked) == Unlocked;
+  return atomic_compare_exchange_strong(&M, Unlocked, Locked,
+                                        memory_order_acquire) == Unlocked;
 }
 
 // The following is based on https://akkadia.org/drepper/futex.pdf.
 void HybridMutex::lockSlow() {
-  u32 V = atomic_compare_exchange(&M, Unlocked, Locked);
+  u32 V = atomic_compare_exchange_strong(&M, Unlocked, Locked,
+                                         memory_order_acquire);
   if (V == Unlocked)
     return;
   if (V != Sleeping)


        


More information about the llvm-commits mailing list