Skip to content

Commit

Permalink
<new>: SRWLOCK to protect the new handler instead of an indexed…
Browse files Browse the repository at this point in the history
… `CRITICAL_SECTION` (#4407)

Co-authored-by: Stephan T. Lavavej <stl@nuwen.net>
  • Loading branch information
AlexGuteniev and StephanTLavavej authored Feb 27, 2024
1 parent e57fc4f commit 88fe449
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
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

0 comments on commit 88fe449

Please sign in to comment.