Use C++11 atomics for RefCounter (#1097)

This commit is contained in:
Stephen Booth 2023-07-28 23:58:50 -05:00 committed by GitHub
parent 271bd05afa
commit 9bcba812af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 56 deletions

View File

@ -25,37 +25,7 @@
#include "trefcounter.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if defined(HAVE_GCC_ATOMIC)
# define ATOMIC_INT int
# define ATOMIC_INC(x) __sync_add_and_fetch(&(x), 1)
# define ATOMIC_DEC(x) __sync_sub_and_fetch(&(x), 1)
#elif defined(HAVE_WIN_ATOMIC)
# if !defined(NOMINMAX)
# define NOMINMAX
# endif
# include <windows.h>
# define ATOMIC_INT long
# define ATOMIC_INC(x) InterlockedIncrement(&x)
# define ATOMIC_DEC(x) InterlockedDecrement(&x)
#elif defined(HAVE_MAC_ATOMIC)
# include <libkern/OSAtomic.h>
# define ATOMIC_INT int32_t
# define ATOMIC_INC(x) OSAtomicIncrement32Barrier(&x)
# define ATOMIC_DEC(x) OSAtomicDecrement32Barrier(&x)
#elif defined(HAVE_IA64_ATOMIC)
# include <ia64intrin.h>
# define ATOMIC_INT int
# define ATOMIC_INC(x) __sync_add_and_fetch(&x, 1)
# define ATOMIC_DEC(x) __sync_sub_and_fetch(&x, 1)
#else
# define ATOMIC_INT int
# define ATOMIC_INC(x) (++x)
# define ATOMIC_DEC(x) (--x)
#endif
#include <atomic>
namespace TagLib
{
@ -64,9 +34,11 @@ namespace TagLib
{
public:
RefCounterPrivate() :
refCount(1) {}
refCount(1)
{
}
volatile ATOMIC_INT refCount;
std::atomic_int refCount;
};
RefCounter::RefCounter() :
@ -81,16 +53,16 @@ namespace TagLib
void RefCounter::ref()
{
ATOMIC_INC(d->refCount);
d->refCount.fetch_add(1);
}
bool RefCounter::deref()
{
return (ATOMIC_DEC(d->refCount) == 0);
return d->refCount.fetch_sub(1) == 1;
}
int RefCounter::count() const
{
return static_cast<int>(d->refCount);
return d->refCount.load();
}
} // namespace TagLib

View File

@ -29,26 +29,6 @@
#include "taglib_export.h"
#include "taglib.h"
#ifdef __APPLE__
# define OSATOMIC_DEPRECATED 0
# include <libkern/OSAtomic.h>
# define TAGLIB_ATOMIC_MAC
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
# define TAGLIB_ATOMIC_WIN
#elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 401) \
&& (defined(__i386__) || defined(__i486__) || defined(__i586__) || \
defined(__i686__) || defined(__x86_64) || defined(__ia64)) \
&& !defined(__INTEL_COMPILER)
# define TAGLIB_ATOMIC_GCC
#elif defined(__ia64) && defined(__INTEL_COMPILER)
# include <ia64intrin.h>
# define TAGLIB_ATOMIC_GCC
#endif
#ifndef DO_NOT_DOCUMENT // Tell Doxygen to skip this class.
/*!
* \internal