-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathxlock.cpp
131 lines (104 loc) · 3.17 KB
/
xlock.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// global lock for locales, etc.
#include <yvals.h>
#include "xmtx.h"
#include <locale.h>
#include <stdlib.h>
_STD_BEGIN
#define MAX_LOCK 8 // must be power of two
#pragma warning(disable : 4074)
#pragma init_seg(compiler)
static _Rmtx mtx[MAX_LOCK];
static long init = -1;
#if !defined(MRTDLL)
__thiscall _Init_locks::_Init_locks() noexcept { // initialize locks
if (InterlockedIncrement(&init) == 0) {
for (auto& elem : mtx) {
_Mtxinit(&elem);
}
}
}
__thiscall _Init_locks::~_Init_locks() noexcept { // clean up locks
if (InterlockedDecrement(&init) < 0) {
for (auto& elem : mtx) {
_Mtxdst(&elem);
}
}
}
#endif
void __cdecl _Init_locks::_Init_locks_ctor(_Init_locks*) noexcept { // initialize locks
if (InterlockedIncrement(&init) == 0) {
for (auto& elem : mtx) {
_Mtxinit(&elem);
}
}
}
void __cdecl _Init_locks::_Init_locks_dtor(_Init_locks*) noexcept { // clean up locks
if (InterlockedDecrement(&init) < 0) {
for (auto& elem : mtx) {
_Mtxdst(&elem);
}
}
}
static _Init_locks initlocks;
#if !defined(MRTDLL)
__thiscall _Lockit::_Lockit() noexcept : _Locktype(0) { // lock default mutex
if (_Locktype == _LOCK_LOCALE) {
_lock_locales();
} else {
_Mtxlock(&mtx[0]);
}
}
__thiscall _Lockit::_Lockit(int kind) noexcept : _Locktype(kind) { // lock the mutex
if (_Locktype == _LOCK_LOCALE) {
_lock_locales();
} else if (_Locktype < MAX_LOCK) {
_Mtxlock(&mtx[_Locktype]);
}
}
__thiscall _Lockit::~_Lockit() noexcept { // unlock the mutex
if (_Locktype == _LOCK_LOCALE) {
_unlock_locales();
} else if (_Locktype < MAX_LOCK) {
_Mtxunlock(&mtx[_Locktype]);
}
}
#endif
void __cdecl _Lockit::_Lockit_ctor(_Lockit*) noexcept { // lock default mutex
_Mtxlock(&mtx[0]);
}
void __cdecl _Lockit::_Lockit_ctor(_Lockit* _This, int kind) noexcept { // lock the mutex
if (kind == _LOCK_LOCALE) {
_lock_locales();
} else {
_This->_Locktype = kind & (MAX_LOCK - 1);
_Mtxlock(&mtx[_This->_Locktype]);
}
}
void __cdecl _Lockit::_Lockit_dtor(_Lockit* _This) noexcept { // unlock the mutex
_Mtxunlock(&mtx[_This->_Locktype]);
}
_RELIABILITY_CONTRACT
void __cdecl _Lockit::_Lockit_ctor(int kind) noexcept { // lock the mutex
if (kind == _LOCK_LOCALE) {
_lock_locales();
} else {
_Mtxlock(&mtx[kind & (MAX_LOCK - 1)]);
}
}
_RELIABILITY_CONTRACT
void __cdecl _Lockit::_Lockit_dtor(int kind) noexcept { // unlock the mutex
if (kind == _LOCK_LOCALE) {
_unlock_locales();
} else {
_Mtxunlock(&mtx[kind & (MAX_LOCK - 1)]);
}
}
extern "C" void _Lock_at_thread_exit_mutex() { // lock the at-thread-exit mutex
_Mtxlock(&mtx[_LOCK_AT_THREAD_EXIT]);
}
extern "C" void _Unlock_at_thread_exit_mutex() { // unlock the at-thread-exit mutex
_Mtxunlock(&mtx[_LOCK_AT_THREAD_EXIT]);
}
_STD_END