[libcxx-commits] [libcxx] [libc++] fix atomic::wait memory order on platforms with weak ordering (PR #144257)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jun 15 03:14:09 PDT 2025


https://github.com/huixie90 created https://github.com/llvm/llvm-project/pull/144257

None

>From 452301b4e8f3beb4441932a72049200688db8db8 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 15 Jun 2025 11:13:39 +0100
Subject: [PATCH] [libc++] fix atomic::wait memory order on platforms with weak
 ordering

---
 libcxx/src/atomic.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/libcxx/src/atomic.cpp b/libcxx/src/atomic.cpp
index c1af8d6f95aae..8b875f26bca7d 100644
--- a/libcxx/src/atomic.cpp
+++ b/libcxx/src/atomic.cpp
@@ -153,6 +153,9 @@ static void __libcpp_contention_wait(__cxx_atomic_contention_t volatile* __conte
                                      __cxx_contention_t __old_value) {
   __cxx_atomic_fetch_add(__contention_state, __cxx_contention_t(1), memory_order_seq_cst);
   // We sleep as long as the monitored value hasn't changed.
+#if !defined(__x86_64__) && !defined(__aarch64__)
+  __cxx_atomic_thread_fence(memory_order_seq_cst);
+#endif
   __libcpp_platform_wait_on_address(__platform_state, __old_value);
   __cxx_atomic_fetch_sub(__contention_state, __cxx_contention_t(1), memory_order_release);
 }
@@ -163,7 +166,7 @@ static void __libcpp_contention_wait(__cxx_atomic_contention_t volatile* __conte
 static void __libcpp_atomic_notify(void const volatile* __location) {
   auto const __entry = __libcpp_contention_state(__location);
   // The value sequence laundering happens on the next line below.
-  __cxx_atomic_fetch_add(&__entry->__platform_state, __cxx_contention_t(1), memory_order_release);
+  __cxx_atomic_fetch_add(&__entry->__platform_state, __cxx_contention_t(1), memory_order_seq_cst);
   __libcpp_contention_notify(
       &__entry->__contention_state,
       &__entry->__platform_state,



More information about the libcxx-commits mailing list