summaryrefslogtreecommitdiffstats
path: root/raul
diff options
context:
space:
mode:
Diffstat (limited to 'raul')
-rw-r--r--raul/AtomicInt.hpp118
-rw-r--r--raul/AtomicPtr.hpp61
2 files changed, 46 insertions, 133 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
diff --git a/raul/AtomicPtr.hpp b/raul/AtomicPtr.hpp
index 00337b4..91c5c35 100644
--- a/raul/AtomicPtr.hpp
+++ b/raul/AtomicPtr.hpp
@@ -19,9 +19,7 @@
#include <cstddef>
-#ifdef RAUL_CPP0x
-
-#include <atomic>
+#include "raul/barrier.hpp"
namespace Raul {
@@ -35,56 +33,27 @@ public:
inline AtomicPtr(const AtomicPtr& copy) : _val(copy.get()) {}
- inline T* get() const { return static_cast<T*>(_val.load()); }
-
- inline void operator=(T* val) { _val = val; }
-
- /** Set value to newval iff current value is oldval */
- inline bool compare_and_exchange(void* oldval, void* newval) {
- return _val.compare_exchange_strong(oldval, newval);
+ inline T* get() const {
+ Raul::barrier();
+ return static_cast<T*>(_val);
}
-private:
- std::atomic<void*> _val;
-};
-
-} // namespace Raul
-
-#else // !RAUL_CPP0x
-
-#include <glib.h>
-
-namespace Raul {
-
-/** Atomic pointer.
- * \ingroup raul
- */
-template<typename T>
-class AtomicPtr {
-public:
- inline AtomicPtr()
- { g_atomic_pointer_set(static_cast<volatile gpointer*>(&_val), NULL); }
-
- inline AtomicPtr(const AtomicPtr& copy)
- { g_atomic_pointer_set(static_cast<volatile gpointer*>(&_val),
- static_cast<gpointer>(copy.get())); }
-
- inline T* get() const
- { return static_cast<T*>(g_atomic_pointer_get(static_cast<volatile gpointer*>(&_val))); }
-
- inline void operator=(T* val)
- { g_atomic_pointer_set(&_val, static_cast<gpointer>(val)); }
+ inline void operator=(T* val) {
+ _val = val;
+ Raul::barrier();
+ }
- /** Set value to newval iff current value is oldval */
- inline bool compare_and_exchange(gpointer oldval, gpointer newval)
- { return g_atomic_pointer_compare_and_exchange(&_val, oldval, newval); }
+ /** Set value to @a val iff current value is @a old.
+ * @return true iff set succeeded.
+ */
+ inline bool compare_and_exchange(void* old, void* val) {
+ return __sync_bool_compare_and_swap(&_val, old, val);
+ }
private:
- mutable volatile gpointer _val;
+ void* _val;
};
} // namespace Raul
-#endif // RAUL_CPP0x
-
#endif // RAUL_ATOMIC_PTR_HPP