diff --git a/stl/inc/yvals.h b/stl/inc/yvals.h index d3b61ec03af..9428b35aaeb 100644 --- a/stl/inc/yvals.h +++ b/stl/inc/yvals.h @@ -309,10 +309,9 @@ _EMIT_STL_WARNING(STL4001, "/clr:pure is deprecated and will be REMOVED."); #endif #endif // !defined(_CRTDATA2_IMPORT) -#define _LOCK_LOCALE 0 -#define _LOCK_STREAM 2 -#define _LOCK_DEBUG 3 -#define _LOCK_AT_THREAD_EXIT 4 +#define _LOCK_LOCALE 0 +#define _LOCK_STREAM 2 +#define _LOCK_DEBUG 3 #ifndef _STD_ATOMIC_ALWAYS_USE_CMPXCHG16B #if _STL_WIN32_WINNT >= _STL_WIN32_WINNT_WINBLUE && defined(_WIN64) diff --git a/stl/src/xlock.cpp b/stl/src/xlock.cpp index 226f8229f1c..1aea80d1e2d 100644 --- a/stl/src/xlock.cpp +++ b/stl/src/xlock.cpp @@ -13,7 +13,7 @@ _STD_BEGIN -constexpr int _Max_lock = 8; // must be power of two +constexpr int _Max_lock = 8; // must be power of two; TRANSITION, ABI: may be less now #pragma warning(disable : 4074) #pragma init_seg(compiler) @@ -121,14 +121,4 @@ void __cdecl _Lockit::_Lockit_dtor(int kind) noexcept { // unlock the mutex _Mtxunlock(&mtx[kind & (_Max_lock - 1)]); } } - -extern "C" { -void _Lock_at_thread_exit_mutex() noexcept { // lock the at-thread-exit mutex - _Mtxlock(&mtx[_LOCK_AT_THREAD_EXIT]); -} -void _Unlock_at_thread_exit_mutex() noexcept { // unlock the at-thread-exit mutex - _Mtxunlock(&mtx[_LOCK_AT_THREAD_EXIT]); -} -} // extern "C" - _STD_END diff --git a/stl/src/xnotify.cpp b/stl/src/xnotify.cpp index 1b6e8fcbdbf..96aebb485b2 100644 --- a/stl/src/xnotify.cpp +++ b/stl/src/xnotify.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include +#include #include #include @@ -23,20 +24,19 @@ namespace { }; _At_thread_exit_block _Thread_exit_data; + + constinit std::mutex _Thread_exit_data_mutex; } // unnamed namespace extern "C" { -void _Lock_at_thread_exit_mutex() noexcept; -void _Unlock_at_thread_exit_mutex() noexcept; - _CRTIMP2_PURE void __cdecl _Cnd_register_at_thread_exit(_Cnd_t cnd, _Mtx_t mtx, int* p) noexcept { // register condition variable and mutex for cleanup at thread exit // find block with available space _At_thread_exit_block* block = &_Thread_exit_data; - _Lock_at_thread_exit_mutex(); + std::lock_guard _Lock{_Thread_exit_data_mutex}; while (block != nullptr) { // loop through list of blocks if (block->num_used == _Nitems) { // block is full; move to next block and allocate if (block->next == nullptr) { @@ -58,7 +58,6 @@ _CRTIMP2_PURE void __cdecl _Cnd_register_at_thread_exit(_Cnd_t cnd, _Mtx_t mtx, block = nullptr; } } - _Unlock_at_thread_exit_mutex(); } _CRTIMP2_PURE void __cdecl _Cnd_unregister_at_thread_exit(_Mtx_t mtx) noexcept { @@ -67,7 +66,7 @@ _CRTIMP2_PURE void __cdecl _Cnd_unregister_at_thread_exit(_Mtx_t mtx) noexcept { // find condition variables waiting for this thread to exit _At_thread_exit_block* block = &_Thread_exit_data; - _Lock_at_thread_exit_mutex(); + std::lock_guard _Lock{_Thread_exit_data_mutex}; while (block != nullptr) { // loop through list of blocks for (int i = 0; block->num_used != 0 && i < _Nitems; ++i) { if (block->data[i].mtx == mtx) { // release slot @@ -78,7 +77,6 @@ _CRTIMP2_PURE void __cdecl _Cnd_unregister_at_thread_exit(_Mtx_t mtx) noexcept { block = block->next; } - _Unlock_at_thread_exit_mutex(); } _CRTIMP2_PURE void __cdecl _Cnd_do_broadcast_at_thread_exit() noexcept { @@ -88,7 +86,7 @@ _CRTIMP2_PURE void __cdecl _Cnd_do_broadcast_at_thread_exit() noexcept { _At_thread_exit_block* block = &_Thread_exit_data; const unsigned int currentThreadId = _Thrd_id(); - _Lock_at_thread_exit_mutex(); + std::lock_guard _Lock{_Thread_exit_data_mutex}; while (block != nullptr) { // loop through list of blocks for (int i = 0; block->num_used != 0 && i < _Nitems; ++i) { if (block->data[i].mtx != nullptr && block->data[i].id._Id == currentThreadId) { // notify and release slot @@ -104,7 +102,6 @@ _CRTIMP2_PURE void __cdecl _Cnd_do_broadcast_at_thread_exit() noexcept { block = block->next; } - _Unlock_at_thread_exit_mutex(); } } // extern "C"