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

Roger Sanders via libcxx-commits libcxx-commits at lists.llvm.org
Sun Oct 26 02:17:09 PDT 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"));
----------------
RogerSanders wrote:

Good catch - that's a mistake (and an embarrassing one at that!). I think I did that in my testing and meant to fix it up, but didn't leave a note and forgot. It worked anyway, since "api-ms-win-core-synch-l1-2-0.dll" isn't actually a DLL name, it's an [API set](https://learn.microsoft.com/en-us/windows/win32/apiindex/windows-apisets) name, and this particular one is (currently) implemented by kernel32.dll, the one module that you can guarantee is always loaded. That's implementation detail though, and could change in the future.

I'll prepare a revision to call LoadLibrary as it should have done, but I'll need to introduce a helper method and a cleanup approach to call FreeLibrary on module unload. I'll do some research within libc++ to see how resource handles like this are currently handled. I've been known to throw this through a std::unique_ptr with a destructor these days, but that assumes that HMODULE is a pointer type, which while it will be forever, it technically isn't codified as such, so I wouldn't do that here. Might need a custom type to handle the free.

<rant>Why C++ doesn't have a generic, general purpose RAII wrapper type in 2025 is beyond me.</rant>

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


More information about the libcxx-commits mailing list