{
public:
AtomicRefCount();
int increment();
int decrement();
void reset();
private:
AtomicRefCount& operator=( const AtomicRefCount& );volatile int m_count;
Mutex m_lock;
};
#include "atomicrefcount.h"
#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
# include <windows.h>
#elif defined( __APPLE__ )
# include <libkern/OSAtomic.h>
#elif defined( HAVE_GCC_ATOMIC_BUILTINS )
// Use intrinsic functions - no #include required.
#else
# include "mutexguard.h"
#endif
#ifdef _WIN32_WCE
# include <winbase.h>
#endif
AtomicRefCount::AtomicRefCount()
: m_count( 0 )
{
}
int AtomicRefCount::increment()
{
#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
return (int) ::InterlockedIncrement( (volatile LONG*)&m_count );
#elif defined( __APPLE__ )
return (int) OSAtomicIncrement32Barrier( (volatile int32_t*)&m_count );
#elif defined( HAVE_GCC_ATOMIC_BUILTINS )
// Use the gcc intrinsic for atomic increment if supported.
return (int) __sync_add_and_fetch( &m_count, 1 );
#else
// Fallback to using a lock
MutexGuard m( m_lock );
return ++m_count;
#endif
}
int AtomicRefCount::decrement()
{
#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
return (int) ::InterlockedDecrement( (volatile LONG*)&m_count );
#elif defined( __APPLE__ )
return (int) OSAtomicDecrement32Barrier( (volatile int32_t*)&m_count );
#elif defined( HAVE_GCC_ATOMIC_BUILTINS )
// Use the gcc intrinsic for atomic decrement if supported.
return (int) __sync_sub_and_fetch( &m_count, 1 );
#else
// Fallback to using a lock
MutexGuard m( m_lock );
return --m_count;
#endif
}
void AtomicRefCount::reset()
{
#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
::InterlockedExchange( (volatile LONG*)&m_count, (volatile LONG)0 );
#elif defined( __APPLE__ )
OSAtomicAnd32Barrier( (int32_t)0, (volatile int32_t*)&m_count );
#elif defined( HAVE_GCC_ATOMIC_BUILTINS )
// Use the gcc intrinsic for atomic decrement if supported.
__sync_fetch_and_and( &m_count, 0 );
#else
// Fallback to using a lock
MutexGuard m( m_lock );
m_count = 0;
#endif
}
本文介绍了一个名为AtomicRefCount的类的实现细节,该类用于提供跨平台的原子操作功能,如递增、递减和重置计数器值。通过使用特定于平台的原子操作或者互斥锁作为备选方案,确保了线程安全。
880

被折叠的 条评论
为什么被折叠?



