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

<new>: SRWLOCK to protect the new handler instead of an indexed CRITICAL_SECTION #4407

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion stl/inc/yvals.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ _EMIT_STL_WARNING(STL4001, "/clr:pure is deprecated and will be REMOVED.");
#endif // !defined(_CRTDATA2_IMPORT)

#define _LOCK_LOCALE 0
#define _LOCK_MALLOC 1
#define _LOCK_STREAM 2
#define _LOCK_DEBUG 3
#define _LOCK_AT_THREAD_EXIT 4
Expand Down
8 changes: 4 additions & 4 deletions stl/src/stdhndlr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

// set_new_handler

#include <mutex>
#include <new.h>
#include <new>

namespace {
_STD new_handler _New_handler;
constinit _STD mutex _New_handler_mutex;

int __cdecl _New_handler_interface(size_t) { // interface to existing Microsoft _callnewh mechanism
_New_handler();
Expand All @@ -18,18 +20,16 @@ namespace {
_STD_BEGIN

_CRTIMP2 new_handler __cdecl set_new_handler(_In_opt_ new_handler pnew) noexcept { // remove current handler
_BEGIN_LOCK(_LOCK_MALLOC) // lock thread to ensure atomicity
lock_guard _Lock{_New_handler_mutex};
new_handler pold = _New_handler;
_New_handler = pnew;
_set_new_handler(pnew ? _New_handler_interface : nullptr);
return pold;
_END_LOCK()
}

_CRTIMP2 new_handler __cdecl get_new_handler() noexcept { // get current new handler
_BEGIN_LOCK(_LOCK_MALLOC) // lock thread to ensure atomicity
lock_guard _Lock{_New_handler_mutex};
return _New_handler;
_END_LOCK()
}

_STD_END