1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: functions to manipulate atomically integers and pointers
4 // Author: Armel Asselin
7 // Copyright: (c) Armel Asselin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // get the value of wxUSE_THREADS configuration flag
21 // constraints on the various functions:
22 // - wxAtomicDec must return a zero value if the value is zero once
23 // decremented else it must return any non-zero value (the true value is OK
24 // but not necessary).
28 #if defined(HAVE_GCC_ATOMIC_BUILTINS)
30 // NB: we intentionally don't use Linux's asm/atomic.h header, because it's
31 // an internal kernel header that doesn't always work in userspace:
32 // http://bugs.mysql.com/bug.php?id=28456
33 // http://golubenco.org/blog/atomic-operations/
35 inline void wxAtomicInc (wxUint32
&value
)
37 __sync_fetch_and_add(&value
, 1);
40 inline wxUint32
wxAtomicDec (wxUint32
&value
)
42 return __sync_sub_and_fetch(&value
, 1);
46 #elif defined(__WINDOWS__)
48 // include standard Windows headers
49 #include "wx/msw/wrapwin.h"
51 inline void wxAtomicInc (wxUint32
&value
)
53 InterlockedIncrement ((LONG
*)&value
);
56 inline wxUint32
wxAtomicDec (wxUint32
&value
)
58 return InterlockedDecrement ((LONG
*)&value
);
61 #elif defined(__WXMAC__) || defined(__DARWIN__)
63 #include "libkern/OSAtomic.h"
64 inline void wxAtomicInc (wxUint32
&value
)
66 OSAtomicIncrement32 ((int32_t*)&value
);
69 inline wxUint32
wxAtomicDec (wxUint32
&value
)
71 return OSAtomicDecrement32 ((int32_t*)&value
);
74 #elif defined (__SOLARIS__)
78 inline void wxAtomicInc (wxUint32
&value
)
80 atomic_add_32 ((uint32_t*)&value
, 1);
83 inline wxUint32
wxAtomicDec (wxUint32
&value
)
85 return atomic_add_32_nv ((uint32_t*)&value
, (uint32_t)-1);
88 #else // unknown platform
90 // it will result in inclusion if the generic implementation code a bit later in this page
91 #define wxNEEDS_GENERIC_ATOMIC_OPS
93 #endif // unknown platform
95 #else // else of wxUSE_THREADS
96 // if no threads are used we can safely use simple ++/--
98 inline void wxAtomicInc (wxUint32
&value
) { ++value
; }
99 inline wxUint32
wxAtomicDec (wxUint32
&value
) { return --value
; }
101 #endif // !wxUSE_THREADS
103 // ----------------------------------------------------------------------------
104 // proxies to actual implementations, but for various other types with same
106 // ----------------------------------------------------------------------------
108 #ifdef wxNEEDS_GENERIC_ATOMIC_OPS
110 #include "wx/thread.h" // for wxCriticalSection
115 wxAtomicInt32() { } // non initialized for consistency with basic int type
116 wxAtomicInt32(wxInt32 v
) : m_value(v
) { }
117 wxAtomicInt32(const wxAtomicInt32
& a
) : m_value(a
.m_value
) {}
119 operator wxInt32() const { return m_value
; }
120 operator volatile wxInt32
&() { return m_value
; }
122 wxAtomicInt32
& operator=(wxInt32 v
) { m_value
= v
; return *this; }
126 wxCriticalSectionLocker
lock(m_locker
);
132 wxCriticalSectionLocker
lock(m_locker
);
137 volatile wxInt32 m_value
;
138 wxCriticalSection m_locker
;
141 inline void wxAtomicInc(wxAtomicInt32
&value
) { value
.Inc(); }
142 inline wxInt32
wxAtomicDec(wxAtomicInt32
&value
) { return value
.Dec(); }
144 #else // !wxNEEDS_GENERIC_ATOMIC_OPS
146 #define wxHAS_ATOMIC_OPS
148 inline void wxAtomicInc(wxInt32
&value
) { wxAtomicInc((wxUint32
&)value
); }
149 inline wxInt32
wxAtomicDec(wxInt32
&value
) { return wxAtomicDec((wxUint32
&)value
); }
151 typedef wxInt32 wxAtomicInt32
;
153 #endif // wxNEEDS_GENERIC_ATOMIC_OPS
155 // all the native implementations use 32 bits currently
156 // for a 64 bits implementation we could use (a future) wxAtomicInt64 as
158 typedef wxAtomicInt32 wxAtomicInt
;
160 #endif // _WX_ATOMIC_H_