Line data Source code
1 : /****************************************************************************
2 : **
3 : ** Copyright (C) 2011 Thiago Macieira <thiago@kde.org>
4 : ** Copyright (C) 2016 Intel Corporation.
5 : ** Contact: https://www.qt.io/licensing/
6 : **
7 : ** This file is part of the QtCore module of the Qt Toolkit.
8 : **
9 : ** $QT_BEGIN_LICENSE:LGPL$
10 : ** Commercial License Usage
11 : ** Licensees holding valid commercial Qt licenses may use this file in
12 : ** accordance with the commercial license agreement provided with the
13 : ** Software or, alternatively, in accordance with the terms contained in
14 : ** a written agreement between you and The Qt Company. For licensing terms
15 : ** and conditions see https://www.qt.io/terms-conditions. For further
16 : ** information use the contact form at https://www.qt.io/contact-us.
17 : **
18 : ** GNU Lesser General Public License Usage
19 : ** Alternatively, this file may be used under the terms of the GNU Lesser
20 : ** General Public License version 3 as published by the Free Software
21 : ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 : ** packaging of this file. Please review the following information to
23 : ** ensure the GNU Lesser General Public License version 3 requirements
24 : ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 : **
26 : ** GNU General Public License Usage
27 : ** Alternatively, this file may be used under the terms of the GNU
28 : ** General Public License version 2.0 or (at your option) the GNU General
29 : ** Public license version 3 or any later version approved by the KDE Free
30 : ** Qt Foundation. The licenses are as published by the Free Software
31 : ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 : ** included in the packaging of this file. Please review the following
33 : ** information to ensure the GNU General Public License requirements will
34 : ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 : ** https://www.gnu.org/licenses/gpl-3.0.html.
36 : **
37 : ** $QT_END_LICENSE$
38 : **
39 : ****************************************************************************/
40 :
41 : #include <QtCore/qatomic.h>
42 :
43 : #ifndef QBASICATOMIC_H
44 : #define QBASICATOMIC_H
45 :
46 : #if defined(QT_BOOTSTRAPPED)
47 : # include <QtCore/qatomic_bootstrap.h>
48 :
49 : // If C++11 atomics are supported, use them!
50 : // Note that constexpr support is sometimes disabled in QNX builds but its
51 : // library has <atomic>.
52 : #elif defined(Q_COMPILER_ATOMICS) && (defined(Q_COMPILER_CONSTEXPR) || defined(Q_OS_QNX))
53 : # include <QtCore/qatomic_cxx11.h>
54 :
55 : // We only support one fallback: MSVC, because even on version 2015, it lacks full constexpr support
56 : #elif defined(Q_CC_MSVC)
57 : # include <QtCore/qatomic_msvc.h>
58 :
59 : // No fallback
60 : #else
61 : # error "Qt requires C++11 support"
62 : #endif
63 :
64 : QT_WARNING_PUSH
65 : QT_WARNING_DISABLE_MSVC(4522)
66 :
67 : QT_BEGIN_NAMESPACE
68 :
69 : #if 0
70 : // silence syncqt warnings
71 : QT_END_NAMESPACE
72 : #pragma qt_no_master_include
73 : #pragma qt_sync_stop_processing
74 : #endif
75 :
76 : // New atomics
77 :
78 : #if defined(Q_COMPILER_CONSTEXPR) && defined(Q_COMPILER_DEFAULT_MEMBERS) && defined(Q_COMPILER_DELETE_MEMBERS)
79 : # if defined(Q_CC_CLANG) && Q_CC_CLANG < 303
80 : /*
81 : Do not define QT_BASIC_ATOMIC_HAS_CONSTRUCTORS for Clang before version 3.3.
82 : For details about the bug: see http://llvm.org/bugs/show_bug.cgi?id=12670
83 : */
84 : # else
85 : # define QT_BASIC_ATOMIC_HAS_CONSTRUCTORS
86 : # endif
87 : #endif
88 :
89 : template <typename T>
90 : class QBasicAtomicInteger
91 : {
92 : public:
93 : typedef QAtomicOps<T> Ops;
94 : // static check that this is a valid integer
95 : Q_STATIC_ASSERT_X(QTypeInfo<T>::isIntegral, "template parameter is not an integral type");
96 : Q_STATIC_ASSERT_X(QAtomicOpsSupport<sizeof(T)>::IsSupported, "template parameter is an integral of a size not supported on this platform");
97 :
98 : typename Ops::Type _q_value;
99 :
100 : // Everything below is either implemented in ../arch/qatomic_XXX.h or (as fallback) in qgenericatomic.h
101 :
102 24643 : T load() const Q_DECL_NOTHROW { return Ops::load(_q_value); }
103 : void store(T newValue) Q_DECL_NOTHROW { Ops::store(_q_value, newValue); }
104 :
105 : T loadAcquire() const Q_DECL_NOTHROW { return Ops::loadAcquire(_q_value); }
106 : void storeRelease(T newValue) Q_DECL_NOTHROW { Ops::storeRelease(_q_value, newValue); }
107 : operator T() const Q_DECL_NOTHROW { return loadAcquire(); }
108 : T operator=(T newValue) Q_DECL_NOTHROW { storeRelease(newValue); return newValue; }
109 :
110 : static Q_DECL_CONSTEXPR bool isReferenceCountingNative() Q_DECL_NOTHROW { return Ops::isReferenceCountingNative(); }
111 : static Q_DECL_CONSTEXPR bool isReferenceCountingWaitFree() Q_DECL_NOTHROW { return Ops::isReferenceCountingWaitFree(); }
112 :
113 5747 : bool ref() Q_DECL_NOTHROW { return Ops::ref(_q_value); }
114 10336 : bool deref() Q_DECL_NOTHROW { return Ops::deref(_q_value); }
115 :
116 : static Q_DECL_CONSTEXPR bool isTestAndSetNative() Q_DECL_NOTHROW { return Ops::isTestAndSetNative(); }
117 : static Q_DECL_CONSTEXPR bool isTestAndSetWaitFree() Q_DECL_NOTHROW { return Ops::isTestAndSetWaitFree(); }
118 :
119 : bool testAndSetRelaxed(T expectedValue, T newValue) Q_DECL_NOTHROW
120 : { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue); }
121 : bool testAndSetAcquire(T expectedValue, T newValue) Q_DECL_NOTHROW
122 : { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue); }
123 : bool testAndSetRelease(T expectedValue, T newValue) Q_DECL_NOTHROW
124 : { return Ops::testAndSetRelease(_q_value, expectedValue, newValue); }
125 : bool testAndSetOrdered(T expectedValue, T newValue) Q_DECL_NOTHROW
126 : { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue); }
127 :
128 : bool testAndSetRelaxed(T expectedValue, T newValue, T ¤tValue) Q_DECL_NOTHROW
129 : { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue, ¤tValue); }
130 : bool testAndSetAcquire(T expectedValue, T newValue, T ¤tValue) Q_DECL_NOTHROW
131 : { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue, ¤tValue); }
132 : bool testAndSetRelease(T expectedValue, T newValue, T ¤tValue) Q_DECL_NOTHROW
133 : { return Ops::testAndSetRelease(_q_value, expectedValue, newValue, ¤tValue); }
134 : bool testAndSetOrdered(T expectedValue, T newValue, T ¤tValue) Q_DECL_NOTHROW
135 : { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue, ¤tValue); }
136 :
137 : static Q_DECL_CONSTEXPR bool isFetchAndStoreNative() Q_DECL_NOTHROW { return Ops::isFetchAndStoreNative(); }
138 : static Q_DECL_CONSTEXPR bool isFetchAndStoreWaitFree() Q_DECL_NOTHROW { return Ops::isFetchAndStoreWaitFree(); }
139 :
140 : T fetchAndStoreRelaxed(T newValue) Q_DECL_NOTHROW
141 : { return Ops::fetchAndStoreRelaxed(_q_value, newValue); }
142 : T fetchAndStoreAcquire(T newValue) Q_DECL_NOTHROW
143 : { return Ops::fetchAndStoreAcquire(_q_value, newValue); }
144 : T fetchAndStoreRelease(T newValue) Q_DECL_NOTHROW
145 : { return Ops::fetchAndStoreRelease(_q_value, newValue); }
146 : T fetchAndStoreOrdered(T newValue) Q_DECL_NOTHROW
147 : { return Ops::fetchAndStoreOrdered(_q_value, newValue); }
148 :
149 : static Q_DECL_CONSTEXPR bool isFetchAndAddNative() Q_DECL_NOTHROW { return Ops::isFetchAndAddNative(); }
150 : static Q_DECL_CONSTEXPR bool isFetchAndAddWaitFree() Q_DECL_NOTHROW { return Ops::isFetchAndAddWaitFree(); }
151 :
152 : T fetchAndAddRelaxed(T valueToAdd) Q_DECL_NOTHROW
153 : { return Ops::fetchAndAddRelaxed(_q_value, valueToAdd); }
154 : T fetchAndAddAcquire(T valueToAdd) Q_DECL_NOTHROW
155 : { return Ops::fetchAndAddAcquire(_q_value, valueToAdd); }
156 : T fetchAndAddRelease(T valueToAdd) Q_DECL_NOTHROW
157 : { return Ops::fetchAndAddRelease(_q_value, valueToAdd); }
158 : T fetchAndAddOrdered(T valueToAdd) Q_DECL_NOTHROW
159 : { return Ops::fetchAndAddOrdered(_q_value, valueToAdd); }
160 :
161 : T fetchAndSubRelaxed(T valueToAdd) Q_DECL_NOTHROW
162 : { return Ops::fetchAndSubRelaxed(_q_value, valueToAdd); }
163 : T fetchAndSubAcquire(T valueToAdd) Q_DECL_NOTHROW
164 : { return Ops::fetchAndSubAcquire(_q_value, valueToAdd); }
165 : T fetchAndSubRelease(T valueToAdd) Q_DECL_NOTHROW
166 : { return Ops::fetchAndSubRelease(_q_value, valueToAdd); }
167 : T fetchAndSubOrdered(T valueToAdd) Q_DECL_NOTHROW
168 : { return Ops::fetchAndSubOrdered(_q_value, valueToAdd); }
169 :
170 : T fetchAndAndRelaxed(T valueToAdd) Q_DECL_NOTHROW
171 : { return Ops::fetchAndAndRelaxed(_q_value, valueToAdd); }
172 : T fetchAndAndAcquire(T valueToAdd) Q_DECL_NOTHROW
173 : { return Ops::fetchAndAndAcquire(_q_value, valueToAdd); }
174 : T fetchAndAndRelease(T valueToAdd) Q_DECL_NOTHROW
175 : { return Ops::fetchAndAndRelease(_q_value, valueToAdd); }
176 : T fetchAndAndOrdered(T valueToAdd) Q_DECL_NOTHROW
177 : { return Ops::fetchAndAndOrdered(_q_value, valueToAdd); }
178 :
179 : T fetchAndOrRelaxed(T valueToAdd) Q_DECL_NOTHROW
180 : { return Ops::fetchAndOrRelaxed(_q_value, valueToAdd); }
181 : T fetchAndOrAcquire(T valueToAdd) Q_DECL_NOTHROW
182 : { return Ops::fetchAndOrAcquire(_q_value, valueToAdd); }
183 : T fetchAndOrRelease(T valueToAdd) Q_DECL_NOTHROW
184 : { return Ops::fetchAndOrRelease(_q_value, valueToAdd); }
185 : T fetchAndOrOrdered(T valueToAdd) Q_DECL_NOTHROW
186 : { return Ops::fetchAndOrOrdered(_q_value, valueToAdd); }
187 :
188 : T fetchAndXorRelaxed(T valueToAdd) Q_DECL_NOTHROW
189 : { return Ops::fetchAndXorRelaxed(_q_value, valueToAdd); }
190 : T fetchAndXorAcquire(T valueToAdd) Q_DECL_NOTHROW
191 : { return Ops::fetchAndXorAcquire(_q_value, valueToAdd); }
192 : T fetchAndXorRelease(T valueToAdd) Q_DECL_NOTHROW
193 : { return Ops::fetchAndXorRelease(_q_value, valueToAdd); }
194 : T fetchAndXorOrdered(T valueToAdd) Q_DECL_NOTHROW
195 : { return Ops::fetchAndXorOrdered(_q_value, valueToAdd); }
196 :
197 : T operator++() Q_DECL_NOTHROW
198 : { return fetchAndAddOrdered(1) + 1; }
199 : T operator++(int) Q_DECL_NOTHROW
200 : { return fetchAndAddOrdered(1); }
201 : T operator--() Q_DECL_NOTHROW
202 : { return fetchAndSubOrdered(1) - 1; }
203 : T operator--(int) Q_DECL_NOTHROW
204 : { return fetchAndSubOrdered(1); }
205 :
206 : T operator+=(T v) Q_DECL_NOTHROW
207 : { return fetchAndAddOrdered(v) + v; }
208 : T operator-=(T v) Q_DECL_NOTHROW
209 : { return fetchAndSubOrdered(v) - v; }
210 : T operator&=(T v) Q_DECL_NOTHROW
211 : { return fetchAndAndOrdered(v) & v; }
212 : T operator|=(T v) Q_DECL_NOTHROW
213 : { return fetchAndOrOrdered(v) | v; }
214 : T operator^=(T v) Q_DECL_NOTHROW
215 : { return fetchAndXorOrdered(v) ^ v; }
216 :
217 :
218 : #ifdef QT_BASIC_ATOMIC_HAS_CONSTRUCTORS
219 : QBasicAtomicInteger() = default;
220 100 : constexpr QBasicAtomicInteger(T value) Q_DECL_NOTHROW : _q_value(value) {}
221 : QBasicAtomicInteger(const QBasicAtomicInteger &) = delete;
222 : QBasicAtomicInteger &operator=(const QBasicAtomicInteger &) = delete;
223 : QBasicAtomicInteger &operator=(const QBasicAtomicInteger &) volatile = delete;
224 : #endif
225 : };
226 : typedef QBasicAtomicInteger<int> QBasicAtomicInt;
227 :
228 : template <typename X>
229 : class QBasicAtomicPointer
230 : {
231 : public:
232 : typedef X *Type;
233 : typedef QAtomicOps<Type> Ops;
234 : typedef typename Ops::Type AtomicType;
235 :
236 : AtomicType _q_value;
237 :
238 : Type load() const Q_DECL_NOTHROW { return Ops::load(_q_value); }
239 : void store(Type newValue) Q_DECL_NOTHROW { Ops::store(_q_value, newValue); }
240 : operator Type() const Q_DECL_NOTHROW { return loadAcquire(); }
241 : Type operator=(Type newValue) Q_DECL_NOTHROW { storeRelease(newValue); return newValue; }
242 :
243 : // Atomic API, implemented in qatomic_XXX.h
244 : Type loadAcquire() const Q_DECL_NOTHROW { return Ops::loadAcquire(_q_value); }
245 : void storeRelease(Type newValue) Q_DECL_NOTHROW { Ops::storeRelease(_q_value, newValue); }
246 :
247 : static Q_DECL_CONSTEXPR bool isTestAndSetNative() Q_DECL_NOTHROW { return Ops::isTestAndSetNative(); }
248 : static Q_DECL_CONSTEXPR bool isTestAndSetWaitFree() Q_DECL_NOTHROW { return Ops::isTestAndSetWaitFree(); }
249 :
250 : bool testAndSetRelaxed(Type expectedValue, Type newValue) Q_DECL_NOTHROW
251 : { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue); }
252 : bool testAndSetAcquire(Type expectedValue, Type newValue) Q_DECL_NOTHROW
253 : { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue); }
254 : bool testAndSetRelease(Type expectedValue, Type newValue) Q_DECL_NOTHROW
255 : { return Ops::testAndSetRelease(_q_value, expectedValue, newValue); }
256 : bool testAndSetOrdered(Type expectedValue, Type newValue) Q_DECL_NOTHROW
257 : { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue); }
258 :
259 : bool testAndSetRelaxed(Type expectedValue, Type newValue, Type ¤tValue) Q_DECL_NOTHROW
260 : { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue, ¤tValue); }
261 : bool testAndSetAcquire(Type expectedValue, Type newValue, Type ¤tValue) Q_DECL_NOTHROW
262 : { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue, ¤tValue); }
263 : bool testAndSetRelease(Type expectedValue, Type newValue, Type ¤tValue) Q_DECL_NOTHROW
264 : { return Ops::testAndSetRelease(_q_value, expectedValue, newValue, ¤tValue); }
265 : bool testAndSetOrdered(Type expectedValue, Type newValue, Type ¤tValue) Q_DECL_NOTHROW
266 : { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue, ¤tValue); }
267 :
268 : static Q_DECL_CONSTEXPR bool isFetchAndStoreNative() Q_DECL_NOTHROW { return Ops::isFetchAndStoreNative(); }
269 : static Q_DECL_CONSTEXPR bool isFetchAndStoreWaitFree() Q_DECL_NOTHROW { return Ops::isFetchAndStoreWaitFree(); }
270 :
271 : Type fetchAndStoreRelaxed(Type newValue) Q_DECL_NOTHROW
272 : { return Ops::fetchAndStoreRelaxed(_q_value, newValue); }
273 : Type fetchAndStoreAcquire(Type newValue) Q_DECL_NOTHROW
274 : { return Ops::fetchAndStoreAcquire(_q_value, newValue); }
275 : Type fetchAndStoreRelease(Type newValue) Q_DECL_NOTHROW
276 : { return Ops::fetchAndStoreRelease(_q_value, newValue); }
277 : Type fetchAndStoreOrdered(Type newValue) Q_DECL_NOTHROW
278 : { return Ops::fetchAndStoreOrdered(_q_value, newValue); }
279 :
280 : static Q_DECL_CONSTEXPR bool isFetchAndAddNative() Q_DECL_NOTHROW { return Ops::isFetchAndAddNative(); }
281 : static Q_DECL_CONSTEXPR bool isFetchAndAddWaitFree() Q_DECL_NOTHROW { return Ops::isFetchAndAddWaitFree(); }
282 :
283 : Type fetchAndAddRelaxed(qptrdiff valueToAdd) Q_DECL_NOTHROW
284 : { return Ops::fetchAndAddRelaxed(_q_value, valueToAdd); }
285 : Type fetchAndAddAcquire(qptrdiff valueToAdd) Q_DECL_NOTHROW
286 : { return Ops::fetchAndAddAcquire(_q_value, valueToAdd); }
287 : Type fetchAndAddRelease(qptrdiff valueToAdd) Q_DECL_NOTHROW
288 : { return Ops::fetchAndAddRelease(_q_value, valueToAdd); }
289 : Type fetchAndAddOrdered(qptrdiff valueToAdd) Q_DECL_NOTHROW
290 : { return Ops::fetchAndAddOrdered(_q_value, valueToAdd); }
291 :
292 : Type fetchAndSubRelaxed(qptrdiff valueToAdd) Q_DECL_NOTHROW
293 : { return Ops::fetchAndSubRelaxed(_q_value, valueToAdd); }
294 : Type fetchAndSubAcquire(qptrdiff valueToAdd) Q_DECL_NOTHROW
295 : { return Ops::fetchAndSubAcquire(_q_value, valueToAdd); }
296 : Type fetchAndSubRelease(qptrdiff valueToAdd) Q_DECL_NOTHROW
297 : { return Ops::fetchAndSubRelease(_q_value, valueToAdd); }
298 : Type fetchAndSubOrdered(qptrdiff valueToAdd) Q_DECL_NOTHROW
299 : { return Ops::fetchAndSubOrdered(_q_value, valueToAdd); }
300 :
301 : Type operator++() Q_DECL_NOTHROW
302 : { return fetchAndAddOrdered(1) + 1; }
303 : Type operator++(int) Q_DECL_NOTHROW
304 : { return fetchAndAddOrdered(1); }
305 : Type operator--() Q_DECL_NOTHROW
306 : { return fetchAndSubOrdered(1) - 1; }
307 : Type operator--(int) Q_DECL_NOTHROW
308 : { return fetchAndSubOrdered(1); }
309 : Type operator+=(qptrdiff valueToAdd) Q_DECL_NOTHROW
310 : { return fetchAndAddOrdered(valueToAdd) + valueToAdd; }
311 : Type operator-=(qptrdiff valueToSub) Q_DECL_NOTHROW
312 : { return fetchAndSubOrdered(valueToSub) - valueToSub; }
313 :
314 : #ifdef QT_BASIC_ATOMIC_HAS_CONSTRUCTORS
315 : QBasicAtomicPointer() = default;
316 : constexpr QBasicAtomicPointer(Type value) Q_DECL_NOTHROW : _q_value(value) {}
317 : QBasicAtomicPointer(const QBasicAtomicPointer &) = delete;
318 : QBasicAtomicPointer &operator=(const QBasicAtomicPointer &) = delete;
319 : QBasicAtomicPointer &operator=(const QBasicAtomicPointer &) volatile = delete;
320 : #endif
321 : };
322 :
323 : #ifndef Q_BASIC_ATOMIC_INITIALIZER
324 : # define Q_BASIC_ATOMIC_INITIALIZER(a) { (a) }
325 : #endif
326 :
327 : QT_END_NAMESPACE
328 :
329 : QT_WARNING_POP
330 :
331 : #endif // QBASICATOMIC_H
|