Skip to content
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

Avoid <atomic> in filesystem.cpp #3011

Merged
merged 2 commits into from
Aug 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions stl/src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// Do not include or define anything else here.
// In particular, basic_string must not be included here.

#include <atomic>
#include <clocale>
#include <corecrt_terminate.h>
#include <cstdlib>
Expand Down Expand Up @@ -794,22 +793,15 @@ _Success_(return == __std_win_error::_Success) __std_win_error
namespace {
_Success_(return > 0 && return < nBufferLength) DWORD WINAPI
_Stl_GetTempPath2W(_In_ DWORD nBufferLength, _Out_writes_to_opt_(nBufferLength, return +1) LPWSTR lpBuffer) {
// See GH-3011: This is intentionally not attempting to cache the function pointer.
// TRANSITION, ABI: This should use __crtGetTempPath2W after this code is moved into the STL's DLL.
using _Fun_ptr = decltype(&::GetTempPath2W);

_Fun_ptr _PfGetTempPath2W;
{
static _STD atomic<_Fun_ptr> _Static{nullptr};

_PfGetTempPath2W = _Static.load(_STD memory_order_relaxed);
if (!_PfGetTempPath2W) {
const auto _Kernel32 = ::GetModuleHandleW(L"kernel32.dll");
_Analysis_assume_(_Kernel32);
_PfGetTempPath2W = reinterpret_cast<_Fun_ptr>(::GetProcAddress(_Kernel32, "GetTempPath2W"));
if (!_PfGetTempPath2W) {
_PfGetTempPath2W = &::GetTempPathW;
}
_Static.store(_PfGetTempPath2W, _STD memory_order_relaxed); // overwriting with the same value is okay
}
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
const auto _Kernel32 = ::GetModuleHandleW(L"kernel32.dll");
_Analysis_assume_(_Kernel32);
_Fun_ptr _PfGetTempPath2W = reinterpret_cast<_Fun_ptr>(::GetProcAddress(_Kernel32, "GetTempPath2W"));
if (!_PfGetTempPath2W) {
_PfGetTempPath2W = &::GetTempPathW;
}

return _PfGetTempPath2W(nBufferLength, lpBuffer);
Expand Down