summaryrefslogtreecommitdiffstats
path: root/raul/AtomicInt.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'raul/AtomicInt.hpp')
-rw-r--r--raul/AtomicInt.hpp118
1 files changed, 31 insertions, 87 deletions
diff --git a/raul/AtomicInt.hpp b/raul/AtomicInt.hpp
index 224b0dd..2fd624c 100644
--- a/raul/AtomicInt.hpp
+++ b/raul/AtomicInt.hpp
@@ -17,9 +17,9 @@
#ifndef RAUL_ATOMIC_INT_HPP
#define RAUL_ATOMIC_INT_HPP
-#ifdef RAUL_CPP0x
+#include "raul/barrier.hpp"
-#include <atomic>
+namespace Raul {
/** Atomic integer.
* \ingroup raul
@@ -30,118 +30,62 @@ public:
inline AtomicInt(const AtomicInt& copy) : _val(copy.get()) {}
- inline int get() const { return _val.load(); }
-
- inline void operator=(int val) { _val = val; }
- inline void operator=(const AtomicInt& val) { _val = val.get(); }
-
- inline void operator+=(int val) { _val += val; }
+ inline int get() const {
+ Raul::barrier();
+ return _val;
+ }
- inline void operator-=(int val) { _val -= val; }
+ inline void operator=(int val) {
+ _val = val;
+ Raul::barrier();
+ }
- inline bool operator==(int val) const { return _val == val; }
+ inline void operator=(const AtomicInt& val) {
+ _val = val.get();
+ Raul::barrier();
+ }
- inline int operator+(int val) const { return _val + val; }
+ inline AtomicInt& operator++() { return operator+=(1); }
+ inline AtomicInt& operator--() { return operator-=(1); }
+ inline bool operator==(int val) const { return get() == val; }
+ inline int operator+(int val) const { return get() + val; }
+ inline int operator-(int val) const { return get() - val; }
- inline AtomicInt& operator++() { ++_val; return *this; }
+ inline AtomicInt& operator+=(int val) {
+ __sync_fetch_and_add(&_val, val);
+ return *this;
+ }
- inline AtomicInt& operator--() { --_val; return *this; }
+ inline AtomicInt& operator-=(int val) {
+ __sync_fetch_and_sub(&_val, val);
+ return *this;
+ }
/** Set value to @a val iff current value is @a old.
* @return true iff set succeeded.
*/
inline bool compare_and_exchange(int old, int val) {
- return _val.compare_exchange_strong(old, val);
+ return __sync_bool_compare_and_swap(&_val, old, val);
}
/** Add val to value.
* @return value immediately before addition took place.
*/
inline int exchange_and_add(int val) {
- return _val.fetch_add(val);
+ return __sync_fetch_and_add(&_val, val);
}
/** Decrement value.
* @return true if value is now 0, otherwise false.
*/
inline bool decrement_and_test() {
- return _val.fetch_add(-1) == 0;
+ return __sync_sub_and_fetch(&_val, 1) == 0;
}
private:
- std::atomic<int> _val;
-};
-
-#else // !RAUL_CPP0x
-
-#include <glib.h>
-
-namespace Raul {
-
-/** Atomic integer.
- * \ingroup raul
- */
-class AtomicInt {
-public:
- inline AtomicInt(int val)
- { g_atomic_int_set(static_cast<volatile gint*>(&_val), val); }
-
- inline AtomicInt(const AtomicInt& copy)
- { g_atomic_int_set(static_cast<volatile gint*>(&_val), copy.get()); }
-
- inline int get() const
- { return g_atomic_int_get(static_cast<volatile gint*>(&_val)); }
-
- inline void operator=(int val)
- { g_atomic_int_set(static_cast<volatile gint*>(&_val), val); }
-
- inline void operator+=(int val)
- { g_atomic_int_add(static_cast<volatile gint*>(&_val), val); }
-
- inline void operator-=(int val)
- { g_atomic_int_add(static_cast<volatile gint*>(&_val), -val); }
-
- inline bool operator==(int val) const
- { return get() == val; }
-
- inline int operator+(int val) const
- { return get() + val; }
-
- inline AtomicInt& operator++() // prefix
- { g_atomic_int_inc(static_cast<volatile gint*>(&_val)); return *this; }
-
- inline AtomicInt& operator--() // prefix
- { g_atomic_int_add(static_cast<volatile gint*>(&_val), -1); return *this; }
-
- /** Set value to @a val iff current value is @a old.
- * @return true iff set succeeded.
- */
- inline bool compare_and_exchange(int old, int val)
- { return g_atomic_int_compare_and_exchange(static_cast<volatile gint*>(&_val), old, val); }
-
- /** Add val to value.
- * @return value immediately before addition took place.
- */
- inline int exchange_and_add(int val) {
-#if GLIB_CHECK_VERSION(2, 30, 0)
- return g_atomic_int_add(static_cast<volatile gint*>(&_val), val);
-#else
- return g_atomic_int_exchange_and_add(static_cast<volatile gint*>(&_val), val);
-#endif
- }
-
- /** Decrement value.
- * @return true if value is now 0, otherwise false.
- */
- inline bool decrement_and_test()
- { return g_atomic_int_dec_and_test(static_cast<volatile gint*>(&_val)); }
-
-private:
- volatile mutable int _val;
+ int _val;
};
} // namespace Raul
-#endif // RAUL_CPP0x
-
#endif // RAUL_ATOMIC_INT_HPP