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

<locale>: Double-checked locking for locale::classic #3048

Merged
merged 8 commits into from
Oct 27, 2022
44 changes: 40 additions & 4 deletions stl/src/locale0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,36 @@ __PURE_APPDOMAIN_GLOBAL locale::id ctype<unsigned short>::id(0);

__PURE_APPDOMAIN_GLOBAL locale::id codecvt<unsigned short, char, mbstate_t>::id(0);

#define _Compiler_barrier() _STL_DISABLE_DEPRECATED_WARNING _ReadWriteBarrier() _STL_RESTORE_DEPRECATED_WARNING

#if defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC)
#define _Memory_barrier() __dmb(0xB) // inner shared data memory barrier
#define _Compiler_or_memory_barrier() _Memory_barrier()
#elif defined(_M_IX86) || defined(_M_X64)
// x86/x64 hardware only emits memory barriers inside _Interlocked intrinsics
#define _Compiler_or_memory_barrier() _Compiler_barrier()
#else // ^^^ x86/x64 / unsupported hardware vvv
#error Unsupported hardware
#endif // hardware
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

MattStephanson marked this conversation as resolved.
Show resolved Hide resolved
_MRTIMP2_PURE const locale& __CLRCALL_PURE_OR_CDECL locale::classic() { // get reference to "C" locale
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
_Init();
const auto mem = reinterpret_cast<const intptr_t*>(&locale::_Locimp::_Clocptr);
intptr_t as_bytes;
#ifdef _WIN64
#ifdef _M_ARM
as_bytes = __ldrexd(_Mem);
#else
as_bytes = __iso_volatile_load64(mem);
#endif
#else // ^^^ _WIN64 / !_WIN64 vvv
as_bytes = __iso_volatile_load32(mem);
#endif // ^^^ !_WIN64 ^^^
MattStephanson marked this conversation as resolved.
Show resolved Hide resolved
_Compiler_or_memory_barrier();
const auto ptr = reinterpret_cast<locale::_Locimp*>(as_bytes);
if (ptr == nullptr) {
_Init();
}

return classic_locale;
}

Expand All @@ -157,9 +185,17 @@ _MRTIMP2_PURE locale::_Locimp* __CLRCALL_PURE_OR_CDECL locale::_Init(bool _Do_in
ptr->_Catmask = all; // set current locale to "C"
ptr->_Name = "C";

_Locimp::_Clocptr = ptr; // set classic to match
_Locimp::_Clocptr->_Incref();
::new (&classic_locale) locale(_Locimp::_Clocptr);
// set classic to match
ptr->_Incref();
::new (&classic_locale) locale(ptr);
const auto mem = reinterpret_cast<volatile intptr_t*>(&locale::_Locimp::_Clocptr);
const auto as_bytes = reinterpret_cast<intptr_t>(ptr);
_Compiler_or_memory_barrier();
#ifdef _WIN64
__iso_volatile_store64(mem, as_bytes);
#else
__iso_volatile_store32(mem, as_bytes);
#endif
MattStephanson marked this conversation as resolved.
Show resolved Hide resolved
}

if (_Do_incref) {
Expand Down