From 3c77f1bcd104e901ba722761c668cc775bbaca81 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sun, 18 Feb 2024 13:16:46 +0200 Subject: [PATCH 1/3] `SRWLOCK` for `_Cnd_register_at_thread_exit` --- stl/inc/yvals.h | 1 - stl/src/xlock.cpp | 12 +----------- stl/src/xnotify.cpp | 15 ++++++--------- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/stl/inc/yvals.h b/stl/inc/yvals.h index 842efae279..1b44cbced3 100644 --- a/stl/inc/yvals.h +++ b/stl/inc/yvals.h @@ -313,7 +313,6 @@ _EMIT_STL_WARNING(STL4001, "/clr:pure is deprecated and will be REMOVED."); #define _LOCK_MALLOC 1 #define _LOCK_STREAM 2 #define _LOCK_DEBUG 3 -#define _LOCK_AT_THREAD_EXIT 4 #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 226f8229f1..520c4d6b39 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 = 4; // must be power of two #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 1b6e8fcbdb..96aebb485b 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" From 6675cdb3e32611e209c2de29d728a2025c141d8c Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sun, 18 Feb 2024 13:25:00 +0200 Subject: [PATCH 2/3] format --- stl/inc/yvals.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stl/inc/yvals.h b/stl/inc/yvals.h index 1b44cbced3..a5d63a979c 100644 --- a/stl/inc/yvals.h +++ b/stl/inc/yvals.h @@ -309,10 +309,10 @@ _EMIT_STL_WARNING(STL4001, "/clr:pure is deprecated and will be REMOVED."); #endif #endif // !defined(_CRTDATA2_IMPORT) -#define _LOCK_LOCALE 0 -#define _LOCK_MALLOC 1 -#define _LOCK_STREAM 2 -#define _LOCK_DEBUG 3 +#define _LOCK_LOCALE 0 +#define _LOCK_MALLOC 1 +#define _LOCK_STREAM 2 +#define _LOCK_DEBUG 3 #ifndef _STD_ATOMIC_ALWAYS_USE_CMPXCHG16B #if _STL_WIN32_WINNT >= _STL_WIN32_WINNT_WINBLUE && defined(_WIN64) From 1a63a45dc68f516069f247a001ba261c19b90b5b Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Mon, 19 Feb 2024 09:53:48 +0200 Subject: [PATCH 3/3] revert size of table reduction --- stl/src/xlock.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/src/xlock.cpp b/stl/src/xlock.cpp index 520c4d6b39..1aea80d1e2 100644 --- a/stl/src/xlock.cpp +++ b/stl/src/xlock.cpp @@ -13,7 +13,7 @@ _STD_BEGIN -constexpr int _Max_lock = 4; // 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)