diff options
author | David Robillard <d@drobilla.net> | 2012-08-13 18:21:53 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2012-08-13 18:21:53 +0000 |
commit | 331cdd5eb4cacd5396e7e56dd826b320212674fd (patch) | |
tree | fc4badc5fbdbbee9302e1ee158330b10fb3c83c6 /raul/AtomicPtr.hpp | |
parent | ca660c8b407affd24802c43429b6341e76010660 (diff) | |
download | raul-331cdd5eb4cacd5396e7e56dd826b320212674fd.tar.gz raul-331cdd5eb4cacd5396e7e56dd826b320212674fd.tar.bz2 raul-331cdd5eb4cacd5396e7e56dd826b320212674fd.zip |
Drop glib for atomics and use gcc/clang builtins.
git-svn-id: http://svn.drobilla.net/lad/trunk/raul@4681 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'raul/AtomicPtr.hpp')
-rw-r--r-- | raul/AtomicPtr.hpp | 61 |
1 files changed, 15 insertions, 46 deletions
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 |