[libcxx-commits] [libcxx] [libc++] Use public os_sync API instead of private __ulock on newer Apple platforms (PR #202519)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Jun 9 00:09:46 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Louis Dionne (ldionne)
<details>
<summary>Changes</summary>
The atomic wait and wake implementation on Apple platforms currently relies on `__ulock_wait` and `__ulock_wake`, which are private kernel APIs. This is a problem for anyone shipping apps through the App Store since Apple flags private symbol usage during review.
Starting with macOS 14.4 and iOS 17.4, Apple ships public replacements through `os_sync_wait_on_address` and `os_sync_wake_by_address_any/all` in `<os/os_sync_wait_on_address.h>`. These cover the same functionality and are documented, stable, and safe for App Store submissions.
The change adds a `_LIBCPP_USE_OS_SYNC` macro that gets defined when the deployment target is new enough. In `atomic.cpp` the existing `__APPLE__` branch internally selects between the new public `os_sync` path and the existing `__ulock` fallback, so newer targets pick up the public API while older ones keep working exactly as before. The timeout path also benefits here since `os_sync_wait_on_address_with_timeout` accepts nanoseconds directly, avoiding the microsecond conversion that `__ulock_wait` required.
This takes over #<!-- -->182947.
Fixes #<!-- -->182908
Fixes #<!-- -->146142
---
Full diff: https://github.com/llvm/llvm-project/pull/202519.diff
1 Files Affected:
- (modified) libcxx/src/atomic.cpp (+49-3)
``````````diff
diff --git a/libcxx/src/atomic.cpp b/libcxx/src/atomic.cpp
index b661714cff6ec..1a104ffe557c7 100644
--- a/libcxx/src/atomic.cpp
+++ b/libcxx/src/atomic.cpp
@@ -17,6 +17,17 @@
#include <thread>
#include <type_traits>
+// Determine whether to use the public os_sync API on Apple platforms based on the deployment target.
+// Otherwise we fall back to the private __ulock API.
+#if defined(__APPLE__)
+# if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 140400) || \
+ (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 170400) || \
+ (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 170400) || \
+ (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 100400)
+# define _LIBCPP_USE_OS_SYNC
+# endif
+#endif
+
#ifdef __linux__
# include <linux/futex.h>
@@ -49,6 +60,13 @@
# include <memory>
# include <windows.h>
+#elif defined(__APPLE__)
+
+# if defined(_LIBCPP_USE_OS_SYNC)
+# include <os/os_sync_wait_on_address.h>
+# include <time.h>
+# endif
+
#elif defined(__Fuchsia__)
# include <zircon/syscalls.h>
@@ -95,14 +113,40 @@ static void __platform_wake_by_address(void const* __ptr, bool __notify_one) {
#elif defined(__APPLE__)
+# if defined(_LIBCPP_USE_OS_SYNC)
+
+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 || _Size == 4, "Can only wait on 8 bytes or 4 bytes value");
+ uint64_t __value = 0;
+ std::memcpy(&__value, __val, _Size);
+ if constexpr (is_same_v<MaybeTimeout, NoTimeout>) {
+ os_sync_wait_on_address(const_cast<void*>(__ptr), __value, _Size, OS_SYNC_WAIT_ON_ADDRESS_NONE);
+ } else {
+ os_sync_wait_on_address_with_timeout(
+ const_cast<void*>(__ptr), __value, _Size, OS_SYNC_WAIT_ON_ADDRESS_NONE, CLOCK_MONOTONIC_RAW, maybe_timeout_ns);
+ }
+}
+
+template <std::size_t _Size>
+static void __platform_wake_by_address(void const* __ptr, bool __notify_one) {
+ static_assert(_Size == 8 || _Size == 4, "Can only wake up on 8 bytes or 4 bytes value");
+ if (__notify_one)
+ os_sync_wake_by_address_any(const_cast<void*>(__ptr), _Size, OS_SYNC_WAKE_BY_ADDRESS_NONE);
+ else
+ os_sync_wake_by_address_all(const_cast<void*>(__ptr), _Size, OS_SYNC_WAKE_BY_ADDRESS_NONE);
+}
+
+# else // !_LIBCPP_USE_OS_SYNC -- fall back to the private __ulock API
+
extern "C" int __ulock_wait(
uint32_t operation, void* addr, uint64_t value, uint32_t timeout); /* timeout is specified in microseconds */
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
+# define UL_COMPARE_AND_WAIT 1
+# define UL_COMPARE_AND_WAIT64 5
+# define ULF_WAKE_ALL 0x00000100
template <std::size_t _Size, class MaybeTimeout>
static void __platform_wait_on_address(void const* __ptr, void const* __val, MaybeTimeout maybe_timeout_ns) {
@@ -137,6 +181,8 @@ static void __platform_wake_by_address(void const* __ptr, bool __notify_one) {
__ulock_wake(UL_COMPARE_AND_WAIT64 | (__notify_one ? 0 : ULF_WAKE_ALL), const_cast<void*>(__ptr), 0);
}
+# endif // _LIBCPP_USE_OS_SYNC
+
#elif defined(__FreeBSD__) && __SIZEOF_LONG__ == 8
/*
* Since __cxx_contention_t is int64_t even on 32bit FreeBSD
``````````
</details>
https://github.com/llvm/llvm-project/pull/202519
More information about the libcxx-commits
mailing list