-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc++] Improve performance of std::atomic_flag on Windows #163524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
63ee5da
4515628
49efee9
899aed7
2586c83
930b0c4
27b395d
b6df5a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,10 @@ | |
| // OpenBSD has no indirect syscalls | ||
| # define _LIBCPP_FUTEX(...) futex(__VA_ARGS__) | ||
|
|
||
| #elif defined(_WIN32) | ||
|
|
||
| # include <windows.h> | ||
|
|
||
| #else // <- Add other operating systems here | ||
|
|
||
| // Baseline needs no new headers | ||
|
|
@@ -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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a way (e.g. a macro) to check if the function is available at compile time? we don't do this sort of check at runtime on other systems
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't know which OS the resulting binary will be run on, so no, no such thing, except by dropping support for windows 7 completely. Which may be a reasonable move - ms-stl already did - but that would require a project-wide RFC, not just a mundane-looking PR to an obscure module.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. libc++ seems to have dropped Windows 7 support (for modern toolchains where _WIN32_WINNT > _WIN32_WINNT_WIN8 ), but avoiding the dependency on synchronization.lib does sound very meaningful.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's not true. Libc++ does support Windows 7 - but when building, So if your toolchain defaults to a higher version, you need to manually define So for things like this, like the example in chrono.cpp, we can choose to either load symbols dynamically, if targeting an older version, or link directly, if we're targeting a new enough version. But in this case, the PR author pointed out that it would require linking in another import library if we'd link directly, which both is extra work on the CMake level, and also translates to extra work for users who statically link in libc++. So as the overhead is near-zero with only loading it the first time it is called, the PR author thought it was simplest to always go with the dynamic loading approach, which sounds reasonable to me.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that's exactly what I meant. Considering that both MinGW and Windows SDK default to _WIN32_WINNT_WIN10.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if they currently aren't using the latest LLVM, I don't think that logic should be extended so that they can't update to the latest one, for things they are working on. Whether the next release is ready for shipping right now or not seems beside the point - as long as that branch is worked on, and is built for a Win7 baseline, IMO.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, considering that version 4.0 will not be released anytime soon, absolutely NO SUPPORT for it from the VideoLAN Team before its release, and that VideoLAN also provides GCC builds, I suspect that there are likely few or no end users who will be affected by LLVM's decision to drop Windows 7 support. In this case, LLVM's downstream users should perhaps reconsider their platform support policies rather than passing the burden onto LLVM.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we figure out how to get synchronization.a into libc++.a? If no, we need GetProcAddress even on win11, and there is no additional support burden.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we can rely on llvm-lib, it's very simple, but if we have to be compatible with ar, it becomes very ugly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. VLC 4.0 will target Windows 7 as minimal, yes. VLC 4.0 will be LLVM-focused now, this being the default on macOS/iOS/visionOS, Android, and all Windows versions. Only some Linux versions will stay on GCC, because it is the default on some distributions. |
||
| wait_on_address(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 wake_by_address_single = reinterpret_cast<void(WINAPI*)(PVOID)>( | ||
| GetProcAddress(GetModuleHandleW(L"api-ms-win-core-synch-l1-2-0.dll"), "WakeByAddressSingle")); | ||
DavidSpickett marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (wake_by_address_single != nullptr) { | ||
| wake_by_address_single(const_cast<__cxx_atomic_contention_t*>(__ptr)); | ||
| } else { | ||
| // The fallback implementation of waking does nothing, as the fallback wait implementation just does polling, so | ||
| // there's nothing to do here. | ||
| } | ||
| } else { | ||
| // WakeByAddressAll was added in Windows 8 (build 9200) | ||
| static auto wake_by_address_all = reinterpret_cast<void(WINAPI*)(PVOID)>( | ||
| GetProcAddress(GetModuleHandleW(L"api-ms-win-core-synch-l1-2-0.dll"), "WakeByAddressAll")); | ||
| if (wake_by_address_all != nullptr) { | ||
| wake_by_address_all(const_cast<__cxx_atomic_contention_t*>(__ptr)); | ||
| } else { | ||
| // The fallback implementation of waking does nothing, as the fallback wait implementation just does polling, so | ||
| // there's nothing to do here. | ||
| } | ||
philnik777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| #else // <- Add other operating systems here | ||
|
|
||
| // Baseline is just a timed backoff | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.