X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cde76cf2a96d43f62ecb7e18c51f73e871a6d4bd..13d843f1c15fe721f03d933c600cd5ca62944003:/include/wx/atomic.h diff --git a/include/wx/atomic.h b/include/wx/atomic.h index 9918f63ecc..829e29bdd3 100644 --- a/include/wx/atomic.h +++ b/include/wx/atomic.h @@ -25,46 +25,50 @@ #if wxUSE_THREADS -#if defined(__WXMSW__) +#if defined(HAVE_GCC_ATOMIC_BUILTINS) -// include standard Windows headers -#include "wx/msw/wrapwin.h" +// NB: we intentionally don't use Linux's asm/atomic.h header, because it's +// an internal kernel header that doesn't always work in userspace: +// http://bugs.mysql.com/bug.php?id=28456 +// http://golubenco.org/blog/atomic-operations/ inline void wxAtomicInc (wxUint32 &value) { - InterlockedIncrement ((LONG*)&value); + __sync_fetch_and_add(&value, 1); } inline wxUint32 wxAtomicDec (wxUint32 &value) { - return InterlockedDecrement ((LONG*)&value); + return __sync_sub_and_fetch(&value, 1); } -#elif defined(__WXMAC__) || defined(__DARWIN__) -#include "libkern/OSAtomic.h" +#elif defined(__WXMSW__) + +// include standard Windows headers +#include "wx/msw/wrapwin.h" + inline void wxAtomicInc (wxUint32 &value) { - OSAtomicIncrement32 ((int32_t*)&value); + InterlockedIncrement ((LONG*)&value); } inline wxUint32 wxAtomicDec (wxUint32 &value) { - return OSAtomicDecrement32 ((int32_t*)&value); + return InterlockedDecrement ((LONG*)&value); } -#elif defined(__LINUX__) - -#include +#elif defined(__WXMAC__) || defined(__DARWIN__) +#include "libkern/OSAtomic.h" inline void wxAtomicInc (wxUint32 &value) { - atomic_inc ((atomic_t*)&value); + OSAtomicIncrement32 ((int32_t*)&value); } inline wxUint32 wxAtomicDec (wxUint32 &value) { - return atomic_dec_and_test ((atomic_t*)&value) ? 0 : 1; + return OSAtomicDecrement32 ((int32_t*)&value); } #elif defined (__SOLARIS__) @@ -111,13 +115,14 @@ inline wxUint32 wxAtomicDec (wxUint32 &value) { return --value; } class wxAtomicInt32 { public: - wxAtomicInt() { } // non initialized for consistency with basic int type - wxAtomicInt(wxInt32 v) : m_value(v) { } + wxAtomicInt32() { } // non initialized for consistency with basic int type + wxAtomicInt32(wxInt32 v) : m_value(v) { } + wxAtomicInt32(const wxAtomicInt32& a) : m_value(a.m_value) {} operator wxInt32() const { return m_value; } - operator wxInt32&() { return m_value; } + operator volatile wxInt32&() { return m_value; } - wxAtomicInt& operator=(wxInt32 v) { m_value = v; return *this; } + wxAtomicInt32& operator=(wxInt32 v) { m_value = v; return *this; } void Inc() { @@ -136,8 +141,8 @@ private: wxCriticalSection m_locker; }; -inline void wxAtomicInc(wxAtomicInt &value) { value.Inc(); } -inline wxInt32 wxAtomicDec(wxAtomicInt &value) { return value.Dec(); } +inline void wxAtomicInc(wxAtomicInt32 &value) { value.Inc(); } +inline wxInt32 wxAtomicDec(wxAtomicInt32 &value) { return value.Dec(); } #else // !wxHAS_GENERIC_ATOMIC_OPS