| 1 |
/* -*- C++ -*- */ |
| 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_H |
| 20 |
|
| 21 |
#if defined(unix) |
| 22 |
#include <sched.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#if defined(__SVR4) |
| 26 |
#include <thread.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#if defined(__sgi) |
| 30 |
#include <mutex.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#if defined(__APPLE__) |
| 34 |
#include <libkern/OSAtomic.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "threads/cpuinfo.h" |
| 38 |
|
| 39 |
#if defined(_MSC_VER) |
| 40 |
|
| 41 |
#if !defined(NO_INLINE) |
| 42 |
#pragma inline_depth(255) |
| 43 |
#define NO_INLINE __declspec(noinline) |
| 44 |
#define INLINE __forceinline |
| 45 |
#define inline __forceinline |
| 46 |
#endif // !defined(NO_INLINE) |
| 47 |
|
| 48 |
#else |
| 49 |
|
| 50 |
#endif // defined(_MSC_VER) |
| 51 |
|
| 52 |
|
| 53 |
#if defined(__SUNPRO_CC) |
| 54 |
// x86-interchange.il, x86_64-interchange.il contributed by Markus Bernhardt. |
| 55 |
extern "C" size_t MyInterlockedExchange (size_t * oldval, |
| 56 |
size_t newval); |
| 57 |
#endif |
| 58 |
|
| 59 |
#if defined(_WIN32) && !defined(_WIN64) |
| 60 |
|
| 61 |
#ifndef _WIN32_WINNT |
| 62 |
#define _WIN32_WINNT 0x0500 |
| 63 |
#endif |
| 64 |
|
| 65 |
// NOTE: Below is the new "pause" instruction, which is inocuous for |
| 66 |
// previous architectures, but crucial for Intel chips with |
| 67 |
// hyperthreading. See |
| 68 |
// http://www.usenix.org/events/wiess02/tech/full_papers/nakajima/nakajima.pdf |
| 69 |
// for discussion. |
| 70 |
// On PowerPC `or Rx Rx Rx` is a nop, but may have extra effects like setting |
| 71 |
// priority etc. `or r27 r27 r27` is an equivanent of "pause", and in ISA 2.06+ |
| 72 |
// has an alias `yield`. For examples of usage, see Boost libfiber, catch2, seqan3. |
| 73 |
// https://stackoverflow.com/questions/5425506/equivalent-of-x86-pause-instruction-for-ppc |
| 74 |
// https://utcc.utoronto.ca/~cks/space/blog/tech/PowerPCInstructionOddity?showcomments |
| 75 |
|
| 76 |
#define _MM_PAUSE {__asm{_emit 0xf3};__asm {_emit 0x90}} |
| 77 |
#include <windows.h> |
| 78 |
|
| 79 |
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) |
| 80 |
|
| 81 |
#define _MM_PAUSE { asm (".byte 0xf3; .byte 0x90" : : : "memory"); } |
| 82 |
|
| 83 |
#elif defined(__POWERPC__) |
| 84 |
|
| 85 |
#define _MM_PAUSE { __asm__ volatile ("or r27,r27,r27"); } |
| 86 |
|
| 87 |
#else |
| 88 |
|
| 89 |
#define _MM_PAUSE |
| 90 |
|
| 91 |
#endif // defined(_WIN32) && !defined(_WIN64) |
| 92 |
|
| 93 |
//extern volatile bool anyThreadCreated; |
| 94 |
|
| 95 |
namespace HL { |
| 96 |
|
| 97 |
class SpinLockType { |
| 98 |
private: |
| 99 |
|
| 100 |
enum { UNLOCKED = 0, LOCKED = 1 }; |
| 101 |
|
| 102 |
public: |
| 103 |
|
| 104 |
SpinLockType (void) |
| 105 |
#if defined(__APPLE__) |
| 106 |
: mutex (OS_SPINLOCK_INIT) |
| 107 |
#else |
| 108 |
: mutex (UNLOCKED) |
| 109 |
#endif |
| 110 |
{} |
| 111 |
|
| 112 |
~SpinLockType() |
| 113 |
{} |
| 114 |
|
| 115 |
inline void lock() { |
| 116 |
if (true) { // anyThreadCreated) { |
| 117 |
if (MyInterlockedExchange (const_cast<size_t *>(&mutex), LOCKED) |
| 118 |
!= UNLOCKED) { |
| 119 |
contendedLock(); |
| 120 |
} |
| 121 |
} else { |
| 122 |
mutex = LOCKED; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
inline void unlock() { |
| 129 |
if (true) { // anyThreadCreated) { |
| 130 |
#if defined(_WIN32) && !defined(_WIN64) |
| 131 |
__asm {} |
| 132 |
#elif defined(__GNUC__) |
| 133 |
asm volatile ("" : : : "memory"); |
| 134 |
#endif |
| 135 |
} |
| 136 |
mutex = UNLOCKED; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
#if !defined(__SUNPRO_CC) |
| 141 |
inline static size_t MyInterlockedExchange (size_t *, size_t); |
| 142 |
#endif |
| 143 |
|
| 144 |
private: |
| 145 |
|
| 146 |
#if 0 // defined(__APPLE__) |
| 147 |
OSSpinLock mutex; |
| 148 |
|
| 149 |
#else |
| 150 |
|
| 151 |
NO_INLINE |
| 152 |
void contendedLock() { |
| 153 |
const int MAX_SPIN = 1000; |
| 154 |
while (true) { |
| 155 |
if (MyInterlockedExchange (const_cast<size_t *>(&mutex), LOCKED) |
| 156 |
== UNLOCKED) { |
| 157 |
return; |
| 158 |
} |
| 159 |
int count = 0; |
| 160 |
while ((mutex == LOCKED) && (count < MAX_SPIN)) { |
| 161 |
_MM_PAUSE; |
| 162 |
count++; |
| 163 |
} |
| 164 |
if (count == MAX_SPIN) { |
| 165 |
yieldProcessor(); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
// Is this system a multiprocessor? |
| 171 |
inline bool onMultiprocessor (void) { |
| 172 |
static CPUInfo cpuInfo; |
| 173 |
return (cpuInfo.getNumProcessors() > 1); |
| 174 |
} |
| 175 |
|
| 176 |
inline void yieldProcessor (void) { |
| 177 |
#if defined(_WIN32) |
| 178 |
Sleep(0); |
| 179 |
#elif defined(__SVR4) |
| 180 |
thr_yield(); |
| 181 |
#else |
| 182 |
sched_yield(); |
| 183 |
#endif |
| 184 |
} |
| 185 |
|
| 186 |
enum { MAX_SPIN_LIMIT = 1024 }; |
| 187 |
|
| 188 |
volatile size_t mutex; |
| 189 |
#endif |
| 190 |
|
| 191 |
}; |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
// Atomically: |
| 196 |
// retval = *oldval; |
| 197 |
// *oldval = newval; |
| 198 |
// return retval; |
| 199 |
|
| 200 |
#if !defined(__SUNPRO_CC) // && !defined(__APPLE__) |
| 201 |
inline size_t |
| 202 |
HL::SpinLockType::MyInterlockedExchange (size_t * oldval, |
| 203 |
size_t newval) |
| 204 |
{ |
| 205 |
#if defined(_WIN32) && defined(_MSC_VER) |
| 206 |
return InterlockedExchange ((volatile LONG *) oldval, newval); |
| 207 |
|
| 208 |
//#elif (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100 |
| 209 |
// return __sync_val_compare_and_swap(oldval, *oldval, newval); |
| 210 |
|
| 211 |
#elif defined(__sparc) |
| 212 |
asm volatile ("swap [%1],%0" |
| 213 |
:"=r" (newval) |
| 214 |
:"r" (oldval), "0" (newval) |
| 215 |
: "memory"); |
| 216 |
|
| 217 |
#elif defined(__i386__) |
| 218 |
asm volatile ("lock; xchgl %0, %1" |
| 219 |
: "=r" (newval) |
| 220 |
: "m" (*oldval), "0" (newval) |
| 221 |
: "memory"); |
| 222 |
|
| 223 |
#elif defined(__sgi) |
| 224 |
newval = test_and_set (oldval, newval); |
| 225 |
|
| 226 |
#elif defined(__x86_64__) |
| 227 |
// Contributed by Kurt Roeckx. |
| 228 |
asm volatile ("lock; xchgq %0, %1" |
| 229 |
: "=r" (newval) |
| 230 |
: "m" (*oldval), "0" (newval) |
| 231 |
: "memory"); |
| 232 |
|
| 233 |
#elif defined(__ppc) || defined(__powerpc__) || defined(PPC) || defined(__POWERPC__) |
| 234 |
// PPC assembly contributed by Maged Michael. |
| 235 |
int ret; |
| 236 |
asm volatile ( |
| 237 |
"La..%=0: lwarx %0,0,%1 ;" |
| 238 |
" cmpw %0,%2;" |
| 239 |
" beq La..%=1;" |
| 240 |
" stwcx. %2,0,%1;" |
| 241 |
" bne- La..%=0;" |
| 242 |
"La..%=1: isync;" |
| 243 |
: "=&r"(ret) |
| 244 |
: "r"(oldval), "r"(newval) |
| 245 |
: "cr0", "memory"); |
| 246 |
return ret; |
| 247 |
|
| 248 |
#elif defined(__arm__) |
| 249 |
// Contributed by Bo Granlund. |
| 250 |
long result; |
| 251 |
asm volatile ( |
| 252 |
"\n\t" |
| 253 |
"swp %0,%2,[%1] \n\t" |
| 254 |
"" |
| 255 |
: "=&r"(result) |
| 256 |
: "r"(oldval), "r"(newval) |
| 257 |
: "memory"); |
| 258 |
return (result); |
| 259 |
#elif defined(__APPLE__) |
| 260 |
size_t oldValue = *oldval; |
| 261 |
bool swapped = OSAtomicCompareAndSwapLongBarrier (oldValue, newval, (volatile long *) oldval); |
| 262 |
if (swapped) { |
| 263 |
return newval; |
| 264 |
} else { |
| 265 |
return oldValue; |
| 266 |
} |
| 267 |
#else |
| 268 |
#error "No spin lock implementation is available for this platform." |
| 269 |
#endif |
| 270 |
return newval; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
#endif |
| 275 |
|
| 276 |
#endif // _SPINLOCK_H_ |