]> git.saurik.com Git - wxWidgets.git/blame - include/wx/atomic.h
Revert "Make wxMSW stack walking methods work with Unicode identifiers."
[wxWidgets.git] / include / wx / atomic.h
CommitLineData
cde76cf2
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/atomic.h
3// Purpose: functions to manipulate atomically integers and pointers
4// Author: Armel Asselin
5// Created: 12/13/2006
cde76cf2
VZ
6// Copyright: (c) Armel Asselin
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifndef _WX_ATOMIC_H_
11#define _WX_ATOMIC_H_
12
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
16
17// get the value of wxUSE_THREADS configuration flag
18#include "wx/defs.h"
19
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).
24
25#if wxUSE_THREADS
26
47964710 27#if defined(HAVE_GCC_ATOMIC_BUILTINS)
cde76cf2 28
47964710
VS
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/
cde76cf2
VZ
33
34inline void wxAtomicInc (wxUint32 &value)
35{
47964710 36 __sync_fetch_and_add(&value, 1);
cde76cf2
VZ
37}
38
39inline wxUint32 wxAtomicDec (wxUint32 &value)
40{
47964710 41 return __sync_sub_and_fetch(&value, 1);
cde76cf2
VZ
42}
43
cde76cf2 44
bb5a9514 45#elif defined(__WINDOWS__)
47964710
VS
46
47// include standard Windows headers
48#include "wx/msw/wrapwin.h"
49
cde76cf2
VZ
50inline void wxAtomicInc (wxUint32 &value)
51{
47964710 52 InterlockedIncrement ((LONG*)&value);
cde76cf2
VZ
53}
54
55inline wxUint32 wxAtomicDec (wxUint32 &value)
56{
47964710 57 return InterlockedDecrement ((LONG*)&value);
cde76cf2
VZ
58}
59
47964710 60#elif defined(__WXMAC__) || defined(__DARWIN__)
cde76cf2 61
47964710 62#include "libkern/OSAtomic.h"
cde76cf2
VZ
63inline void wxAtomicInc (wxUint32 &value)
64{
47964710 65 OSAtomicIncrement32 ((int32_t*)&value);
cde76cf2
VZ
66}
67
68inline wxUint32 wxAtomicDec (wxUint32 &value)
69{
47964710 70 return OSAtomicDecrement32 ((int32_t*)&value);
cde76cf2
VZ
71}
72
73#elif defined (__SOLARIS__)
74
75#include <atomic.h>
76
77inline void wxAtomicInc (wxUint32 &value)
78{
79 atomic_add_32 ((uint32_t*)&value, 1);
80}
81
82inline wxUint32 wxAtomicDec (wxUint32 &value)
83{
84 return atomic_add_32_nv ((uint32_t*)&value, (uint32_t)-1);
85}
86
87#else // unknown platform
88
89// it will result in inclusion if the generic implementation code a bit later in this page
42124e68 90#define wxNEEDS_GENERIC_ATOMIC_OPS
cde76cf2
VZ
91
92#endif // unknown platform
93
94#else // else of wxUSE_THREADS
95// if no threads are used we can safely use simple ++/--
96
97inline void wxAtomicInc (wxUint32 &value) { ++value; }
98inline wxUint32 wxAtomicDec (wxUint32 &value) { return --value; }
99
100#endif // !wxUSE_THREADS
101
102// ----------------------------------------------------------------------------
103// proxies to actual implementations, but for various other types with same
104// behaviour
105// ----------------------------------------------------------------------------
106
42124e68 107#ifdef wxNEEDS_GENERIC_ATOMIC_OPS
cde76cf2 108
cde76cf2
VZ
109#include "wx/thread.h" // for wxCriticalSection
110
111class wxAtomicInt32
112{
113public:
eb096b8e
VS
114 wxAtomicInt32() { } // non initialized for consistency with basic int type
115 wxAtomicInt32(wxInt32 v) : m_value(v) { }
bc1d617a 116 wxAtomicInt32(const wxAtomicInt32& a) : m_value(a.m_value) {}
cde76cf2
VZ
117
118 operator wxInt32() const { return m_value; }
eb096b8e 119 operator volatile wxInt32&() { return m_value; }
cde76cf2 120
eb096b8e 121 wxAtomicInt32& operator=(wxInt32 v) { m_value = v; return *this; }
cde76cf2
VZ
122
123 void Inc()
124 {
125 wxCriticalSectionLocker lock(m_locker);
126 ++m_value;
127 }
128
129 wxInt32 Dec()
130 {
131 wxCriticalSectionLocker lock(m_locker);
132 return --m_value;
133 }
134
135private:
136 volatile wxInt32 m_value;
137 wxCriticalSection m_locker;
138};
139
eb096b8e
VS
140inline void wxAtomicInc(wxAtomicInt32 &value) { value.Inc(); }
141inline wxInt32 wxAtomicDec(wxAtomicInt32 &value) { return value.Dec(); }
cde76cf2 142
42124e68
VZ
143#else // !wxNEEDS_GENERIC_ATOMIC_OPS
144
145#define wxHAS_ATOMIC_OPS
cde76cf2
VZ
146
147inline void wxAtomicInc(wxInt32 &value) { wxAtomicInc((wxUint32&)value); }
148inline wxInt32 wxAtomicDec(wxInt32 &value) { return wxAtomicDec((wxUint32&)value); }
149
150typedef wxInt32 wxAtomicInt32;
151
42124e68 152#endif // wxNEEDS_GENERIC_ATOMIC_OPS
cde76cf2
VZ
153
154// all the native implementations use 32 bits currently
155// for a 64 bits implementation we could use (a future) wxAtomicInt64 as
156// default type
157typedef wxAtomicInt32 wxAtomicInt;
158
159#endif // _WX_ATOMIC_H_