Skip to content

Commit 3ef766a

Browse files
committed
[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
1 parent 243bc75 commit 3ef766a

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

compiler-rt/lib/scudo/standalone/atomic_helpers.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ inline void atomic_store_relaxed(volatile T *A, typename T::Type V) {
133133
}
134134

135135
template <typename T>
136-
inline typename T::Type atomic_compare_exchange(volatile T *A,
137-
typename T::Type Cmp,
138-
typename T::Type Xchg) {
139-
atomic_compare_exchange_strong(A, &Cmp, Xchg, memory_order_acquire);
136+
inline typename T::Type
137+
atomic_compare_exchange_strong(volatile T *A, typename T::Type Cmp,
138+
typename T::Type Xchg, memory_order MO) {
139+
atomic_compare_exchange_strong(A, &Cmp, Xchg, MO);
140140
return Cmp;
141141
}
142142

compiler-rt/lib/scudo/standalone/linux.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,14 @@ enum State : u32 { Unlocked = 0, Locked = 1, Sleeping = 2 };
104104
}
105105

106106
bool HybridMutex::tryLock() {
107-
return atomic_compare_exchange(&M, Unlocked, Locked) == Unlocked;
107+
return atomic_compare_exchange_strong(&M, Unlocked, Locked,
108+
memory_order_acquire) == Unlocked;
108109
}
109110

110111
// The following is based on https://akkadia.org/drepper/futex.pdf.
111112
void HybridMutex::lockSlow() {
112-
u32 V = atomic_compare_exchange(&M, Unlocked, Locked);
113+
u32 V = atomic_compare_exchange_strong(&M, Unlocked, Locked,
114+
memory_order_acquire);
113115
if (V == Unlocked)
114116
return;
115117
if (V != Sleeping)

0 commit comments

Comments
 (0)