40#if (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))) \
41 || defined(__x86_64__) || defined(__i386__) || defined(__SSE2__)
43 #include <immintrin.h>
44 #define DSPARK_SPIN_PAUSE() _mm_pause()
45#elif defined(_MSC_VER) && defined(_M_ARM64)
48 #define DSPARK_SPIN_PAUSE() __yield()
49#elif defined(__aarch64__) || defined(__arm__) || defined(__ARM_NEON)
51 #define DSPARK_SPIN_PAUSE() __asm__ __volatile__("yield")
54 #define DSPARK_SPIN_PAUSE() ((void)0)
93 if (!flag_.test_and_set(std::memory_order_acquire)) {
98 while (flag_.test(std::memory_order_relaxed)) {
110 return !flag_.test_and_set(std::memory_order_acquire);
126 flag_.clear(std::memory_order_release);
169 : lock_(spinLock), acquired_(spinLock.tryLock()) {}
178 [[nodiscard]]
bool isLocked() const noexcept {
return acquired_; }
191 std::atomic_flag flag_{};
#define DSPARK_SPIN_PAUSE()
RAII wrapper that acquires the lock on construction and releases on destruction.
~ScopedLock() noexcept
Destructs the guard and releases the lock.
ScopedLock & operator=(const ScopedLock &)=delete
ScopedLock(SpinLock &spinLock) noexcept
Constructs the guard and blocks until the lock is acquired.
ScopedLock(const ScopedLock &)=delete
RAII wrapper that tries to acquire the lock without blocking.
ScopedTryLock & operator=(const ScopedTryLock &)=delete
~ScopedTryLock() noexcept
Destructs the guard and releases the lock ONLY if it was successfully acquired.
ScopedTryLock(const ScopedTryLock &)=delete
bool isLocked() const noexcept
Queries whether the lock acquisition was successful.
ScopedTryLock(SpinLock &spinLock) noexcept
Constructs the guard and attempts a single lock acquisition.
A minimal, real-time safe spin lock with a TTAS wait loop.
bool tryLock() noexcept
Attempts to acquire the lock without waiting.
void unlock() noexcept
Releases the lock, restoring it to the clear state.
SpinLock() noexcept=default
Constructs an unlocked SpinLock.
bool try_lock() noexcept
Standard-library spelling of tryLock().
void lock() noexcept
Acquires the lock, spinning until successful.
Main namespace for the DSPark framework.