[libcxx-commits] [libcxx] [libc++] Improve performance of std::atomic_flag on Windows (PR #163524)
Martin Storsjö via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Oct 17 14:23:19 PDT 2025
================
@@ -101,6 +105,40 @@ static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const vo
_umtx_op(const_cast<__cxx_atomic_contention_t*>(__ptr), UMTX_OP_WAKE, __notify_one ? 1 : INT_MAX, nullptr, nullptr);
}
+#elif defined(_WIN32)
+
+static void
+__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
+ // WaitOnAddress was added in Windows 8 (build 9200)
+ static auto __pWaitOnAddress = reinterpret_cast<BOOL(WINAPI*)(volatile void*, PVOID, SIZE_T, DWORD)>(
+ GetProcAddress(GetModuleHandleW(L"api-ms-win-core-synch-l1-2-0.dll"), "WaitOnAddress"));
+ if (__pWaitOnAddress != nullptr) {
+ __pWaitOnAddress(const_cast<__cxx_atomic_contention_t*>(__ptr), &__val, sizeof(__val), INFINITE);
+ } else {
+ __libcpp_thread_poll_with_backoff(
+ [=]() -> bool { return !__cxx_nonatomic_compare_equal(__cxx_atomic_load(__ptr, memory_order_relaxed), __val); },
+ __libcpp_timed_backoff_policy());
+ }
+}
+
+static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {
+ if (__notify_one) {
+ // WakeByAddressSingle was added in Windows 8 (build 9200)
+ static auto __pWakeByAddressSingle = reinterpret_cast<void(WINAPI*)(PVOID)>(
+ GetProcAddress(GetModuleHandleW(L"api-ms-win-core-synch-l1-2-0.dll"), "WakeByAddressSingle"));
+ if (__pWakeByAddressSingle != nullptr) {
+ __pWakeByAddressSingle(const_cast<__cxx_atomic_contention_t*>(__ptr));
+ }
+ } else {
+ // WakeByAddressAll was added in Windows 8 (build 9200)
+ static auto __pWakeByAddressAll = reinterpret_cast<void(WINAPI*)(PVOID)>(
+ GetProcAddress(GetModuleHandleW(L"api-ms-win-core-synch-l1-2-0.dll"), "WakeByAddressAll"));
+ if (__pWakeByAddressAll != nullptr) {
+ __pWakeByAddressAll(const_cast<__cxx_atomic_contention_t*>(__ptr));
+ }
----------------
mstorsjo wrote:
Even though it's evident once one looks a bit more on the fallback implementation, it might be nice for clarity, to have an `} else { // The fallback implementation of waking does nothing, as the fallback wait implementation just does polling` or something along those lines? Just to make it clear that the regular pattern would be `if (funcPtr) { funcPtr(); } else { fallback(); }` but the fallback is empty here.
https://github.com/llvm/llvm-project/pull/163524
More information about the libcxx-commits
mailing list