AGX Dynamics 2.41.3.2
Loading...
Searching...
No Matches
AtomicValue.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. 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#pragma once
17
19#include <agx/Integer.h>
20#include <ostream>
21#include <atomic>
22
23namespace agx
24{
25
29 template< class T >
31 {
32 public:
33 explicit AtomicValueT(T initialValue = 0);
34
36
38
42 inline T get() const;
43
48 inline T set(T value);
49
54 inline AtomicValueT<T>& operator= (T value);
55
60 inline T add(T value);
61
66 inline T sub(T value);
67
72 inline T inc();
73
78 inline T dec();
79
87 inline T compareAndSet(T test, T newValue);
88
89 private:
90 #if !AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
91 std::atomic< T > m_value;
92 #else
93 T m_value;
94 #endif
95 };
96
97
102
103
104
105 // -----------------------------------------------------------------------------------------------
106 template<class T>
107 inline std::ostream& operator << ( std::ostream& stream, const AtomicValueT<T>& value )
108 {
109 stream << value.get();
110 return stream;
111 }
112
113 /* Implementation */
114
115 #if !(defined(AGX_APPLE_IOS) || (AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()))
116
117 template<class T>
118 inline AtomicValueT<T>::AtomicValueT(T initialValue) : m_value(initialValue)
119 {
120 }
121
122 template<class T>
124 {
125 m_value.store( av.m_value.load() );
126 }
127
128 template<class T>
130 {
131 m_value.store( rhs.m_value.load() );
132 return *this;
133 }
134
135 template<class T>
136 inline T AtomicValueT<T>::get() const
137 {
138 return m_value;
139 }
140
141 template<class T>
142 inline T AtomicValueT<T>::set(T value)
143 {
144 return m_value.exchange( value );
145 }
146
147 template<class T>
149 {
150 m_value = value;
151 return *this;
152 }
153
154
155 template<class T>
156 inline T AtomicValueT<T>::add(T value)
157 {
158 return m_value.fetch_add( value );
159 }
160
161 template<class T>
162 inline T AtomicValueT<T>::sub(T value)
163 {
164 return m_value.fetch_sub( value );
165 }
166
167 template<class T>
169 {
170 return m_value.fetch_add( 1 );
171 }
172
173 template<class T>
175 {
176 return m_value.fetch_sub( 1 );
177 }
178
179 template<class T>
180 inline T AtomicValueT<T>::compareAndSet(T test, T newValue)
181 {
182 return m_value.compare_exchange_strong( test, newValue );
183 }
184
185 #else
186 template<class T>
187 inline AtomicValueT<T>::AtomicValueT(T initialValue) : m_value(initialValue)
188 {
189 }
190
191 template<class T>
193 {
194 m_value = av.m_value;
195 }
196
197 template<class T>
198 AtomicValueT<T>& AtomicValueT<T>::operator=(const agx::AtomicValueT<T>& rhs )
199 {
200 m_value = rhs.m_value;
201 return *this;
202 }
203
204 template<class T>
205 inline T AtomicValueT<T>::get() const
206 {
207 return m_value;
208 }
209
210 template<class T>
211 inline T AtomicValueT<T>::set(T value)
212 {
213 T prev = m_value;
214 m_value = value;
215
216 return prev;
217 }
218
219 template<class T>
220 inline AtomicValueT<T>& AtomicValueT<T>::operator=(T value)
221 {
222 m_value = value;
223 return *this;
224 }
225
226
227 template<class T>
228 inline T AtomicValueT<T>::add(T value)
229 {
230 T prev = m_value;
231 m_value += value;
232
233 return prev;
234 }
235
236 template<class T>
237 inline T AtomicValueT<T>::sub(T value)
238 {
239 T prev = m_value;
240 m_value -= value;
241
242
243 return prev;
244 }
245
246 template<class T>
247 inline T AtomicValueT<T>::inc()
248 {
249 return this->add(1);
250 }
251
252 template<class T>
253 inline T AtomicValueT<T>::dec()
254 {
255 return this->add(-1);
256 }
257
258 template<class T>
259 inline T AtomicValueT<T>::compareAndSet(T test, T newValue)
260 {
261 T prev = m_value;
262 if (m_value == test)
263 m_value = newValue;
264
265 return prev;
266 }
267
268 #endif
269}
AtomicValueT template for integral types, all operations are atomic.
Definition: AtomicValue.h:31
T set(T value)
Set the value.
Definition: AtomicValue.h:142
T get() const
Get the current value.
Definition: AtomicValue.h:136
T inc()
Increment the value.
Definition: AtomicValue.h:168
AtomicValueT(const agx::AtomicValueT< T > &av)
Definition: AtomicValue.h:123
AtomicValueT< T > & operator=(const agx::AtomicValueT< T > &rhs)
Definition: AtomicValue.h:129
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...
Definition: AtomicValue.h:180
T dec()
Decrement the value.
Definition: AtomicValue.h:174
AtomicValueT(T initialValue=0)
Definition: AtomicValue.h:118
T add(T value)
Add a value.
Definition: AtomicValue.h:156
T sub(T value)
Subtract a value.
Definition: AtomicValue.h:162
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
std::ostream & operator<<(std::ostream &os, const agx::AddedMassInteraction::Matrix6x6 &m)