[libcxx-commits] [libcxx] [libc++] Allows any types of size 4 and 8 to use native platform ulock_wait (Proof of Concept) (PR #161086)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Oct 11 13:48:15 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);
----------------
huixie90 wrote:
yes and I did some godbolt as you suggested. with this program
```
void use(uint64_t);
void __libcpp_platform_wait_on_address(void const volatile* __ptr, void const volatile* __val) {
constexpr size_t _Size = 8;
char buffer[_Size];
std::memcpy(&buffer, const_cast<const void*>(__val), _Size);
use(*reinterpret_cast<uint64_t const*>(&buffer));
}
```
it generates
```
__libcpp_platform_wait_on_address(void const volatile*, void const*):
ldr x0, [x1]
b use(unsigned long)
```
so effectively copy the the address of the 2nd argument directly to the first argument register.
It actually caught another bug in my original code. My original code cast it to `uint64_t const volatile*` and. this `volatile` forced the compiler to copy the value on the stack
https://github.com/llvm/llvm-project/pull/161086
More information about the libcxx-commits
mailing list