SPH
AtomicFloat.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/Assert.h"
9 #include "common/Globals.h"
10 #include <atomic>
11 
13 
18 template <typename Type>
19 class Atomic {
20 private:
21  std::atomic<Type> value;
22 
23 public:
24  INLINE Atomic() = default;
25 
26  INLINE Atomic(const Type f) {
27  value.store(f);
28  }
29 
30  INLINE Atomic(const Atomic& other)
31  : Atomic(other.value.load()) {}
32 
33  INLINE Type get() const {
34  return value;
35  }
36 
37  INLINE Atomic& operator=(const Type f) {
38  value.store(f);
39  return *this;
40  }
41 
42  INLINE Atomic& operator+=(const Type f) {
43  atomicOp(f, [](const Type lhs, const Type rhs) { return lhs + rhs; });
44  return *this;
45  }
46 
47  INLINE Atomic& operator-=(const Type f) {
48  atomicOp(f, [](const Type lhs, const Type rhs) { return lhs - rhs; });
49  return *this;
50  }
51 
52  INLINE Atomic& operator*=(const Type f) {
53  atomicOp(f, [](const Type lhs, const Type rhs) { return lhs * rhs; });
54  return *this;
55  }
56 
57  INLINE Atomic& operator/=(const Type f) {
58  SPH_ASSERT(f != 0._f);
59  atomicOp(f, [](const Type lhs, const Type rhs) { return lhs / rhs; });
60  return *this;
61  }
62 
63  INLINE Type operator+(const Type f) const {
64  return value.load() + f;
65  }
66 
67  INLINE Type operator-(const Type f) const {
68  return value.load() - f;
69  }
70 
71  INLINE Type operator*(const Type f) const {
72  return value.load() * f;
73  }
74 
75  INLINE Type operator/(const Type f) const {
76  SPH_ASSERT(f != 0._f);
77  return value.load() / f;
78  }
79 
80  INLINE bool operator==(const Type f) const {
81  return value.load() == f;
82  }
83 
84  INLINE bool operator!=(const Type f) const {
85  return value.load() != f;
86  }
87 
88  INLINE bool operator>(const Type f) const {
89  return value.load() > f;
90  }
91 
92  INLINE bool operator<(const Type f) const {
93  return value.load() < f;
94  }
95 
96  INLINE friend std::ostream& operator<<(std::ostream& stream, const Atomic& f) {
97  stream << f.value.load();
98  return stream;
99  }
100 
101 private:
102  template <typename TOp>
103  INLINE Type atomicOp(const Type rhs, const TOp& op) {
104  Type lhs = value.load();
105  Type desired = op(lhs, rhs);
106  while (!value.compare_exchange_weak(lhs, desired)) {
107  desired = op(lhs, rhs);
108  }
109  return desired;
110  }
111 };
112 
Custom assertions.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Global parameters of the code.
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Atomic value implemented using compare-exchange.
Definition: AtomicFloat.h:19
INLINE friend std::ostream & operator<<(std::ostream &stream, const Atomic &f)
Definition: AtomicFloat.h:96
INLINE bool operator>(const Type f) const
Definition: AtomicFloat.h:88
INLINE Type operator-(const Type f) const
Definition: AtomicFloat.h:67
INLINE Atomic(const Type f)
Definition: AtomicFloat.h:26
INLINE Type operator/(const Type f) const
Definition: AtomicFloat.h:75
INLINE Type operator+(const Type f) const
Definition: AtomicFloat.h:63
INLINE Atomic & operator-=(const Type f)
Definition: AtomicFloat.h:47
INLINE bool operator<(const Type f) const
Definition: AtomicFloat.h:92
INLINE Type operator*(const Type f) const
Definition: AtomicFloat.h:71
INLINE Atomic & operator*=(const Type f)
Definition: AtomicFloat.h:52
INLINE Atomic & operator/=(const Type f)
Definition: AtomicFloat.h:57
INLINE bool operator==(const Type f) const
Definition: AtomicFloat.h:80
INLINE Atomic & operator+=(const Type f)
Definition: AtomicFloat.h:42
INLINE Type get() const
Definition: AtomicFloat.h:33
INLINE bool operator!=(const Type f) const
Definition: AtomicFloat.h:84
INLINE Atomic(const Atomic &other)
Definition: AtomicFloat.h:30
INLINE Atomic & operator=(const Type f)
Definition: AtomicFloat.h:37
INLINE Atomic()=default