[libcxx-commits] [libcxx] [libc++] Fix FreeBSD atomic timed wait system call (PR #180400)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 11 06:16:17 PST 2026


https://github.com/huixie90 updated https://github.com/llvm/llvm-project/pull/180400

>From 48cdeb63e24b954e65e124912f7549a59ef552e7 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 8 Feb 2026 10:02:01 +0000
Subject: [PATCH 1/3] [libc++] FreeBSD use fallback for atomic timed wait

---
 libcxx/src/atomic.cpp | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

diff --git a/libcxx/src/atomic.cpp b/libcxx/src/atomic.cpp
index 637c971ed8896..b198cf845f504 100644
--- a/libcxx/src/atomic.cpp
+++ b/libcxx/src/atomic.cpp
@@ -135,17 +135,10 @@ static void __platform_wait_on_address(void const* __ptr, void const* __val, uin
   if (__timeout_ns == 0) {
     _umtx_op(const_cast<void*>(__ptr), UMTX_OP_WAIT, *reinterpret_cast<__cxx_contention_t*>(&buffer), nullptr, nullptr);
   } else {
-    _umtx_time ut;
-    ut._timeout.tv_sec  = __timeout_ns / 1'000'000'000;
-    ut._timeout.tv_nsec = __timeout_ns % 1'000'000'000;
-    ut._flags           = 0;               // Relative time (not absolute)
-    ut._clockid         = CLOCK_MONOTONIC; // Use monotonic clock
-
-    _umtx_op(const_cast<void*>(__ptr),
-             UMTX_OP_WAIT,
-             *reinterpret_cast<__cxx_contention_t*>(&buffer),
-             reinterpret_cast<void*>(sizeof(ut)), // Pass size as uaddr
-             &ut);                                // Pass _umtx_time structure as uaddr2
+    __libcpp_thread_poll_with_backoff(
+        [=]() -> bool { return std::memcmp(const_cast<const void*>(__ptr), __val, _Size) != 0; },
+        __libcpp_timed_backoff_policy(),
+        std::chrono::nanoseconds(__timeout_ns));
   }
 }
 

>From 668eb2a562171a54ac4c8aebffdd2ba0f3958c23 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Wed, 11 Feb 2026 12:51:57 +0000
Subject: [PATCH 2/3] fix

---
 libcxx/src/atomic.cpp | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/libcxx/src/atomic.cpp b/libcxx/src/atomic.cpp
index b198cf845f504..59da0b59be359 100644
--- a/libcxx/src/atomic.cpp
+++ b/libcxx/src/atomic.cpp
@@ -130,15 +130,20 @@ static void __platform_wake_by_address(void const* __ptr, bool __notify_one) {
 template <std::size_t _Size>
 static void __platform_wait_on_address(void const* __ptr, void const* __val, uint64_t __timeout_ns) {
   static_assert(_Size == 8, "Can only wait on 8 bytes value");
-  char buffer[_Size];
+  alignas(__cxx_contention_t) char buffer[_Size];
   std::memcpy(&buffer, const_cast<const void*>(__val), _Size);
   if (__timeout_ns == 0) {
     _umtx_op(const_cast<void*>(__ptr), UMTX_OP_WAIT, *reinterpret_cast<__cxx_contention_t*>(&buffer), nullptr, nullptr);
   } else {
-    __libcpp_thread_poll_with_backoff(
-        [=]() -> bool { return std::memcmp(const_cast<const void*>(__ptr), __val, _Size) != 0; },
-        __libcpp_timed_backoff_policy(),
-        std::chrono::nanoseconds(__timeout_ns));
+    timespec timeout{};
+    timeout.tv_sec  = __timeout_ns / 1'000'000'000;
+    timeout.tv_nsec = __timeout_ns % 1'000'000'000;
+
+    _umtx_op(const_cast<void*>(__ptr),
+             UMTX_OP_WAIT,
+             *reinterpret_cast<__cxx_contention_t*>(&buffer),
+             static_cast<void*>(static_cast<uintptr_t>(sizeof(timeout))),
+             &timeout);
   }
 }
 

>From cf7eda763503432f711739af488fc021bed198d7 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Wed, 11 Feb 2026 14:16:04 +0000
Subject: [PATCH 3/3] review

---
 libcxx/src/atomic.cpp | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/libcxx/src/atomic.cpp b/libcxx/src/atomic.cpp
index 59da0b59be359..5fa071169b374 100644
--- a/libcxx/src/atomic.cpp
+++ b/libcxx/src/atomic.cpp
@@ -130,10 +130,13 @@ static void __platform_wake_by_address(void const* __ptr, bool __notify_one) {
 template <std::size_t _Size>
 static void __platform_wait_on_address(void const* __ptr, void const* __val, uint64_t __timeout_ns) {
   static_assert(_Size == 8, "Can only wait on 8 bytes value");
-  alignas(__cxx_contention_t) char buffer[_Size];
-  std::memcpy(&buffer, const_cast<const void*>(__val), _Size);
+  // At the moment, on FreeBSD, both stable and unstable ABI only supports platform wait with __cxx_contention_t
+  // It is safe to reinterpret_cast the val as it is ever going to be passed a __cxx_contention_t
+  // If in the future FreeBSD decides to experiment unstable ABI to support more types, this cast will no longer be
+  // safe.
+  __cxx_contention_t value = *reinterpret_cast<const __cxx_contention_t*>(__val);
   if (__timeout_ns == 0) {
-    _umtx_op(const_cast<void*>(__ptr), UMTX_OP_WAIT, *reinterpret_cast<__cxx_contention_t*>(&buffer), nullptr, nullptr);
+    _umtx_op(const_cast<void*>(__ptr), UMTX_OP_WAIT, value, nullptr, nullptr);
   } else {
     timespec timeout{};
     timeout.tv_sec  = __timeout_ns / 1'000'000'000;
@@ -141,7 +144,7 @@ static void __platform_wait_on_address(void const* __ptr, void const* __val, uin
 
     _umtx_op(const_cast<void*>(__ptr),
              UMTX_OP_WAIT,
-             *reinterpret_cast<__cxx_contention_t*>(&buffer),
+             value,
              static_cast<void*>(static_cast<uintptr_t>(sizeof(timeout))),
              &timeout);
   }



More information about the libcxx-commits mailing list