Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4atomic_defines.hh
Go to the documentation of this file.
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_defines.hh 93110 2015-11-05 08:37:42Z jmadsen $
31 //
32 //
36 //
37 //
38 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
40 
41 
42 #ifndef G4atomic_defines_hh_
43 #define G4atomic_defines_hh_
44 
45 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
46 
47 #ifdef G4MULTITHREADED
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
51 #include <functional>
52 #include <atomic>
53 
54 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
55 
56 namespace atomics
57 {
58  //------------------------------------------------------------------------//
59  namespace details
60  {
61  //--------------------------------------------------------------------//
62  template <typename _Tp>
63  using OpFunction = std::function<_Tp(const _Tp&, const _Tp&)>;
64  //--------------------------------------------------------------------//
65  template <typename _Tp>
66  inline void do_fetch_and_store(std::atomic<_Tp>* _atomic,
67  const _Tp& _value,
68  std::memory_order mem_odr)
69  {
70  _atomic->store(_value, mem_odr);
71  }
72  //--------------------------------------------------------------------//
73  template <typename _Tp>
74  inline void do_fetch_and_store(std::atomic<_Tp>* _atomic,
75  const std::atomic<_Tp>& _value,
76  std::memory_order mem_odr)
77  {
78  _atomic->store(_value.load(), mem_odr);
79  }
80  //--------------------------------------------------------------------//
81  template <typename _Tp>
82  inline void do_compare_and_swap(std::atomic<_Tp>* _atomic,
83  const _Tp& _value,
84  const OpFunction<_Tp>& _operator,
85  std::memory_order mem_odr)
86  {
87  _Tp _expected = _Tp();
88  do {
89  _expected = _atomic->load();
90  } while (!(_atomic->compare_exchange_weak(_expected,
91  _operator(_expected,
92  _value),
93  mem_odr)));
94  }
95  //--------------------------------------------------------------------//
96  template <typename _Tp>
97  inline void do_compare_and_swap(std::atomic<_Tp>* _atomic,
98  const std::atomic<_Tp>&
99  _atomic_value,
100  const OpFunction<_Tp>& _operator,
101  std::memory_order mem_odr)
102  {
103  _Tp _expected = _Tp();
104  do {
105  _expected = _atomic->load();
106  } while (!(_atomic->compare_exchange_weak(_expected,
107  _operator(_expected,
108  _atomic_value.load()),
109  mem_odr)));
110  }
111  //--------------------------------------------------------------------//
112  }
113  //------------------------------------------------------------------------//
114  // WITH ATOMIC TEMPLATE BASE TYPE AS SECOND PARAMETER
115  //------------------------------------------------------------------------//
116  template <typename T>
117  inline void set(std::atomic<T>* _atomic,
118  const T& _desired,
119  std::memory_order mem_odr
120  = std::memory_order_seq_cst)
121  {
122  details::do_compare_and_swap(_atomic,
123  _desired,
124  details::OpFunction<T>
125  ([](const T&, const T& y){return y;}),
126  mem_odr);
127  }
128  //------------------------------------------------------------------------//
129  template <typename T>
130  inline void set(std::atomic<T>& _atomic,
131  const T& _desired,
132  std::memory_order mem_odr
133  = std::memory_order_seq_cst)
134  {
135  set(&_atomic, _desired, mem_odr);
136  }
137  //------------------------------------------------------------------------//
138  template <typename T>
139  inline void increment(std::atomic<T>* _atomic,
140  const T& _increment,
141  std::memory_order mem_odr)
142  {
143  details::do_compare_and_swap(_atomic, _increment,
144  details::OpFunction<T>
145  ([](const T& x, const T& y){return x+y;}),
146  mem_odr);
147  }
148  //------------------------------------------------------------------------//
149  template <typename T>
150  inline void decrement(std::atomic<T>* _atomic, const T& _decrement,
151  std::memory_order mem_odr)
152  {
153  details::do_compare_and_swap(_atomic, _decrement,
154  details::OpFunction<T>
155  ([](const T& x, const T& y){return x-y;}),
156  mem_odr);
157  }
158  //------------------------------------------------------------------------//
159  template <typename T>
160  inline void multiply(std::atomic<T>* _atomic, const T& _factor,
161  std::memory_order mem_odr)
162  {
163  details::do_compare_and_swap(_atomic, _factor,
164  details::OpFunction<T>
165  ([](const T& x, const T& y){return x*y;}),
166  mem_odr);
167  }
168  //------------------------------------------------------------------------//
169  template <typename T>
170  inline void divide(std::atomic<T>* _atomic, const T& _factor,
171  std::memory_order mem_odr)
172  {
173  details::do_compare_and_swap(_atomic, _factor,
174  details::OpFunction<T>
175  ([](const T& x, const T& y){return x/y;}),
176  mem_odr);
177  }
178  //------------------------------------------------------------------------//
179  // WITH ATOMICS AS SECOND PARAMETER
180  //------------------------------------------------------------------------//
181  template <typename T>
182  inline void set(std::atomic<T>* _atomic,
183  const std::atomic<T>& _atomic_desired,
184  std::memory_order mem_odr
185  = std::memory_order_seq_cst)
186  {
187  //details::do_fetch_and_store(_atomic, _desired);
188  details::do_compare_and_swap(_atomic, _atomic_desired,
189  details::OpFunction<T>
190  ([](const T&, const T& y){return y;}),
191  mem_odr);
192  }
193  //------------------------------------------------------------------------//
194  template <typename T>
195  inline void set(std::atomic<T>& _atomic,
196  const std::atomic<T>& _atomic_desired,
197  std::memory_order mem_odr)
198  {
199  set(&_atomic, _atomic_desired, mem_odr);
200  }
201  //------------------------------------------------------------------------//
202  template <typename T>
203  inline void increment(std::atomic<T>* _atomic,
204  const std::atomic<T>& _atomic_increment,
205  std::memory_order mem_odr)
206  {
207  details::do_compare_and_swap(_atomic, _atomic_increment,
208  details::OpFunction<T>
209  ([](const T& x, const T& y){return x+y;}),
210  mem_odr);
211  }
212  //------------------------------------------------------------------------//
213  template <typename T>
214  inline void decrement(std::atomic<T>* _atomic,
215  const std::atomic<T>& _atomic_decrement,
216  std::memory_order mem_odr)
217  {
218  details::do_compare_and_swap(_atomic, _atomic_decrement,
219  details::OpFunction<T>
220  ([](const T& x, const T& y){return x-y;}),
221  mem_odr);
222  }
223  //------------------------------------------------------------------------//
224  template <typename T>
225  inline void multiply(std::atomic<T>* _atomic,
226  const std::atomic<T>& _atomic_factor,
227  std::memory_order mem_odr)
228  {
229  details::do_compare_and_swap(_atomic, _atomic_factor,
230  details::OpFunction<T>
231  ([](const T& x, const T& y){return x*y;}),
232  mem_odr);
233  }
234  //------------------------------------------------------------------------//
235  template <typename T>
236  inline void divide(std::atomic<T>* _atomic,
237  const std::atomic<T>& _atomic_factor,
238  std::memory_order mem_odr)
239  {
240  details::do_compare_and_swap(_atomic, _atomic_factor,
241  details::OpFunction<T>
242  ([](const T& x, const T& y){return x/y;}),
243  mem_odr);
244  }
245 
246  //------------------------------------------------------------------------//
247  // STANDARD OVERLOAD //
248  //------------------------------------------------------------------------//
249  template <typename T>
250  inline void set(T* _non_atomic, const T& _desired)
251  {
252  *_non_atomic = _desired;
253  }
254  //------------------------------------------------------------------------//
255  template <typename T>
256  inline void set(T& _non_atomic, const T& _desired)
257  {
258  set(&_non_atomic, _desired);
259  }
260  //------------------------------------------------------------------------//
261  // STL PAIR OVERLOAD //
262  //------------------------------------------------------------------------//
263  //
264  //------------------------------------------------------------------------//
265  // WITH ATOMIC TEMPLATE TYPE AS SECOND PARAMETER
266  //------------------------------------------------------------------------//
267  template <typename T, typename U>
268  inline void set(std::pair<std::atomic<T>,
269  std::atomic<U> >* _atomic,
270  const std::pair<T, U>& _desired)
271  {
272  set(&_atomic->first, _desired.first);
273  set(&_atomic->second, _desired.second);
274  }
275  //------------------------------------------------------------------------//
276  template <typename T, typename U>
277  inline void set(std::pair<std::atomic<T>,
278  std::atomic<U> >& _atomic,
279  const std::pair<T, U>& _desired)
280  {
281  set(&_atomic, _desired);
282  }
283  //------------------------------------------------------------------------//
284  template <typename T, typename U>
285  inline void increment(std::pair<std::atomic<T>,
286  std::atomic<U> >* _atomic,
287  const std::pair<T, U>& _increment)
288  {
289  increment(&_atomic->first, _increment.first);
290  increment(&_atomic->second, _increment.second);
291  }
292  //------------------------------------------------------------------------//
293  template <typename T, typename U>
294  inline void decrement(std::pair<std::atomic<T>,
295  std::atomic<U> >* _atomic,
296  const std::pair<T, U>& _decrement)
297  {
298  decrement(&_atomic->first, _decrement.first);
299  decrement(&_atomic->second, _decrement.second);
300  }
301  //------------------------------------------------------------------------//
302  template <typename T, typename U>
303  inline void multiply(std::pair<std::atomic<T>,
304  std::atomic<U> >* _atomic,
305  const std::pair<T, U>& _factor)
306  {
307  multiply(&_atomic->first, _factor.first);
308  multiply(&_atomic->second, _factor.second);
309  }
310  //------------------------------------------------------------------------//
311  template <typename T, typename U>
312  inline void divide(std::pair<std::atomic<T>,
313  std::atomic<U> >* _atomic,
314  const std::pair<T, U>& _factor)
315  {
316  divide(&_atomic->first, _factor.first);
317  divide(&_atomic->second, _factor.second);
318  }
319  //------------------------------------------------------------------------//
320  // WITH ATOMICS AS SECOND PARAMETER
321  //------------------------------------------------------------------------//
322  template <typename T, typename U>
323  inline void set(std::pair<std::atomic<T>,
324  std::atomic<U> >* _atomic,
325  const std::pair<std::atomic<T>,
326  std::atomic<U> >& _desired)
327  {
328  set(&_atomic->first, _desired.first);
329  set(&_atomic->second, _desired.second);
330  }
331  //------------------------------------------------------------------------//
332  template <typename T, typename U>
333  inline void set(std::pair<std::atomic<T>,
334  std::atomic<U> >& _atomic,
335  const std::pair<std::atomic<T>,
336  std::atomic<U> >& _desired)
337  {
338  set(&_atomic, _desired);
339  }
340  //------------------------------------------------------------------------//
341  template <typename T, typename U>
342  inline void increment(std::pair<std::atomic<T>,
343  std::atomic<U> >* _atomic,
344  const std::pair<std::atomic<T>,
345  std::atomic<U> >& _increment)
346  {
347  increment(&_atomic->first, _increment.first);
348  increment(&_atomic->second, _increment.second);
349  }
350  //------------------------------------------------------------------------//
351  template <typename T, typename U>
352  inline void decrement(std::pair<std::atomic<T>,
353  std::atomic<U> >* _atomic,
354  const std::pair<std::atomic<T>,
355  std::atomic<U> >& _decrement)
356  {
357  decrement(&_atomic->first, _decrement.first);
358  decrement(&_atomic->second, _decrement.second);
359  }
360  //------------------------------------------------------------------------//
361  template <typename T, typename U>
362  inline void multiply(std::pair<std::atomic<T>,
363  std::atomic<U> >* _atomic,
364  const std::pair<std::atomic<T>,
365  std::atomic<U> >& _factor)
366  {
367  multiply(&_atomic->first, _factor.first);
368  multiply(&_atomic->second, _factor.second);
369  }
370  //------------------------------------------------------------------------//
371  template <typename T, typename U>
372  inline void divide(std::pair<std::atomic<T>,
373  std::atomic<U> >* _atomic,
374  const std::pair<std::atomic<T>,
375  std::atomic<U> >& _factor)
376  {
377  divide(&_atomic->first, _factor.first);
378  divide(&_atomic->second, _factor.second);
379  }
380  //------------------------------------------------------------------------//
381 
382 
383  //------------------------------------------------------------------------//
384  template <typename T>
385  inline T get(const T& _non_atomic)
386  {
387  return _non_atomic;
388  }
389  //------------------------------------------------------------------------//
390  template <typename T>
391  inline T get(const T& _non_atomic, std::memory_order)
392  {
393  return _non_atomic;
394  }
395  //------------------------------------------------------------------------//
396  template <typename T>
397  inline T get(const std::atomic<T>& _atomic)
398  {
399  return _atomic.load();
400  }
401  //------------------------------------------------------------------------//
402  template <typename T>
403  inline T get(const std::atomic<T>& _atomic,
404  std::memory_order mem_odr)
405  {
406  return _atomic.load(mem_odr);
407  }
408  //------------------------------------------------------------------------//
409  template <typename T, typename U>
410  inline std::pair<T, U> get(const std::pair<std::atomic<T>,
411  std::atomic<U> >& _atomic)
412  {
413  return std::pair<T, U>(get(_atomic.first), get(_atomic.second));
414  }
415  //------------------------------------------------------------------------//
416  template <typename T, typename U>
417  inline std::pair<T, U> get(const std::pair<std::atomic<T>,
418  std::atomic<U> >& _atomic,
419  std::memory_order mem_odr)
420  {
421  return std::pair<T, U>(get(_atomic.first, mem_odr),
422  get(_atomic.second, mem_odr));
423  }
424  //------------------------------------------------------------------------//
425 
426 
427 
428  //------------------------------------------------------------------------//
429  // for plain old data (POD) and pairs (e.g. std::pair<atomic<T>, atomic<U>>)
430  template <typename _Tp_base, typename _Tp_atom>
431  inline _Tp_base base(const _Tp_atom& _atomic)
432  {
433  return get(_atomic);
434  }
435  //------------------------------------------------------------------------//
436 
437 } // namespace atomics
438 
439 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
440 
441 #endif // G4MULTITHREADED
442 
443 #endif // atomic_typedefs_hh_
tuple x
Definition: test.py:50
const XML_Char int const XML_Char int const XML_Char * base
Definition: expat.h:331