From e5bff67c1394bd0f9eb02a2dd6a33785e07dede3 Mon Sep 17 00:00:00 2001 From: Chris Cannam Date: Thu, 7 Mar 2024 11:50:39 +0000 Subject: [PATCH] Replace platform-dependent barriers with c++11 std --- src/common/RingBuffer.h | 9 ++++++--- src/common/sysutils.cpp | 25 ------------------------- src/common/sysutils.h | 38 -------------------------------------- 3 files changed, 6 insertions(+), 66 deletions(-) diff --git a/src/common/RingBuffer.h b/src/common/RingBuffer.h index 3fb0c836..580422c3 100644 --- a/src/common/RingBuffer.h +++ b/src/common/RingBuffer.h @@ -238,7 +238,8 @@ RingBuffer::resized(int newSize) const { RingBuffer *newBuffer = new RingBuffer(newSize); - MBARRIER(); + std::atomic_thread_fence(std::memory_order_seq_cst); + int w = m_writer; int r = m_reader; @@ -464,7 +465,8 @@ RingBuffer::write(const S *const R__ source, int n) w += n; while (w >= m_size) w -= m_size; - MBARRIER(); + std::atomic_thread_fence(std::memory_order_seq_cst); + m_writer = w; return n; @@ -498,7 +500,8 @@ RingBuffer::zero(int n) w += n; while (w >= m_size) w -= m_size; - MBARRIER(); + std::atomic_thread_fence(std::memory_order_seq_cst); + m_writer = w; return n; diff --git a/src/common/sysutils.cpp b/src/common/sysutils.cpp index c5779b6b..4321ea15 100644 --- a/src/common/sysutils.cpp +++ b/src/common/sysutils.cpp @@ -166,31 +166,6 @@ void gettimeofday(struct timeval *tv, void * /* tz */) #endif -#ifdef _WIN32 -void system_memorybarrier() -{ -#ifdef _MSC_VER - MemoryBarrier(); -#else /* (mingw) */ - LONG Barrier = 0; - __asm__ __volatile__("xchgl %%eax,%0 " - : "=r" (Barrier)); -#endif -} -#else /* !_WIN32 */ -#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) -// Not required -#else -#include -void system_memorybarrier() -{ - pthread_mutex_t dummy = PTHREAD_MUTEX_INITIALIZER; - pthread_mutex_lock(&dummy); - pthread_mutex_unlock(&dummy); -} -#endif -#endif - } diff --git a/src/common/sysutils.h b/src/common/sysutils.h index 53a771e6..37edb989 100644 --- a/src/common/sysutils.h +++ b/src/common/sysutils.h @@ -117,43 +117,5 @@ void gettimeofday(struct timeval *p, void *tz); } // end namespace -// The following should be functions in the RubberBand namespace, really - -#ifdef _WIN32 - -namespace RubberBand { -extern void system_memorybarrier(); -} -#define MBARRIER() RubberBand::system_memorybarrier() - -#else // !_WIN32 - -#include - -#ifdef __APPLE__ -# if defined __MAC_10_12 -# define MBARRIER() __sync_synchronize() -# else -# include -# define MBARRIER() OSMemoryBarrier() -# endif -#else -# if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) -# define MBARRIER() __sync_synchronize() -# else -namespace RubberBand { -extern void system_memorybarrier(); -} -# define MBARRIER() ::RubberBand::system_memorybarrier() -# endif -#endif - -#endif // !_WIN32 - -#ifdef NO_THREADING -# undef MBARRIER -# define MBARRIER() -#endif // NO_THREADING - #endif