[llvm-branch-commits] [libcxx] release/22.x: [libc++] Fix FreeBSD atomic timed wait system call (#180400) (PR #181801)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Feb 17 03:13:58 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: None (llvmbot)

<details>
<summary>Changes</summary>

Backport 180f7823cf1952e43b5f27c2deab3318c8d96ace

Requested by: @<!-- -->huixie90

---
Full diff: https://github.com/llvm/llvm-project/pull/181801.diff


1 Files Affected:

- (modified) libcxx/src/atomic.cpp (+12-11) 


``````````diff
diff --git a/libcxx/src/atomic.cpp b/libcxx/src/atomic.cpp
index 9f2e0403f2318..3948f8b037976 100644
--- a/libcxx/src/atomic.cpp
+++ b/libcxx/src/atomic.cpp
@@ -144,22 +144,23 @@ static void __platform_wake_by_address(void const* __ptr, bool __notify_one) {
 template <std::size_t _Size, class MaybeTimeout>
 static void __platform_wait_on_address(void const* __ptr, void const* __val, MaybeTimeout maybe_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, FreeBSD is stuck on stable ABI, which 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 under this ABI
+  // 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 constexpr (is_same_v<MaybeTimeout, NoTimeout>) {
-    _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 {
-    _umtx_time ut;
-    ut._timeout.tv_sec  = maybe_timeout_ns / 1'000'000'000;
-    ut._timeout.tv_nsec = maybe_timeout_ns % 1'000'000'000;
-    ut._flags           = 0;               // Relative time (not absolute)
-    ut._clockid         = CLOCK_MONOTONIC; // Use monotonic clock
+    timespec timeout{};
+    timeout.tv_sec  = maybe_timeout_ns / 1'000'000'000;
+    timeout.tv_nsec = maybe_timeout_ns % 1'000'000'000;
 
     _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
+             value,
+             reinterpret_cast<void*>(static_cast<uintptr_t>(sizeof(timeout))),
+             &timeout);
   }
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/181801


More information about the llvm-branch-commits mailing list