[libcxx-commits] [libcxx] [libc++] Improve performance of std::atomic_flag on Windows (PR #163524)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Nov 2 14:40:39 PST 2025


================
@@ -101,6 +105,46 @@ 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 wait_on_address = reinterpret_cast<BOOL(WINAPI*)(volatile void*, PVOID, SIZE_T, DWORD)>(
+      GetProcAddress(GetModuleHandleW(L"api-ms-win-core-synch-l1-2-0.dll"), "WaitOnAddress"));
+  if (wait_on_address != nullptr) {
----------------
Alcaro wrote:

That was added in https://github.com/microsoft/STL/pull/593. I'm sure most of these questions were answered somewhere near that PR, but there's hundreds of posts in there and I don't feel like reading through them all.

That code doesn't directly prove whether the apiset is always present on 8+ (if it isn't, there's a srwlock fallback). However, it does prove that if the apiset is present, then it will never disappear; it also proves it's _usually_ present (otherwise they'd notice that performance didn't improve as they'd expect).

I agree with your conclusion; if it's good enough for them, then it's good enough for us.

(These days, they've dropped pre-8 support and just link with synchronization.lib. https://github.com/microsoft/STL/blob/main/stl/src/atomic_wait.cpp#L14)

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


More information about the libcxx-commits mailing list