TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::AtomicInt32 Class Reference

#include <AtomicInt32.h>

Public Member Functions

 AtomicInt32 ()
 
 AtomicInt32 (const int32 x)
 
 AtomicInt32 (const AtomicInt32 &x)
 
const AtomicInt32operator= (const int32 x)
 
void operator= (const AtomicInt32 &x)
 
int32 value () const
 
int32 add (const int32 x)
 
int32 sub (const int32 x)
 
void increment ()
 
int32 decrement ()
 
int32 compareAndSet (const int32 comperand, const int32 exchange)
 

Private Attributes

volatile int32 m_value
 

Detailed Description

An integer that may safely be used on different threads without external locking.

On Win32, Linux, FreeBSD, and Mac OS X this is implemented without locks.

BETA API This is unsupported and may change

Constructor & Destructor Documentation

G3D::AtomicInt32::AtomicInt32 ( )
inline

Initial value is undefined.

43 {}
G3D::AtomicInt32::AtomicInt32 ( const int32  x)
inlineexplicit

Atomic set

46  {
47  m_value = x;
48  }
volatile int32 m_value
Definition: AtomicInt32.h:36
G3D::int16 x
Definition: Vector2int16.h:37
G3D::AtomicInt32::AtomicInt32 ( const AtomicInt32 x)
inline

Atomic set

51  {
52  m_value = x.m_value;
53  }
volatile int32 m_value
Definition: AtomicInt32.h:36
G3D::int16 x
Definition: Vector2int16.h:37

Member Function Documentation

int32 G3D::AtomicInt32::add ( const int32  x)
inline

Returns the old value, before the add.

72  {
73 # if defined(G3D_WINDOWS)
74 
75  return InterlockedExchangeAdd(&m_value, x);
76 
77 # elif defined(G3D_LINUX) || defined(G3D_FREEBSD)
78 
79  int32 old;
80  asm volatile ("lock; xaddl %0,%1"
81  : "=r"(old), "=m"(m_value) /* outputs */
82  : "0"(x), "m"(m_value) /* inputs */
83  : "memory", "cc");
84  return old;
85 
86 # elif defined(G3D_OSX)
87 
88  int32 old = m_value;
89  OSAtomicAdd32(x, &m_value);
90  return old;
91 
92 # endif
93  }
volatile int32 m_value
Definition: AtomicInt32.h:36
int32_t int32
Definition: Define.h:146
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the caller graph for this function:

int32 G3D::AtomicInt32::compareAndSet ( const int32  comperand,
const int32  exchange 
)
inline

Atomic test-and-set: if *this == comperand then *this := exchange else do nothing. In both cases, returns the old value of *this.

Performs an atomic comparison of this with the Comperand value. If this is equal to the Comperand value, the Exchange value is stored in this. Otherwise, no operation is performed.

Under VC6 the sign bit may be lost.

142  {
143 # if defined(G3D_WINDOWS)
144  return InterlockedCompareExchange(&m_value, exchange, comperand);
145 # elif defined(G3D_LINUX) || defined(G3D_FREEBSD) || defined(G3D_OSX)
146  // Based on Apache Portable Runtime
147  // http://koders.com/c/fid3B6631EE94542CDBAA03E822CA780CBA1B024822.aspx
148  int32 ret;
149  asm volatile ("lock; cmpxchgl %1, %2"
150  : "=a" (ret)
151  : "r" (exchange), "m" (m_value), "0"(comperand)
152  : "memory", "cc");
153  return ret;
154 
155  // Note that OSAtomicCompareAndSwap32 does not return a useful value for us
156  // so it can't satisfy the cmpxchgl contract.
157 # endif
158  }
volatile int32 m_value
Definition: AtomicInt32.h:36
int32_t int32
Definition: Define.h:146

+ Here is the caller graph for this function:

int32 G3D::AtomicInt32::decrement ( )
inline

Returns zero if the result is zero after decrement, non-zero otherwise.

113  {
114 # if defined(G3D_WINDOWS)
115  // Note: returns the newly decremented value
116  return InterlockedDecrement(&m_value);
117 # elif defined(G3D_LINUX) || defined(G3D_FREEBSD)
118  unsigned char nz;
119 
120  asm volatile ("lock; decl %1;\n\t"
121  "setnz %%al"
122  : "=a" (nz)
123  : "m" (m_value)
124  : "memory", "cc");
125  return nz;
126 # elif defined(G3D_OSX)
127  // Note: returns the newly decremented value
128  return OSAtomicDecrement32(&m_value);
129 # endif
130  }
volatile int32 m_value
Definition: AtomicInt32.h:36

+ Here is the caller graph for this function:

void G3D::AtomicInt32::increment ( )
inline
100  {
101 # if defined(G3D_WINDOWS)
102  // Note: returns the newly incremented value
103  InterlockedIncrement(&m_value);
104 # elif defined(G3D_LINUX) || defined(G3D_FREEBSD)
105  add(1);
106 # elif defined(G3D_OSX)
107  // Note: returns the newly incremented value
108  OSAtomicIncrement32(&m_value);
109 # endif
110  }
volatile int32 m_value
Definition: AtomicInt32.h:36
int32 add(const int32 x)
Definition: AtomicInt32.h:72

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const AtomicInt32& G3D::AtomicInt32::operator= ( const int32  x)
inline

Atomic set

56  {
57  m_value = x;
58  return *this;
59  }
volatile int32 m_value
Definition: AtomicInt32.h:36
G3D::int16 x
Definition: Vector2int16.h:37
void G3D::AtomicInt32::operator= ( const AtomicInt32 x)
inline

Atomic set

62  {
63  m_value = x.m_value;
64  }
volatile int32 m_value
Definition: AtomicInt32.h:36
G3D::int16 x
Definition: Vector2int16.h:37
int32 G3D::AtomicInt32::sub ( const int32  x)
inline

Returns old value.

96  {
97  return add(-x);
98  }
int32 add(const int32 x)
Definition: AtomicInt32.h:72
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the call graph for this function:

int32 G3D::AtomicInt32::value ( ) const
inline

Returns the current value

67  {
68  return m_value;
69  }
volatile int32 m_value
Definition: AtomicInt32.h:36

+ Here is the caller graph for this function:

Member Data Documentation

volatile int32 G3D::AtomicInt32::m_value
private

The documentation for this class was generated from the following file: