aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/cxxsupp/libcxxmsvc/src/support/win32/atomic_win32.cpp
blob: 28cb0722ce7bb61219595d502d42906eea198d47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <intrin.h>
#include <cstdint>


_LIBCPP_BEGIN_NAMESPACE_STD

namespace {
static const int __msvc_locks_size = 1024;
volatile long __msvc_locks[__msvc_locks_size];

size_t __msvc_lock_hash(void* __p) {
    uintptr_t __num = reinterpret_cast<uintptr_t>(__p);
    return (__num ^ (__num >> 10)) & (__msvc_locks_size - 1);
}
}

void __msvc_lock(void* __p) {
    volatile long& __lock = __msvc_locks[__msvc_lock_hash(__p)];
    while (_InterlockedExchange(&__lock, 1) == 0) {
#if defined(_M_ARM) || defined(_M_ARM64)
        __yield();
#endif
    }
}

void __msvc_unlock(void* __p) {
    volatile long& __lock = __msvc_locks[__msvc_lock_hash(__p)];
    _InterlockedExchange(&__lock, 0);
}

_LIBCPP_END_NAMESPACE_STD