2#include <ossia/detail/config.hpp>
4#include <ossia/detail/mutex.hpp>
12#if defined(__EMSCRIPTEN__)
14#define ossia_rwlock_pause()
15#elif defined(__x86_64__) || defined(_M_X64)
17#define ossia_rwlock_pause() _mm_pause()
18#elif defined(_M_ARM64)
20#define ossia_rwlock_pause() __yield()
21#elif defined(__i386__)
22#define ossia_rwlock_pause() __asm__ __volatile__("rep; nop")
23#elif defined(__ia64__)
24#define ossia_rwlock_pause() __asm__ __volatile__("hint @pause")
26#define ossia_rwlock_pause() __asm__ __volatile__("yield")
27#elif defined(__sparc) || defined(__sparc__)
28#define ossia_rwlock_pause() __asm__ __volatile__("pause")
29#elif defined(__ppc__) || defined(_ARCH_PPC) || defined(_ARCH_PWR) \
30 || defined(_ARCH_PWR2) || defined(_POWER)
31#define ossia_rwlock_pause() __asm__ volatile("or 27,27,27")
32#elif defined(_MSC_VER)
34#define ossia_rwlock_pause() YieldProcessor()
36#define ossia_rwlock_pause()
44struct TS_CAPABILITY(
"mutex") audio_spin_mutex
46 void lock() noexcept TS_ACQUIRE()
50 constexpr std::array iterations = {5, 10, 3000};
52 for(
int i = 0; i < iterations[0]; ++i)
58 for(
int i = 0; i < iterations[1]; ++i)
68 for(
int i = 0; i < iterations[2]; ++i)
87 std::this_thread::yield();
91 bool try_lock() TS_TRY_ACQUIRE(true)
93 return !locked.load(std::memory_order_relaxed)
94 && !locked.exchange(
true, std::memory_order_acquire);
97 void unlock() TS_RELEASE() { locked.store(
false, std::memory_order_release); }
99 const auto& operator!()
const {
return *
this; }
102 std::atomic<bool> locked{
false};