1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: functions to manipulate atomically integers and pointers
4 // Author: Armel Asselin
6 // Copyright: (c) Armel Asselin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // get the value of wxUSE_THREADS configuration flag
20 // constraints on the various functions:
21 // - wxAtomicDec must return a zero value if the value is zero once
22 // decremented else it must return any non-zero value (the true value is OK
23 // but not necessary).
27 #if defined(HAVE_GCC_ATOMIC_BUILTINS)
29 // NB: we intentionally don't use Linux's asm/atomic.h header, because it's
30 // an internal kernel header that doesn't always work in userspace:
31 // http://bugs.mysql.com/bug.php?id=28456
32 // http://golubenco.org/blog/atomic-operations/
34 inline void wxAtomicInc (wxUint32
&value
)
36 __sync_fetch_and_add(&value
, 1);
39 inline wxUint32
wxAtomicDec (wxUint32
&value
)
41 return __sync_sub_and_fetch(&value
, 1);
45 #elif defined(__WINDOWS__)
47 // include standard Windows headers
48 #include "wx/msw/wrapwin.h"
50 inline void wxAtomicInc (wxUint32
&value
)
52 InterlockedIncrement ((LONG
*)&value
);
55 inline wxUint32
wxAtomicDec (wxUint32
&value
)
57 return InterlockedDecrement ((LONG
*)&value
);
60 #elif defined(__WXMAC__) || defined(__DARWIN__)
62 #include "libkern/OSAtomic.h"
63 inline void wxAtomicInc (wxUint32
&value
)
65 OSAtomicIncrement32 ((int32_t*)&value
);
68 inline wxUint32
wxAtomicDec (wxUint32
&value
)
70 return OSAtomicDecrement32 ((int32_t*)&value
);
73 #elif defined (__SOLARIS__)
77 inline void wxAtomicInc (wxUint32
&value
)
79 atomic_add_32 ((uint32_t*)&value
, 1);
82 inline wxUint32
wxAtomicDec (wxUint32
&value
)
84 return atomic_add_32_nv ((uint32_t*)&value
, (uint32_t)-1);
87 #else // unknown platform
89 // it will result in inclusion if the generic implementation code a bit later in this page
90 #define wxNEEDS_GENERIC_ATOMIC_OPS
92 #endif // unknown platform
94 #else // else of wxUSE_THREADS
95 // if no threads are used we can safely use simple ++/--
97 inline void wxAtomicInc (wxUint32
&value
) { ++value
; }
98 inline wxUint32
wxAtomicDec (wxUint32
&value
) { return --value
; }
100 #endif // !wxUSE_THREADS
102 // ----------------------------------------------------------------------------
103 // proxies to actual implementations, but for various other types with same
105 // ----------------------------------------------------------------------------
107 #ifdef wxNEEDS_GENERIC_ATOMIC_OPS
109 #include "wx/thread.h" // for wxCriticalSection
114 wxAtomicInt32() { } // non initialized for consistency with basic int type
115 wxAtomicInt32(wxInt32 v
) : m_value(v
) { }
116 wxAtomicInt32(const wxAtomicInt32
& a
) : m_value(a
.m_value
) {}
118 operator wxInt32() const { return m_value
; }
119 operator volatile wxInt32
&() { return m_value
; }
121 wxAtomicInt32
& operator=(wxInt32 v
) { m_value
= v
; return *this; }
125 wxCriticalSectionLocker
lock(m_locker
);
131 wxCriticalSectionLocker
lock(m_locker
);
136 volatile wxInt32 m_value
;
137 wxCriticalSection m_locker
;
140 inline void wxAtomicInc(wxAtomicInt32
&value
) { value
.Inc(); }
141 inline wxInt32
wxAtomicDec(wxAtomicInt32
&value
) { return value
.Dec(); }
143 #else // !wxNEEDS_GENERIC_ATOMIC_OPS
145 #define wxHAS_ATOMIC_OPS
147 inline void wxAtomicInc(wxInt32
&value
) { wxAtomicInc((wxUint32
&)value
); }
148 inline wxInt32
wxAtomicDec(wxInt32
&value
) { return wxAtomicDec((wxUint32
&)value
); }
150 typedef wxInt32 wxAtomicInt32
;
152 #endif // wxNEEDS_GENERIC_ATOMIC_OPS
154 // all the native implementations use 32 bits currently
155 // for a 64 bits implementation we could use (a future) wxAtomicInt64 as
157 typedef wxAtomicInt32 wxAtomicInt
;
159 #endif // _WX_ATOMIC_H_