TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
AtomicInt32.h
Go to the documentation of this file.
1 
9 #ifndef G3D_ATOMICINT32_H
10 #define G3D_ATOMICINT32_H
11 
12 #include "G3D/platform.h"
13 #include "G3D/g3dmath.h"
14 
15 #if defined(G3D_OSX)
16 # include <libkern/OSAtomic.h>
17 #endif
18 
19 namespace G3D {
20 
29 class AtomicInt32 {
30 private:
31 # if defined(G3D_WINDOWS)
32  volatile long m_value;
33 # elif defined(G3D_OSX)
35 # else
36  volatile int32 m_value;
37 # endif
38 
39 
40 public:
41 
44 
46  explicit AtomicInt32(const int32 x) {
47  m_value = x;
48  }
49 
52  m_value = x.m_value;
53  }
54 
56  const AtomicInt32& operator=(const int32 x) {
57  m_value = x;
58  return *this;
59  }
60 
62  void operator=(const AtomicInt32& x) {
63  m_value = x.m_value;
64  }
65 
67  int32 value() const {
68  return m_value;
69  }
70 
72  int32 add(const int32 x) {
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  }
94 
96  int32 sub(const int32 x) {
97  return add(-x);
98  }
99 
100  void increment() {
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  }
111 
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  }
131 
132 
142  int32 compareAndSet(const int32 comperand, const int32 exchange) {
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  }
159 
160 };
161 
162 } // namespace
163 
164 #endif
void operator=(const AtomicInt32 &x)
Definition: AtomicInt32.h:62
Definition: AABox.h:25
Definition: AtomicInt32.h:29
void increment()
Definition: AtomicInt32.h:100
AtomicInt32()
Definition: AtomicInt32.h:43
AtomicInt32(const int32 x)
Definition: AtomicInt32.h:46
volatile int32 m_value
Definition: AtomicInt32.h:36
int32 decrement()
Definition: AtomicInt32.h:113
AtomicInt32(const AtomicInt32 &x)
Definition: AtomicInt32.h:51
const AtomicInt32 & operator=(const int32 x)
Definition: AtomicInt32.h:56
int32 value() const
Definition: AtomicInt32.h:67
int32 add(const int32 x)
Definition: AtomicInt32.h:72
int32_t int32
Definition: g3dmath.h:167
int32 compareAndSet(const int32 comperand, const int32 exchange)
Definition: AtomicInt32.h:142
signed int int32_t
Definition: stdint.h:77
int32 sub(const int32 x)
Definition: AtomicInt32.h:96
G3D::int16 x
Definition: Vector2int16.h:37