You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-05 12:10:55 +00:00
SpinLock: Overhaul false sharing avoidance
This commit is contained in:
@@ -31,8 +31,15 @@
|
|||||||
#ifndef SPIN_LOCK_H
|
#ifndef SPIN_LOCK_H
|
||||||
#define SPIN_LOCK_H
|
#define SPIN_LOCK_H
|
||||||
|
|
||||||
|
#include "core/os/thread.h"
|
||||||
#include "core/typedefs.h"
|
#include "core/typedefs.h"
|
||||||
|
|
||||||
|
#ifdef THREADS_ENABLED
|
||||||
|
|
||||||
|
// Note the implementations below avoid false sharing by ensuring their
|
||||||
|
// sizes match the assumed cache line. We can't use align attributes
|
||||||
|
// because these objects may end up unaligned in semi-tightly packed arrays.
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#include <intrin.h>
|
#include <intrin.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -42,7 +49,10 @@
|
|||||||
#include <os/lock.h>
|
#include <os/lock.h>
|
||||||
|
|
||||||
class SpinLock {
|
class SpinLock {
|
||||||
|
union {
|
||||||
mutable os_unfair_lock _lock = OS_UNFAIR_LOCK_INIT;
|
mutable os_unfair_lock _lock = OS_UNFAIR_LOCK_INIT;
|
||||||
|
char aligner[Thread::CACHE_LINE_BYTES];
|
||||||
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
_ALWAYS_INLINE_ void lock() const {
|
_ALWAYS_INLINE_ void lock() const {
|
||||||
@@ -54,9 +64,7 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#else
|
#else // __APPLE__
|
||||||
|
|
||||||
#include "core/os/thread.h"
|
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
||||||
@@ -84,8 +92,11 @@ _ALWAYS_INLINE_ static void _cpu_pause() {
|
|||||||
|
|
||||||
static_assert(std::atomic_bool::is_always_lock_free);
|
static_assert(std::atomic_bool::is_always_lock_free);
|
||||||
|
|
||||||
class alignas(Thread::CACHE_LINE_BYTES) SpinLock {
|
class SpinLock {
|
||||||
|
union {
|
||||||
mutable std::atomic<bool> locked = ATOMIC_VAR_INIT(false);
|
mutable std::atomic<bool> locked = ATOMIC_VAR_INIT(false);
|
||||||
|
char aligner[Thread::CACHE_LINE_BYTES];
|
||||||
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
_ALWAYS_INLINE_ void lock() const {
|
_ALWAYS_INLINE_ void lock() const {
|
||||||
@@ -107,4 +118,14 @@ public:
|
|||||||
|
|
||||||
#endif // __APPLE__
|
#endif // __APPLE__
|
||||||
|
|
||||||
|
#else // THREADS_ENABLED
|
||||||
|
|
||||||
|
class SpinLock {
|
||||||
|
public:
|
||||||
|
void lock() const {}
|
||||||
|
void unlock() const {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // THREADS_ENABLED
|
||||||
|
|
||||||
#endif // SPIN_LOCK_H
|
#endif // SPIN_LOCK_H
|
||||||
|
|||||||
Reference in New Issue
Block a user