Skip to content

Commit

Permalink
Enhance SpinLock
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomShaper committed Oct 21, 2024
1 parent 44fa552 commit f545d40
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
26 changes: 21 additions & 5 deletions core/os/spin_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,35 @@ class SpinLock {

#else

#include "core/os/thread.h"

#include <atomic>
#include <thread>

class SpinLock {
mutable std::atomic_flag locked = ATOMIC_FLAG_INIT;
static_assert(std::atomic_bool::is_always_lock_free);

class alignas(Thread::CACHE_LINE_BYTES) SpinLock {
mutable std::atomic_bool locked = ATOMIC_VAR_INIT(false);

public:
_ALWAYS_INLINE_ void lock() const {
while (locked.test_and_set(std::memory_order_acquire)) {
// Continue.
while (true) {
bool expected = false;
if (locked.compare_exchange_weak(expected, true, std::memory_order_acq_rel, std::memory_order_relaxed)) {
break;
}
do {
std::this_thread::yield();
} while (locked.load(std::memory_order_relaxed));
}
}

_ALWAYS_INLINE_ void unlock() const {
locked.clear(std::memory_order_release);
locked.store(false, std::memory_order_release);
}

_ALWAYS_INLINE_ void acquire() const {
(void)locked.load(std::memory_order_acquire);
}
};

Check failure on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

the following warning is treated as an error

Check warning on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'SpinLock': structure was padded due to alignment specifier

Check failure on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

the following warning is treated as an error

Check warning on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'SpinLock': structure was padded due to alignment specifier

Check failure on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

the following warning is treated as an error

Check failure on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

the following warning is treated as an error

Check failure on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

the following warning is treated as an error

Check warning on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'SpinLock': structure was padded due to alignment specifier

Check failure on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

the following warning is treated as an error

Check warning on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'SpinLock': structure was padded due to alignment specifier

Check failure on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

the following warning is treated as an error

Check failure on line 85 in core/os/spin_lock.h

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

the following warning is treated as an error

Expand Down
8 changes: 8 additions & 0 deletions core/os/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include "core/templates/safe_refcount.h"
#include "core/typedefs.h"

#include <new>

#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.thread.h"
Expand Down Expand Up @@ -85,6 +87,12 @@ class Thread {
void (*term)() = nullptr;
};

#ifdef __cpp_lib_hardware_interference_size
static const size_t CACHE_LINE_BYTES = std::hardware_constructive_interference_size;
#else
static const size_t CACHE_LINE_BYTES = 64;
#endif

private:
friend class Main;

Expand Down

0 comments on commit f545d40

Please sign in to comment.