Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4atomic.hh
이 파일의 문서화 페이지로 가기
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
28 //
29 //
30 // $Id: G4atomic.hh 93110 2015-11-05 08:37:42Z jmadsen $
31 //
32 //
63 //
64 //
65 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
66 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
67 
68 
69 #ifndef G4atomic_hh_
70 #define G4atomic_hh_
71 
72 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
73 
74 #ifdef G4MULTITHREADED
75 
76 #include "G4atomic_defines.hh"
77 
78 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
79 
80 template<typename _Tp>
81 class G4atomic
82 {
83  public:
84 
85  typedef typename std::atomic<_Tp> base_type;
86  typedef _Tp value_type;
87 
88  private:
89 
90  using mem_ord = std::memory_order;
91 
92  public:
93 
94  // constructors
95  explicit
96  G4atomic(mem_ord mo = std::memory_order_acq_rel) : fMemOrder(mo)
97  { atomics::set(&fvalue, value_type()); }
98 
99  explicit
100  G4atomic(const value_type& _init,
101  mem_ord mo = std::memory_order_acq_rel) : fMemOrder(mo)
102  { atomics::set(&fvalue, _init); }
103 
104  // copy-constructor from pure C++11 atomic
105  explicit
106  G4atomic(const base_type& rhs,
107  mem_ord mo = std::memory_order_acq_rel) : fMemOrder(mo)
108  { atomics::set(&fvalue, rhs); }
109 
110  // copy-constructor
111  explicit
112  G4atomic(const G4atomic& rhs) : fMemOrder(rhs.fMemOrder)
113  { atomics::set(&fvalue, rhs.base()); }
114 
115  // assignment operators
116  G4atomic& operator=(const G4atomic& rhs)
117  {
118  if(this != &rhs)
119  atomics::set(&fvalue, rhs.fvalue);
120  return *this;
121  }
122 
123  G4atomic& operator=(const value_type& rhs)
124  {
125  atomics::set(&fvalue, rhs);
126  return *this;
127  }
128 
129  G4atomic& operator=(const base_type& rhs)
130  {
131  atomics::set(&fvalue, rhs);
132  return *this;
133  }
134 
135  // destructor
136  ~G4atomic() { fvalue.~base_type(); }
137 
138  // base version
139  base_type& base() { return fvalue; }
140  const base_type& base() const { return fvalue; }
141  base_type& base() volatile { return fvalue; }
142  const base_type& base() const volatile { return fvalue; }
143 
144  // check if atomic is lock-free
145  bool is_lock_free() const { return fvalue.is_lock_free(); }
146  bool is_lock_free() const volatile { return fvalue.is_lock_free(); }
147 
148  // store functions
149  void store(_Tp _desired, mem_ord mo = std::memory_order_seq_cst)
150  { atomics::set(fvalue, _desired, mo); }
151  void store(_Tp _desired, mem_ord mo = std::memory_order_seq_cst) volatile
152  { atomics::set(fvalue, _desired, mo); }
153 
154  // load functions
155  _Tp load(mem_ord mo = std::memory_order_seq_cst) const
156  { return atomics::get(fvalue, mo); }
157  _Tp load(mem_ord mo = std::memory_order_seq_cst) const volatile
158  { return atomics::get(fvalue, mo); }
159 
160  // implicit conversion functions
161  operator _Tp() const { return this->load(); }
162  operator _Tp() const volatile { return this->load(); }
163 
164  operator base_type&() const { return fvalue; }
165 
166  // compare-and-swap functions
167  bool compare_exchange_weak(_Tp& _expected, _Tp _desired,
168  mem_ord _success, mem_ord _failure)
169  { return fvalue.compare_exchange_weak(_expected, _desired,
170  _success, _failure); }
171  bool compare_exchange_weak(_Tp& _expected, _Tp _desired,
172  mem_ord _success, mem_ord _failure) volatile
173  { return fvalue.compare_exchange_weak(_expected, _desired,
174  _success, _failure); }
175 
176  bool compare_exchange_weak(_Tp& _expected, _Tp _desired, mem_ord _order)
177  { return fvalue.compare_exchange_weak(_expected, _desired, _order); }
178  bool compare_exchange_weak(_Tp& _expected, _Tp _desired,
179  mem_ord _order) volatile
180  { return fvalue.compare_exchange_weak(_expected, _desired, _order); }
181 
182  bool compare_exchange_strong(_Tp& _expected, _Tp _desired,
183  mem_ord _success, mem_ord _failure)
184  { return fvalue.compare_exchange_weak(_expected, _desired,
185  _success, _failure); }
186  bool compare_exchange_strong(_Tp& _expected, _Tp _desired,
187  mem_ord _success, mem_ord _failure) volatile
188  { return fvalue.compare_exchange_weak(_expected, _desired,
189  _success, _failure); }
190 
191  bool compare_exchange_strong(_Tp& _expected, _Tp _desired, mem_ord _order)
192  { return fvalue.compare_exchange_weak(_expected, _desired, _order); }
193  bool compare_exchange_strong(_Tp& _expected, _Tp _desired,
194  mem_ord _order) volatile
195  { return fvalue.compare_exchange_weak(_expected, _desired, _order); }
196 
197  // value_type operators
198  G4atomic& operator+=(const value_type& rhs)
199  { atomics::increment(&fvalue, rhs, fMemOrder); return *this; }
200  G4atomic& operator-=(const value_type& rhs)
201  { atomics::decrement(&fvalue, rhs, fMemOrder); return *this; }
202  G4atomic& operator*=(const value_type& rhs)
203  { atomics::multiply(&fvalue, rhs, fMemOrder); return *this; }
204  G4atomic& operator/=(const value_type& rhs)
205  { atomics::divide(&fvalue, rhs, fMemOrder); return *this; }
206 
207  // atomic operators
208  G4atomic& operator+=(const G4atomic& rhs)
209  { atomics::increment(&fvalue, rhs.fvalue); return *this; }
210  G4atomic& operator-=(const G4atomic& rhs)
211  { atomics::decrement(&fvalue, rhs.fvalue); return *this; }
212  G4atomic& operator*=(const G4atomic& rhs)
213  { atomics::multiply(&fvalue, rhs.fvalue); return *this; }
214  G4atomic& operator/=(const G4atomic& rhs)
215  { atomics::divide(&fvalue, rhs.fvalue); return *this; }
216 
217  G4atomic& operator+=(const G4atomic& rhs) volatile
218  { atomics::increment(&fvalue, rhs.fvalue); return *this; }
219  G4atomic& operator-=(const G4atomic& rhs) volatile
220  { atomics::decrement(&fvalue, rhs.fvalue); return *this; }
221  G4atomic& operator*=(const G4atomic& rhs) volatile
222  { atomics::multiply(&fvalue, rhs.fvalue); return *this; }
223  G4atomic& operator/=(const G4atomic& rhs) volatile
224  { atomics::divide(&fvalue, rhs.fvalue); return *this; }
225 
226  // STL atomic operators
227  G4atomic& operator+=(const std::atomic<_Tp>& rhs)
228  { atomics::increment(&fvalue, rhs, fMemOrder); return *this; }
229  G4atomic& operator-=(const std::atomic<_Tp>& rhs)
230  { atomics::decrement(&fvalue, rhs, fMemOrder); return *this; }
231  G4atomic& operator*=(const std::atomic<_Tp>& rhs)
232  { atomics::multiply(&fvalue, rhs, fMemOrder); return *this; }
233  G4atomic& operator/=(const std::atomic<_Tp>& rhs)
234  { atomics::divide(&fvalue, rhs, fMemOrder); return *this; }
235 
236  G4atomic& operator+=(const std::atomic<_Tp>& rhs) volatile
237  { atomics::increment(&fvalue, rhs, fMemOrder); return *this; }
238  G4atomic& operator-=(const std::atomic<_Tp>& rhs) volatile
239  { atomics::decrement(&fvalue, rhs, fMemOrder); return *this; }
240  G4atomic& operator*=(const std::atomic<_Tp>& rhs) volatile
241  { atomics::multiply(&fvalue, rhs, fMemOrder); return *this; }
242  G4atomic& operator/=(const std::atomic<_Tp>& rhs) volatile
243  { atomics::divide(&fvalue, rhs, fMemOrder); return *this; }
244 
245  // increment operators
246  value_type operator++() { value_type _tmp = ++fvalue; return _tmp; }
247  value_type operator++(int)
248  { value_type _tmp = fvalue++; return _tmp; }
249 
250  value_type operator--() { value_type _tmp = --fvalue; return _tmp; }
251  value_type operator--(int)
252  { value_type _tmp = fvalue--; return _tmp; }
253 
254  protected:
255 
256  base_type fvalue;
257  mem_ord fMemOrder;
258 
259 };
260 
261 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
262 
263 #else // ! G4MULTITHREADED
264 
265 template <typename _Tp> using G4atomic = _Tp;
266 
267 #endif // G4MULTITHREADED
268 
269 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
270 
271 #endif // G4atomic_hh_
const XML_Char int const XML_Char int const XML_Char * base
Definition: expat.h:331
Definition of the G4atomic_defines class.
_Tp G4atomic
Definition: G4atomic.hh:265