Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGuteniev committed Aug 16, 2020
1 parent e7451b3 commit 918bb10
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions stl/src/atomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,21 @@
// implement shared_ptr spin lock

#include <yvals.h>
#include <synchapi.h>

#include <intrin.h>
#pragma warning(disable : 4793)
namespace {
// MUTEX FOR shared_ptr ATOMIC OPERATIONS
SRWLOCK _Shared_ptr_lock = SRWLOCK_INIT;
} // unnamed namespace

_EXTERN_C

// SPIN LOCK FOR shared_ptr ATOMIC OPERATIONS
volatile long _Shared_ptr_flag;

_CRTIMP2_PURE void __cdecl _Lock_shared_ptr_spin_lock() { // spin until _Shared_ptr_flag successfully set
#ifdef _M_ARM
while (_InterlockedExchange_acq(&_Shared_ptr_flag, 1)) {
__yield();
}
#else // _M_ARM
while (_interlockedbittestandset(&_Shared_ptr_flag, 0)) { // set bit 0
}
#endif // _M_ARM
AcquireSRWLockExclusive(&_Shared_ptr_lock);
}

_CRTIMP2_PURE void __cdecl _Unlock_shared_ptr_spin_lock() { // release previously obtained lock
#ifdef _M_ARM
__dmb(_ARM_BARRIER_ISH);
__iso_volatile_store32(reinterpret_cast<volatile int*>(&_Shared_ptr_flag), 0);
#else // _M_ARM
_interlockedbittestandreset(&_Shared_ptr_flag, 0); // reset bit 0
#endif // _M_ARM
ReleaseSRWLockExclusive(&_Shared_ptr_lock);
}

_END_EXTERN_C

0 comments on commit 918bb10

Please sign in to comment.