MDL SDK API nvidia_logo_transpbg.gif Up
atom.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2025 NVIDIA Corporation. All rights reserved.
3 **************************************************************************************************/
8
9#ifndef MI_BASE_ATOM_H
10#define MI_BASE_ATOM_H
11
12#include <mi/base/config.h>
13#include <mi/base/types.h>
14
15// Select implementation to use
16#if defined( MI_ARCH_X86) && (defined( MI_COMPILER_GCC) || defined( MI_COMPILER_ICC))
17# define MI_ATOM32_X86GCC
18#else
19# include <atomic>
20#endif
21
22namespace mi {
23
24namespace base {
25
31class Atom32
32{
33public:
35 Atom32() : m_value( 0) { }
36
38 Atom32( const Uint32 value) : m_value( value) { }
39
40#ifndef MI_ATOM32_X86GCC
42 Atom32( const Atom32& other);
43
45 Atom32& operator=( const Atom32& rhs);
46#endif
47
49 Uint32 operator=( const Uint32 rhs) { m_value = rhs; return rhs; }
50
53
56
59
62
65
68
70 operator Uint32() const { return m_value; }
71
73 Uint32 swap( const Uint32 rhs);
74
75private:
76#ifdef MI_ATOM32_X86GCC
77 // The counter.
78 volatile Uint32 m_value;
79#else
80 // The counter.
81 std::atomic_uint32_t m_value;
82#endif
83};
84
85#ifndef MI_FOR_DOXYGEN_ONLY
86
87#ifdef MI_ATOM32_X86GCC
88
89inline Uint32 Atom32::operator+=( const Uint32 rhs)
90{
91 Uint32 retval;
92 asm volatile(
93 "movl %2,%0\n"
94 "lock; xaddl %0,%1\n"
95 "addl %2,%0\n"
96 : "=&r"( retval), "+m"( m_value)
97 : "r"( rhs)
98 : "cc"
99 );
100 return retval;
101}
102
103inline Uint32 Atom32::operator-=( const Uint32 rhs)
104{
105 Uint32 retval;
106 asm volatile(
107 "neg %2\n"
108 "movl %2,%0\n"
109 "lock; xaddl %0,%1\n"
110 "addl %2,%0\n"
111 : "=&r"( retval), "+m"( m_value)
112 : "r"( rhs)
113 : "cc", "%2"
114 );
115 return retval;
116}
117
119{
120 Uint32 retval;
121 asm volatile(
122 "movl $1,%0\n"
123 "lock; xaddl %0,%1\n"
124 "addl $1,%0\n"
125 : "=&r"( retval), "+m"( m_value)
126 :
127 : "cc"
128 );
129 return retval;
130}
131
132inline Uint32 Atom32::operator++( int)
133{
134 Uint32 retval;
135 asm volatile(
136 "movl $1,%0\n"
137 "lock; xaddl %0,%1\n"
138 : "=&r"( retval), "+m"( m_value)
139 :
140 : "cc"
141 );
142 return retval;
143}
144
146{
147 Uint32 retval;
148 asm volatile(
149 "movl $-1,%0\n"
150 "lock; xaddl %0,%1\n"
151 "addl $-1,%0\n"
152 : "=&r"( retval), "+m"( m_value)
153 :
154 : "cc"
155 );
156 return retval;
157}
158
159inline Uint32 Atom32::operator--( int)
160{
161 Uint32 retval;
162 asm volatile(
163 "movl $-1,%0\n"
164 "lock; xaddl %0,%1\n"
165 : "=&r"( retval), "+m"( m_value)
166 :
167 : "cc"
168 );
169 return retval;
170}
171
172inline Uint32 Atom32::swap( const Uint32 rhs)
173{
174 Uint32 retval;
175 asm volatile(
176 "0:\n"
177 "movl %1,%0\n"
178 "lock; cmpxchg %2,%1\n"
179 "jnz 0b\n"
180 : "=&a"( retval), "+m"( m_value)
181 : "r"( rhs)
182 : "cc"
183 );
184 return retval;
185}
186
187#else
188
189inline Atom32::Atom32( const Atom32& other) : m_value( other.m_value.load()) { }
190
191inline Atom32& Atom32::operator=( const Atom32& rhs)
192{
193 m_value = rhs.m_value.load();
194 return *this;
195}
196
197inline Uint32 Atom32::operator+=( const Uint32 rhs)
198{
199 m_value += rhs;
200 return m_value;
201}
202
203inline Uint32 Atom32::operator-=( const Uint32 rhs)
204{
205 m_value -= rhs;
206 return m_value;
207}
208
210{
211 return ++m_value;
212}
213
214inline Uint32 Atom32::operator++( int)
215{
216 return m_value++;
217}
218
220{
221 return --m_value;
222}
223
224inline Uint32 Atom32::operator--( int)
225{
226 return m_value--;
227}
228
229inline Uint32 Atom32::swap( const Uint32 rhs)
230{
231 return m_value.exchange( rhs);
232}
233
234#endif // MI_ATOM32_X86GCC
235
236#undef MI_ATOM32_X86GCC
237
238#endif // !MI_FOR_DOXYGEN_ONLY
239 // end group mi_base_threads
241
242} // namespace base
243
244} // namespace mi
245
246#endif // MI_BASE_ATOM_H
A 32-bit unsigned counter with atomic arithmetic, increments, and decrements.
Definition: atom.h:32
Configuration of the Base API.
Uint32 operator-=(const Uint32 rhs)
Subtracts rhs from the counter.
Uint32 operator--(int)
Decrements the counter by one (post-decrement).
Atom32()
The default constructor initializes the counter to zero.
Definition: atom.h:35
Uint32 operator+=(const Uint32 rhs)
Adds rhs to the counter.
Uint32 operator=(const Uint32 rhs)
Assigns rhs to the counter.
Definition: atom.h:49
Uint32 operator++(int)
Increments the counter by one (post-increment).
Atom32(const Uint32 value)
This constructor initializes the counter to value.
Definition: atom.h:38
Atom32(const Atom32 &other)
The copy constructor assigns the value of other to the counter.
Uint32 operator++()
Increments the counter by one (pre-increment).
Atom32 & operator=(const Atom32 &rhs)
Assigns the value of rhs to the counter.
Uint32 swap(const Uint32 rhs)
Assigns rhs to the counter and returns the old value of counter.
Uint32 operator--()
Decrements the counter by one (pre-decrement).
unsigned int Uint32
32-bit unsigned integer.
Definition: types.h:49
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: example_derivatives.dox:5
Basic types.