File tree Expand file tree Collapse file tree 1 file changed +27
-6
lines changed
Expand file tree Collapse file tree 1 file changed +27
-6
lines changed Original file line number Diff line number Diff line change 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
4249#include < os/lock.h>
4350
4451class 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
4757public:
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
8593static_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
90101public:
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
You can’t perform that action at this time.
0 commit comments