Mark QAtomic* implicit cast and other operators as deprecated
[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     Q_DECL_DEPRECATED
82     inline QAtomicInt &operator=(int value)
83     {
84         this->store(value);
85         return *this;
86     }
87
88     Q_DECL_DEPRECATED
89     inline QAtomicInt &operator=(const QAtomicInt &other)
90     {
91         this->store(other.load());
92         return *this;
93     }
94
95     Q_DECL_DEPRECATED
96     inline bool operator==(int value) const
97     {
98         return this->load() == value;
99     }
100
101     Q_DECL_DEPRECATED
102     inline bool operator!=(int value) const
103     {
104         return this->load() != value;
105     }
106
107     Q_DECL_DEPRECATED
108     inline operator int() const
109     {
110         return this->load();
111     }
112
113     Q_DECL_DEPRECATED
114     inline bool operator!() const
115     {
116         return !this->load();
117     }
118
119 #ifdef qdoc
120     static bool isReferenceCountingNative();
121     static bool isReferenceCountingWaitFree();
122
123     bool ref();
124     bool deref();
125
126     static bool isTestAndSetNative();
127     static bool isTestAndSetWaitFree();
128
129     bool testAndSetRelaxed(int expectedValue, int newValue);
130     bool testAndSetAcquire(int expectedValue, int newValue);
131     bool testAndSetRelease(int expectedValue, int newValue);
132     bool testAndSetOrdered(int expectedValue, int newValue);
133
134     static bool isFetchAndStoreNative();
135     static bool isFetchAndStoreWaitFree();
136
137     int fetchAndStoreRelaxed(int newValue);
138     int fetchAndStoreAcquire(int newValue);
139     int fetchAndStoreRelease(int newValue);
140     int fetchAndStoreOrdered(int newValue);
141
142     static bool isFetchAndAddNative();
143     static bool isFetchAndAddWaitFree();
144
145     int fetchAndAddRelaxed(int valueToAdd);
146     int fetchAndAddAcquire(int valueToAdd);
147     int fetchAndAddRelease(int valueToAdd);
148     int fetchAndAddOrdered(int valueToAdd);
149 #endif
150 };
151
152 // High-level atomic pointer operations
153 template <typename T>
154 class QAtomicPointer : public QBasicAtomicPointer<T>
155 {
156 public:
157     inline QAtomicPointer(T *value = 0)
158     {
159 #ifdef QT_ARCH_PARISC
160         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
161 #endif
162         this->store(value);
163     }
164     inline QAtomicPointer(const QAtomicPointer<T> &other)
165     {
166 #ifdef QT_ARCH_PARISC
167         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
168 #endif
169         this->store(other.load());
170     }
171
172     Q_DECL_DEPRECATED
173     inline QAtomicPointer<T> &operator=(T *value)
174     {
175         this->store(value);
176         return *this;
177     }
178
179     Q_DECL_DEPRECATED
180     inline QAtomicPointer<T> &operator=(const QAtomicPointer<T> &other)
181     {
182         this->store(other.load());
183         return *this;
184     }
185
186     Q_DECL_DEPRECATED
187     inline bool operator==(T *value) const
188     {
189         return this->load() == value;
190     }
191
192     Q_DECL_DEPRECATED
193     inline bool operator!=(T *value) const
194     {
195         return this->load() != value;
196     }
197
198     Q_DECL_DEPRECATED
199     inline bool operator!() const
200     {
201         return !this->load();
202     }
203
204     Q_DECL_DEPRECATED
205     inline operator T *() const
206     {
207         return this->load();
208     }
209
210     Q_DECL_DEPRECATED
211     inline T *operator->() const
212     {
213         return this->load();
214     }
215
216 #ifdef qdoc
217     static bool isTestAndSetNative();
218     static bool isTestAndSetWaitFree();
219
220     bool testAndSetRelaxed(T *expectedValue, T *newValue);
221     bool testAndSetAcquire(T *expectedValue, T *newValue);
222     bool testAndSetRelease(T *expectedValue, T *newValue);
223     bool testAndSetOrdered(T *expectedValue, T *newValue);
224
225     static bool isFetchAndStoreNative();
226     static bool isFetchAndStoreWaitFree();
227
228     T *fetchAndStoreRelaxed(T *newValue);
229     T *fetchAndStoreAcquire(T *newValue);
230     T *fetchAndStoreRelease(T *newValue);
231     T *fetchAndStoreOrdered(T *newValue);
232
233     static bool isFetchAndAddNative();
234     static bool isFetchAndAddWaitFree();
235
236     T *fetchAndAddRelaxed(qptrdiff valueToAdd);
237     T *fetchAndAddAcquire(qptrdiff valueToAdd);
238     T *fetchAndAddRelease(qptrdiff valueToAdd);
239     T *fetchAndAddOrdered(qptrdiff valueToAdd);
240 #endif
241 };
242
243 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)
244 # pragma GCC diagnostic pop
245 #endif
246
247 /*!
248     This is a helper for the assignment operators of implicitly
249     shared classes. Your assignment operator should look like this:
250
251     \snippet doc/src/snippets/code/src.corelib.thread.qatomic.h 0
252 */
253 template <typename T>
254 inline void qAtomicAssign(T *&d, T *x)
255 {
256     if (d == x)
257         return;
258     x->ref.ref();
259     if (!d->ref.deref())
260         delete d;
261     d = x;
262 }
263
264 /*!
265     This is a helper for the detach method of implicitly shared
266     classes. Your private class needs a copy constructor which copies
267     the members and sets the refcount to 1. After that, your detach
268     function should look like this:
269
270     \snippet doc/src/snippets/code/src.corelib.thread.qatomic.h 1
271 */
272 template <typename T>
273 inline void qAtomicDetach(T *&d)
274 {
275     if (d->ref.load() == 1)
276         return;
277     T *x = d;
278     d = new T(*d);
279     if (!x->ref.deref())
280         delete x;
281 }
282
283 QT_END_NAMESPACE
284 QT_END_HEADER
285
286 #endif // QATOMIC_H