From 2f749a2e0d38c5aae58fbf9947e9386e46ea1836 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 17 Sep 2011 18:39:05 +0000 Subject: C++0x AtomicInt and AtomicPtr implementation. git-svn-id: http://svn.drobilla.net/lad/trunk/raul@3468 a436a847-0d15-0410-975c-d299462d15a1 --- raul/AtomicInt.hpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ raul/AtomicPtr.hpp | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) (limited to 'raul') 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 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 _val; +}; + +#else // !RAUL_CPP0x + #include 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 + +namespace Raul { + +/** Atomic pointer. + * \ingroup raul + */ +template +class AtomicPtr { +public: + inline AtomicPtr() : _val(NULL) {} + + inline AtomicPtr(const AtomicPtr& copy) : _val(copy.get()) {} + + inline T* get() const { return static_cast(_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 _val; +}; + +} // namespace Raul + +#else // !RAUL_CPP0x + #include namespace Raul { @@ -51,4 +84,6 @@ private: } // namespace Raul +#endif // RAUL_CPP0x + #endif // RAUL_ATOMIC_PTR_HPP -- cgit v1.2.1