Debian Codegraph @

vendor/Heap-Layers/locks/spinlock.h

raw symbols SCIP
1 /* -*- C++ -*- */cxx . . $ `<file>/vendor/Heap-Layers/locks/spinlock.h`/
2
3 /*
4
5 Heap Layers: An Extensible Memory Allocation Infrastructure
6
7 Copyright (C) 2000-2020 by Emery Berger
8 http://www.emeryberger.com
9 [email protected]
10
11 Heap Layers is distributed under the terms of the Apache 2.0 license.
12
13 You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 */
17
18 #ifndef HL_SPINLOCK_H
19 #define HL_SPINLOCK_Hcxx . . $ `vendor/Heap-Layers/locks/spinlock.h:19:9`!
20
21 #if !defined(_WIN32) && ((__cpluspluscxx . . $ `<built-in>:449:9`! < 201103) || defined(__SUNPRO_CC)) // Still no support for atomic...
22 #include "spinlock-old.h"
23 #else
24
25 #include <atomic> // C++11
26
27 #if defined(unix)
28 #include <sched.h>
29 #endif
30
31 #if defined(__SVR4)
32 #include <thread.h>
33 #endif
34
35 #include "threads/cpuinfo.h"cxx . . $ `<file>/vendor/Heap-Layers/threads/cpuinfo.h`/
36
37 #if defined(_MSC_VER)
38
39 #if !defined(NO_INLINE)
40 #pragma inline_depth(255)
41 #define NO_INLINE __declspec(noinline)
42 #define INLINE __forceinline
43 #define inline __forceinline
44 #endif // !defined(NO_INLINE)
45
46 #else
47
48 #endif // defined(_MSC_VER)
49
50
51 #if defined(_WIN32) && !defined(_WIN64)
52
53 #ifndef _WIN32_WINNT
54 #define _WIN32_WINNT 0x0500
55 #endif
56
57 #endif
58
59 #define _MM_PAUSEcxx . . $ `vendor/Heap-Layers/locks/spinlock.h:59:9`!
60
61 // The use of the PAUSE instruction has now been disabled, as it can be insanely costly.
62 // See https://aloiskraus.wordpress.com/2018/06/16/why-skylakex-cpus-are-sometimes-50-slower-how-intel-has-broken-existing-code/amp
63
64 /*
65
66 #if defined(_WIN32) // includes WIN64
67
68 // NOTE: Below is the new "pause" instruction, which is inocuous for
69 // previous architectures, but crucial for Intel chips with
70 // hyperthreading. See
71 // http://www.usenix.org/events/wiess02/tech/full_papers/nakajima/nakajima.pdf
72 // for discussion.
73
74 #define _MM_PAUSE YieldProcessor() // {__asm{_emit 0xf3};__asm {_emit 0x90}}
75 #include <windows.h>
76
77 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
78
79 #define _MM_PAUSE { asm (".byte 0xf3; .byte 0x90" : : : "memory"); }
80
81 #else
82
83 #define _MM_PAUSE
84
85 #endif
86
87 */
88
89 namespace HLcxx . . $ HL/ {
90
91 class SpinLockTypecxx . . $ HL/SpinLockType# {
92 private:
93 stdcxx . . $ std/::atomiccxx . . $ std/atomic#<bool> _mutexcxx . . $ HL/SpinLockType#_mutex.;
94 public:
95
96 SpinLockTypecxx . . $ HL/SpinLockType#SpinLockType(49f6e7a06ebc5aa8).()
97 : _mutexcxx . . $ HL/SpinLockType#_mutex. (false)
98 {}
99
100 ~cxx . . $ HL/SpinLockType#`~SpinLockType`(49f6e7a06ebc5aa8).SpinLockTypecxx . . $ HL/SpinLockType#()
101 {}
102
103 inline void lockcxx . . $ HL/SpinLockType#lock(49f6e7a06ebc5aa8).() {
104 if (_mutexcxx . . $ HL/SpinLockType#_mutex..exchangecxx . . $ std/atomic#exchange(26b1389966461a07).(true)) {
105 contendedLockcxx . . $ HL/SpinLockType#contendedLock(49f6e7a06ebc5aa8).();
106 }
107 }
108
109 inline bool didLockcxx . . $ HL/SpinLockType#didLock(c9857a73d68e59ba).() {
110 return !_mutexcxx . . $ HL/SpinLockType#_mutex..exchangecxx . . $ std/atomic#exchange(26b1389966461a07).(true);
111 }
112
113 inline void unlockcxx . . $ HL/SpinLockType#unlock(49f6e7a06ebc5aa8).() {
114 _mutexcxx . . $ HL/SpinLockType#_mutex. =cxx . . $ std/atomic#`operator=`(efaadfb7dc14930a). false;
115 }
116
117 private:
118
119 NO_INLINEcxx . . $ `vendor/Heap-Layers/heaplayers.h:66:9`!
120 void contendedLockcxx . . $ HL/SpinLockType#contendedLock(49f6e7a06ebc5aa8).() {
121 const int MAX_SPIN = 1000;
122 while (true) {
123 if (!_mutexcxx . . $ HL/SpinLockType#_mutex..exchangecxx . . $ std/atomic#exchange(26b1389966461a07).(true)) {
124 return;
125 }
126 int count = 0;
127 while (_mutexcxx . . $ HL/SpinLockType#_mutex. && (count < MAX_SPIN)) {
128 _MM_PAUSEcxx . . $ `vendor/Heap-Layers/locks/spinlock.h:59:9`!;
129 count++;
130 }
131 if (count == MAX_SPIN) {
132 yieldProcessorcxx . . $ HL/SpinLockType#yieldProcessor(49f6e7a06ebc5aa8).();
133 }
134 }
135 }
136
137 // Is this system a multiprocessor?
138 inline bool onMultiprocessorcxx . . $ HL/SpinLockType#onMultiprocessor(c9857a73d68e59ba). (void) {
139 static CPUInfocxx . . $ HL/CPUInfo# cpuInfo;
140 return (cpuInfo.getNumProcessorscxx . . $ HL/CPUInfo#getNumProcessors(b126dc7c1de90089).() > 1);
141 }
142
143 inline void yieldProcessorcxx . . $ HL/SpinLockType#yieldProcessor(49f6e7a06ebc5aa8). (void) {
144 #if defined(_WIN32)
145 Sleep(0);
146 #elif defined(__SVR4)
147 thr_yield();
148 #else
149 sched_yield();
150 #endif
151 }
152
153 enumcxx . . $ HL/SpinLockType#$anonymous_type_0# { MAX_SPIN_LIMITcxx . . $ HL/SpinLockType#MAX_SPIN_LIMIT. = 1024 };
154 };
155
156 typedef SpinLockTypecxx . . $ HL/SpinLockType# SpinLockcxx . . $ HL/SpinLock#;
157
158 }
159
160 #endif
161
162 #endif // _SPINLOCK_H_