From e792f9fc194d6cf5918d08e74cf5277c32905f89 Mon Sep 17 00:00:00 2001 From: KRM7 <70973547+KRM7@users.noreply.github.com> Date: Thu, 28 Mar 2024 23:09:54 +0100 Subject: [PATCH] Update spinlock.hpp --- src/utility/shared_spinlock.hpp | 13 +++++++------ src/utility/spinlock.hpp | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/utility/shared_spinlock.hpp b/src/utility/shared_spinlock.hpp index e14722d6..3853a9c5 100644 --- a/src/utility/shared_spinlock.hpp +++ b/src/utility/shared_spinlock.hpp @@ -4,13 +4,12 @@ #define GA_UTILITY_SHARED_SPINLOCK_HPP #include "utility.hpp" -#include "spinlock.hpp" #include -#include -#include #include #include +#include "spinlock.hpp" + namespace gapp::detail { class shared_spinlock @@ -20,9 +19,8 @@ namespace gapp::detail { while (true) { - while (cntr_.load(std::memory_order_relaxed)) GAPP_PAUSE(); if (try_lock()) break; - std::this_thread::yield(); + while (cntr_.load(std::memory_order_relaxed)) GAPP_PAUSE(); } } @@ -34,6 +32,7 @@ namespace gapp::detail void unlock() noexcept { + //__tsan_release(&cntr_); cntr_.fetch_sub(WRITER, std::memory_order_release); } @@ -41,7 +40,8 @@ namespace gapp::detail { cntr_.fetch_add(1, std::memory_order_release); while (cntr_.load(std::memory_order_relaxed) >= WRITER) GAPP_PAUSE(); - std::ignore = cntr_.load(std::memory_order_acquire); + std::atomic_thread_fence(std::memory_order_acquire); + __tsan_acquire(&cntr_); } bool try_lock_shared() noexcept @@ -54,6 +54,7 @@ namespace gapp::detail void unlock_shared() noexcept { + //__tsan_release(&cntr_); cntr_.fetch_sub(1, std::memory_order_release); } diff --git a/src/utility/spinlock.hpp b/src/utility/spinlock.hpp index 3a6efdcc..f7d5217f 100644 --- a/src/utility/spinlock.hpp +++ b/src/utility/spinlock.hpp @@ -5,7 +5,13 @@ #include "utility.hpp" #include -#include + +#if __has_feature(thread_sanitizer) && __has_include() +#include +#else +#define __tsan_acquire(...) +#define __tsan_release(...) +#endif namespace gapp::detail { @@ -16,9 +22,13 @@ namespace gapp::detail { while (true) { + if (!locked_.test_and_set(std::memory_order_relaxed)) + { + std::atomic_thread_fence(std::memory_order_acquire); + __tsan_acquire(&locked_); + break; + } while (locked_.test(std::memory_order_relaxed)) GAPP_PAUSE(); - if (!locked_.test_and_set(std::memory_order_acquire)) break; - std::this_thread::yield(); } } @@ -30,6 +40,7 @@ namespace gapp::detail void unlock() noexcept { + //__tsan_release(&locked_); locked_.clear(std::memory_order_release); }