[libcxx-commits] [libcxx] [libc++] Allows any types of size 4 and 8 to use native platform ulock_wait (Proof of Concept) (PR #161086)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 3 11:32:54 PDT 2025


================
@@ -70,22 +73,32 @@ extern "C" int __ulock_wait(
 extern "C" int __ulock_wake(uint32_t operation, void* addr, uint64_t wake_value);
 
 // https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/ulock.h#L82
+#  define UL_COMPARE_AND_WAIT 1
 #  define UL_COMPARE_AND_WAIT64 5
 #  define ULF_WAKE_ALL 0x00000100
 
-static void
-__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
-  static_assert(sizeof(__cxx_atomic_contention_t) == 8, "Waiting on 8 bytes value");
-  __ulock_wait(UL_COMPARE_AND_WAIT64, const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);
+template <std::size_t _Size>
+static void __libcpp_platform_wait_on_address(void const volatile* __ptr, void const volatile* __val) {
+  static_assert(_Size == 8 || _Size == 4, "Can only wait on 8 bytes or 4 bytes value");
+  if constexpr (_Size == 4)
+    __ulock_wait(UL_COMPARE_AND_WAIT, const_cast<void*>(__ptr), *reinterpret_cast<uint32_t const volatile*>(__val), 0);
----------------
ldionne wrote:

I think the dereference after this `reinterpret_cast` is UB since the object is not actually a `uint32_t`. Instead, I think we'd need to `memcpy` the bytes of that object into a local buffer and then pass that to  `__ulock_wait` using a `bit_cast<uint64_t>`. Under the hood, I wonder how the compiler's going to code-gen that. Will it pass by register?

That should be easy to Godbolt by stubbing the `__ulock_wait` signature.

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


More information about the libcxx-commits mailing list