summaryrefslogtreecommitdiffstats
path: root/raul
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-09-17 18:39:05 +0000
committerDavid Robillard <d@drobilla.net>2011-09-17 18:39:05 +0000
commit2f749a2e0d38c5aae58fbf9947e9386e46ea1836 (patch)
tree129e7dcd3dc10526e9dca09f289e6c43b1f7a461 /raul
parent0b3655422c4ab955bb67014405efd715b1ff7d69 (diff)
downloadraul-2f749a2e0d38c5aae58fbf9947e9386e46ea1836.tar.gz
raul-2f749a2e0d38c5aae58fbf9947e9386e46ea1836.tar.bz2
raul-2f749a2e0d38c5aae58fbf9947e9386e46ea1836.zip
C++0x AtomicInt and AtomicPtr implementation.
git-svn-id: http://svn.drobilla.net/lad/trunk/raul@3468 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'raul')
-rw-r--r--raul/AtomicInt.hpp58
-rw-r--r--raul/AtomicPtr.hpp35
2 files changed, 93 insertions, 0 deletions
diff --git a/raul/AtomicInt.hpp b/raul/AtomicInt.hpp
index 4aa6145..63d5071 100644
--- a/raul/AtomicInt.hpp
+++ b/raul/AtomicInt.hpp
@@ -18,6 +18,62 @@
#ifndef RAUL_ATOMIC_INT_HPP
#define RAUL_ATOMIC_INT_HPP
+#ifdef RAUL_CPP0x
+
+#include <atomic>
+
+/** Atomic integer.
+ * \ingroup raul
+ */
+class AtomicInt {
+public:
+ inline AtomicInt(int val) : _val(val) {}
+
+ 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+=(int val) { _val += val; }
+
+ inline void operator-=(int val) { _val -= val; }
+
+ inline bool operator==(int val) const { return _val == val; }
+
+ inline int operator+(int val) const { return _val + val; }
+
+ inline AtomicInt& operator++() { ++_val; return *this; }
+
+ inline AtomicInt& operator--() { --_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);
+ }
+
+ /** Add val to value.
+ * @return value immediately before addition took place.
+ */
+ inline int exchange_and_add(int val) {
+ return _val.fetch_add(val);
+ }
+
+ /** Decrement value.
+ * @return true if value is now 0, otherwise false.
+ */
+ inline bool decrement_and_test() {
+ return _val.fetch_add(-1) == 0;
+ }
+
+private:
+ std::atomic<int> _val;
+};
+
+#else // !RAUL_CPP0x
+
#include <glib.h>
namespace Raul {
@@ -81,4 +137,6 @@ private:
} // namespace Raul
+#endif // RAUL_CPP0x
+
#endif // RAUL_ATOMIC_INT_HPP
diff --git a/raul/AtomicPtr.hpp b/raul/AtomicPtr.hpp
index 9c13271..28797de 100644
--- a/raul/AtomicPtr.hpp
+++ b/raul/AtomicPtr.hpp
@@ -18,6 +18,39 @@
#ifndef RAUL_ATOMIC_PTR_HPP
#define RAUL_ATOMIC_PTR_HPP
+#ifdef RAUL_CPP0x
+
+#include <atomic>
+
+namespace Raul {
+
+/** Atomic pointer.
+ * \ingroup raul
+ */
+template<typename T>
+class AtomicPtr {
+public:
+ inline AtomicPtr() : _val(NULL) {}
+
+ 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);
+ }
+
+private:
+ std::atomic<void*> _val;
+};
+
+} // namespace Raul
+
+#else // !RAUL_CPP0x
+
#include <glib.h>
namespace Raul {
@@ -51,4 +84,6 @@ private:
} // namespace Raul
+#endif // RAUL_CPP0x
+
#endif // RAUL_ATOMIC_PTR_HPP