Skip to content

Commit a5caeeb

Browse files
committed
SpinLock: Overhaul false sharing avoidance
1 parent cb411fa commit a5caeeb

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

core/os/spin_lock.h

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@
3131
#ifndef SPIN_LOCK_H
3232
#define SPIN_LOCK_H
3333

34+
#include "core/os/thread.h"
3435
#include "core/typedefs.h"
3536

37+
#ifdef THREADS_ENABLED
38+
39+
// Note the implementations below avoid false sharing by ensuring their
40+
// sizes match the assumed cache line. We can't use align attributes
41+
// because these objects may end up unaligned in semi-tightly packed arrays.
42+
3643
#ifdef _MSC_VER
3744
#include <intrin.h>
3845
#endif
@@ -42,7 +49,10 @@
4249
#include <os/lock.h>
4350

4451
class SpinLock {
45-
mutable os_unfair_lock _lock = OS_UNFAIR_LOCK_INIT;
52+
union {
53+
mutable os_unfair_lock _lock = OS_UNFAIR_LOCK_INIT;
54+
char aligner[Thread::CACHE_LINE_BYTES];
55+
};
4656

4757
public:
4858
_ALWAYS_INLINE_ void lock() const {
@@ -54,9 +64,7 @@ class SpinLock {
5464
}
5565
};
5666

57-
#else
58-
59-
#include "core/os/thread.h"
67+
#else // __APPLE__
6068

6169
#include <atomic>
6270

@@ -84,8 +92,11 @@ _ALWAYS_INLINE_ static void _cpu_pause() {
8492

8593
static_assert(std::atomic_bool::is_always_lock_free);
8694

87-
class alignas(Thread::CACHE_LINE_BYTES) SpinLock {
88-
mutable std::atomic<bool> locked = ATOMIC_VAR_INIT(false);
95+
class SpinLock {
96+
union {
97+
mutable std::atomic<bool> locked = ATOMIC_VAR_INIT(false);
98+
char aligner[Thread::CACHE_LINE_BYTES];
99+
};
89100

90101
public:
91102
_ALWAYS_INLINE_ void lock() const {
@@ -107,4 +118,14 @@ class alignas(Thread::CACHE_LINE_BYTES) SpinLock {
107118

108119
#endif // __APPLE__
109120

121+
#else // THREADS_ENABLED
122+
123+
class SpinLock {
124+
public:
125+
void lock() const {}
126+
void unlock() const {}
127+
};
128+
129+
#endif // THREADS_ENABLED
130+
110131
#endif // SPIN_LOCK_H

0 commit comments

Comments
 (0)