Skip to content

Commit

Permalink
minor improvements to shared_spinlock
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Sep 1, 2024
1 parent 8f898cd commit 889ea1b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
16 changes: 6 additions & 10 deletions src/utility/shared_spinlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
#define GA_UTILITY_SHARED_SPINLOCK_HPP

#include "utility.hpp"
#include "spinlock.hpp"
#include <atomic>
#include <thread>
#include <tuple>
#include <limits>
#include <cstdint>

Expand All @@ -20,16 +17,15 @@ 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();
}
}

bool try_lock() noexcept
{
std::uint32_t expected = 0;
return cntr_.compare_exchange_strong(expected, WRITER, std::memory_order_acq_rel, std::memory_order_relaxed);
return cntr_.compare_exchange_strong(expected, WRITER, std::memory_order_acquire, std::memory_order_relaxed);
}

void unlock() noexcept
Expand All @@ -39,15 +35,15 @@ namespace gapp::detail

void lock_shared() noexcept
{
cntr_.fetch_add(1, std::memory_order_release);
cntr_.fetch_add(1, std::memory_order_relaxed);
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);
GAPP_ANNOTATE_TSAN_ACQUIRE(&cntr_);
}

bool try_lock_shared() noexcept
{
cntr_.fetch_add(1, std::memory_order_release);
if (cntr_.load(std::memory_order_acquire) < WRITER) return true;
if (cntr_.fetch_add(1, std::memory_order_acquire) < WRITER) return true;
cntr_.fetch_sub(1, std::memory_order_relaxed);
return false;
}
Expand Down
4 changes: 1 addition & 3 deletions src/utility/spinlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "utility.hpp"
#include <atomic>
#include <thread>

namespace gapp::detail
{
Expand All @@ -16,9 +15,8 @@ namespace gapp::detail
{
while (true)
{
while (locked_.test(std::memory_order_relaxed)) GAPP_PAUSE();
if (!locked_.test_and_set(std::memory_order_acquire)) break;
std::this_thread::yield();
while (locked_.test(std::memory_order_relaxed)) GAPP_PAUSE();
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/utility/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@
#endif


#if defined(__has_feature)
# if __has_feature(thread_sanitizer) && __has_include(<sanitizer/tsan_interface.h>)
# include <sanitizer/tsan_interface.h>
# define GAPP_ANNOTATE_TSAN_ACQUIRE(p) __tsan_acquire(p)
# define GAPP_ANNOTATE_TSAN_RELEASE(p) __tsan_release(p)
# else
# define GAPP_ANNOTATE_TSAN_ACQUIRE(p)
# define GAPP_ANNOTATE_TSAN_RELEASE(p)
# endif
#elif defined(__SANITIZE_THREAD__) && __has_include(<sanitizer/tsan_interface.h>)
# include <sanitizer/tsan_interface.h>
# define GAPP_ANNOTATE_TSAN_ACQUIRE(p) __tsan_acquire(p)
# define GAPP_ANNOTATE_TSAN_RELEASE(p) __tsan_release(p)
#else
# define GAPP_ANNOTATE_TSAN_ACQUIRE(p)
# define GAPP_ANNOTATE_TSAN_RELEASE(p)
#endif


#if defined(_WIN32) && !defined(GAPP_BUILD_STATIC)
# if defined(gapp_EXPORTS)
# define GAPP_API __declspec(dllexport)
Expand Down

0 comments on commit 889ea1b

Please sign in to comment.