mirror of
https://github.com/taglib/taglib.git
synced 2025-05-25 20:20:25 -04:00
Use C++11 atomics for RefCounter (#1097)
This commit is contained in:
parent
271bd05afa
commit
9bcba812af
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user