0b72ce9adf9b170cd960f75a98cc72c769be0147
[profile/ivi/qtbase.git] / src / corelib / thread / qatomic.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtCore/qglobal.h>
43
44 #ifndef QATOMIC_H
45 #define QATOMIC_H
46
47 #include <QtCore/qbasicatomic.h>
48
49 QT_BEGIN_HEADER
50
51 QT_BEGIN_NAMESPACE
52
53 QT_MODULE(Core)
54
55 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)
56 # pragma GCC diagnostic push
57 # pragma GCC diagnostic ignored "-Wextra"
58 #endif
59
60 // High-level atomic integer operations
61 class Q_CORE_EXPORT QAtomicInt : public QBasicAtomicInt
62 {
63 public:
64     // Non-atomic API
65     inline QAtomicInt(int value = 0)
66     {
67 #ifdef QT_ARCH_PARISC
68         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
69 #endif
70         _q_value = value;
71     }
72
73     inline QAtomicInt(const QAtomicInt &other)
74     {
75 #ifdef QT_ARCH_PARISC
76         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
77 #endif
78         store(other.load());
79     }
80
81     inline QAtomicInt &operator=(int value)
82     {
83         this->store(value);
84         return *this;
85     }
86
87     inline QAtomicInt &operator=(const QAtomicInt &other)
88     {
89         this->store(other.load());
90         return *this;
91     }
92
93     inline bool operator==(int value) const
94     {
95         return this->load() == value;
96     }
97
98     inline bool operator!=(int value) const
99     {
100         return this->load() != value;
101     }
102
103     inline operator int() const
104     {
105         return this->load();
106     }
107
108     inline bool operator!() const
109     {
110         return !this->load();
111     }
112
113 #ifdef qdoc
114     static bool isReferenceCountingNative();
115     static bool isReferenceCountingWaitFree();
116
117     bool ref();
118     bool deref();
119
120     static bool isTestAndSetNative();
121     static bool isTestAndSetWaitFree();
122
123     bool testAndSetRelaxed(int expectedValue, int newValue);
124     bool testAndSetAcquire(int expectedValue, int newValue);
125     bool testAndSetRelease(int expectedValue, int newValue);
126     bool testAndSetOrdered(int expectedValue, int newValue);
127
128     static bool isFetchAndStoreNative();
129     static bool isFetchAndStoreWaitFree();
130
131     int fetchAndStoreRelaxed(int newValue);
132     int fetchAndStoreAcquire(int newValue);
133     int fetchAndStoreRelease(int newValue);
134     int fetchAndStoreOrdered(int newValue);
135
136     static bool isFetchAndAddNative();
137     static bool isFetchAndAddWaitFree();
138
139     int fetchAndAddRelaxed(int valueToAdd);
140     int fetchAndAddAcquire(int valueToAdd);
141     int fetchAndAddRelease(int valueToAdd);
142     int fetchAndAddOrdered(int valueToAdd);
143 #endif
144 };
145
146 // High-level atomic pointer operations
147 template <typename T>
148 class QAtomicPointer : public QBasicAtomicPointer<T>
149 {
150 public:
151     inline QAtomicPointer(T *value = 0)
152     {
153 #ifdef QT_ARCH_PARISC
154         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
155 #endif
156         this->store(value);
157     }
158     inline QAtomicPointer(const QAtomicPointer<T> &other)
159     {
160 #ifdef QT_ARCH_PARISC
161         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
162 #endif
163         this->store(other.load());
164     }
165
166     inline QAtomicPointer<T> &operator=(T *value)
167     {
168         this->store(value);
169         return *this;
170     }
171
172     inline QAtomicPointer<T> &operator=(const QAtomicPointer<T> &other)
173     {
174         this->store(other.load());
175         return *this;
176     }
177
178     inline bool operator==(T *value) const
179     {
180         return this->load() == value;
181     }
182
183     inline bool operator!=(T *value) const
184     {
185         return this->load() != value;
186     }
187
188     inline bool operator!() const
189     {
190         return !this->load();
191     }
192
193     inline operator T *() const
194     {
195         return this->load();
196     }
197
198     inline T *operator->() const
199     {
200         return this->load();
201     }
202
203 #ifdef qdoc
204     static bool isTestAndSetNative();
205     static bool isTestAndSetWaitFree();
206
207     bool testAndSetRelaxed(T *expectedValue, T *newValue);
208     bool testAndSetAcquire(T *expectedValue, T *newValue);
209     bool testAndSetRelease(T *expectedValue, T *newValue);
210     bool testAndSetOrdered(T *expectedValue, T *newValue);
211
212     static bool isFetchAndStoreNative();
213     static bool isFetchAndStoreWaitFree();
214
215     T *fetchAndStoreRelaxed(T *newValue);
216     T *fetchAndStoreAcquire(T *newValue);
217     T *fetchAndStoreRelease(T *newValue);
218     T *fetchAndStoreOrdered(T *newValue);
219
220     static bool isFetchAndAddNative();
221     static bool isFetchAndAddWaitFree();
222
223     T *fetchAndAddRelaxed(qptrdiff valueToAdd);
224     T *fetchAndAddAcquire(qptrdiff valueToAdd);
225     T *fetchAndAddRelease(qptrdiff valueToAdd);
226     T *fetchAndAddOrdered(qptrdiff valueToAdd);
227 #endif
228 };
229
230 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)
231 # pragma GCC diagnostic pop
232 #endif
233
234 /*!
235     This is a helper for the assignment operators of implicitly
236     shared classes. Your assignment operator should look like this:
237
238     \snippet doc/src/snippets/code/src.corelib.thread.qatomic.h 0
239 */
240 template <typename T>
241 inline void qAtomicAssign(T *&d, T *x)
242 {
243     if (d == x)
244         return;
245     x->ref.ref();
246     if (!d->ref.deref())
247         delete d;
248     d = x;
249 }
250
251 /*!
252     This is a helper for the detach method of implicitly shared
253     classes. Your private class needs a copy constructor which copies
254     the members and sets the refcount to 1. After that, your detach
255     function should look like this:
256
257     \snippet doc/src/snippets/code/src.corelib.thread.qatomic.h 1
258 */
259 template <typename T>
260 inline void qAtomicDetach(T *&d)
261 {
262     if (d->ref.load() == 1)
263         return;
264     T *x = d;
265     d = new T(*d);
266     if (!x->ref.deref())
267         delete x;
268 }
269
270 QT_END_NAMESPACE
271 QT_END_HEADER
272
273 #endif // QATOMIC_H