AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
AtomicValue.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_ATOMICVALUE_H
18#define AGX_ATOMICVALUE_H
19
20#include <agx/config/AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING.h>
21#include <agx/Integer.h>
22#include <ostream>
23#include <atomic>
24
25namespace agx
26{
27
31 template< class T >
33 {
34 public:
35 explicit AtomicValueT(T initialValue = 0);
36
38
40
44 inline T get() const;
45
50 inline T set(T value);
51
56 inline AtomicValueT<T>& operator= (T value);
57
62 inline T add(T value);
63
68 inline T sub(T value);
69
74 inline T inc();
75
80 inline T dec();
81
89 inline T compareAndSet(T test, T newValue);
90
91 private:
92 #if !AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
93 std::atomic< T > m_value;
94 #else
95 T m_value;
96 #endif
97 };
98
99
104
105
106
107 // -----------------------------------------------------------------------------------------------
108 template<class T>
109 inline std::ostream& operator << ( std::ostream& stream, const AtomicValueT<T>& value )
110 {
111 stream << value.get();
112 return stream;
113 }
114
115 /* Implementation */
116
117 #if !(defined(AGX_APPLE_IOS) || (AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()))
118
119 template<class T>
120 inline AtomicValueT<T>::AtomicValueT(T initialValue) : m_value(initialValue)
121 {
122 }
123
124 template<class T>
126 {
127 m_value.store( av.m_value.load() );
128 }
129
130 template<class T>
132 {
133 m_value.store( rhs.m_value.load() );
134 return *this;
135 }
136
137 template<class T>
138 inline T AtomicValueT<T>::get() const
139 {
140 return m_value;
141 }
142
143 template<class T>
144 inline T AtomicValueT<T>::set(T value)
145 {
146 return m_value.exchange( value );
147 }
148
149 template<class T>
151 {
152 m_value = value;
153 return *this;
154 }
155
156
157 template<class T>
158 inline T AtomicValueT<T>::add(T value)
159 {
160 return m_value.fetch_add( value );
161 }
162
163 template<class T>
164 inline T AtomicValueT<T>::sub(T value)
165 {
166 return m_value.fetch_sub( value );
167 }
168
169 template<class T>
171 {
172 return m_value.fetch_add( 1 );
173 }
174
175 template<class T>
177 {
178 return m_value.fetch_sub( 1 );
179 }
180
181 template<class T>
182 inline T AtomicValueT<T>::compareAndSet(T test, T newValue)
183 {
184 return m_value.compare_exchange_strong( test, newValue );
185 }
186
187 #else
188 template<class T>
189 inline AtomicValueT<T>::AtomicValueT(T initialValue) : m_value(initialValue)
190 {
191 }
192
193 template<class T>
195 {
196 m_value = av.m_value;
197 }
198
199 template<class T>
200 AtomicValueT<T>& AtomicValueT<T>::operator=(const agx::AtomicValueT<T>& rhs )
201 {
202 m_value = rhs.m_value;
203 return *this;
204 }
205
206 template<class T>
207 inline T AtomicValueT<T>::get() const
208 {
209 return m_value;
210 }
211
212 template<class T>
213 inline T AtomicValueT<T>::set(T value)
214 {
215 T prev = m_value;
216 m_value = value;
217
218 return prev;
219 }
220
221 template<class T>
222 inline AtomicValueT<T>& AtomicValueT<T>::operator=(T value)
223 {
224 m_value = value;
225 return *this;
226 }
227
228
229 template<class T>
230 inline T AtomicValueT<T>::add(T value)
231 {
232 T prev = m_value;
233 m_value += value;
234
235 return prev;
236 }
237
238 template<class T>
239 inline T AtomicValueT<T>::sub(T value)
240 {
241 T prev = m_value;
242 m_value -= value;
243
244
245 return prev;
246 }
247
248 template<class T>
249 inline T AtomicValueT<T>::inc()
250 {
251 return this->add(1);
252 }
253
254 template<class T>
255 inline T AtomicValueT<T>::dec()
256 {
257 return this->add(-1);
258 }
259
260 template<class T>
261 inline T AtomicValueT<T>::compareAndSet(T test, T newValue)
262 {
263 T prev = m_value;
264 if (m_value == test)
265 m_value = newValue;
266
267 return prev;
268 }
269
270 #endif
271}
272
273
274#endif /* _AGX_ATOMICVALUE_H_ */
AtomicValueT template for integral types, all operations are atomic.
Definition AtomicValue.h:33
T set(T value)
Set the value.
T get() const
Get the current value.
T inc()
Increment the value.
AtomicValueT(const agx::AtomicValueT< T > &av)
AtomicValueT< T > & operator=(const agx::AtomicValueT< T > &rhs)
T compareAndSet(T test, T newValue)
Compares the current value with a test value and if they are equal the value is replaced by a new val...
T dec()
Decrement the value.
AtomicValueT(T initialValue=0)
T add(T value)
Add a value.
T sub(T value)
Subtract a value.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
std::ostream & operator<<(std::ostream &os, const agx::AddedMassInteraction::Matrix6x6 &m)