1/****************************************************************************
2**
3** Copyright (C) 2011 Thiago Macieira <thiago@kde.org>
4** Copyright (C) 2018 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
64QT_WARNING_PUSH
65QT_WARNING_DISABLE_MSVC(4522)
66
67QT_BEGIN_NAMESPACE
68
69#if 0
70// silence syncqt warnings
71QT_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
89template <typename T>
90class QBasicAtomicInteger
91{
92public:
93 typedef T Type;
94 typedef QAtomicOps<T> Ops;
95 // static check that this is a valid integer
96 Q_STATIC_ASSERT_X(QTypeInfo<T>::isIntegral, "template parameter is not an integral type");
97 Q_STATIC_ASSERT_X(QAtomicOpsSupport<sizeof(T)>::IsSupported, "template parameter is an integral of a size not supported on this platform");
98
99 typename Ops::Type _q_value;
100
101 // Everything below is either implemented in ../arch/qatomic_XXX.h or (as fallback) in qgenericatomic.h
102
103 T load() const Q_DECL_NOTHROW { return Ops::load(_q_value); }
104 void store(T newValue) Q_DECL_NOTHROW { Ops::store(_q_value, newValue); }
105
106 T loadAcquire() const Q_DECL_NOTHROW { return Ops::loadAcquire(_q_value); }
107 void storeRelease(T newValue) Q_DECL_NOTHROW { Ops::storeRelease(_q_value, newValue); }
108 operator T() const Q_DECL_NOTHROW { return loadAcquire(); }
109 T operator=(T newValue) Q_DECL_NOTHROW { storeRelease(newValue); return newValue; }
110
111 static Q_DECL_CONSTEXPR bool isReferenceCountingNative() Q_DECL_NOTHROW { return Ops::isReferenceCountingNative(); }
112 static Q_DECL_CONSTEXPR bool isReferenceCountingWaitFree() Q_DECL_NOTHROW { return Ops::isReferenceCountingWaitFree(); }
113
114 bool ref() Q_DECL_NOTHROW { return Ops::ref(_q_value); }
115 bool deref() Q_DECL_NOTHROW { return Ops::deref(_q_value); }
116
117 static Q_DECL_CONSTEXPR bool isTestAndSetNative() Q_DECL_NOTHROW { return Ops::isTestAndSetNative(); }
118 static Q_DECL_CONSTEXPR bool isTestAndSetWaitFree() Q_DECL_NOTHROW { return Ops::isTestAndSetWaitFree(); }
119
120 bool testAndSetRelaxed(T expectedValue, T newValue) Q_DECL_NOTHROW
121 { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue); }
122 bool testAndSetAcquire(T expectedValue, T newValue) Q_DECL_NOTHROW
123 { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue); }
124 bool testAndSetRelease(T expectedValue, T newValue) Q_DECL_NOTHROW
125 { return Ops::testAndSetRelease(_q_value, expectedValue, newValue); }
126 bool testAndSetOrdered(T expectedValue, T newValue) Q_DECL_NOTHROW
127 { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue); }
128
129 bool testAndSetRelaxed(T expectedValue, T newValue, T &currentValue) Q_DECL_NOTHROW
130 { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue, &currentValue); }
131 bool testAndSetAcquire(T expectedValue, T newValue, T &currentValue) Q_DECL_NOTHROW
132 { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue, &currentValue); }
133 bool testAndSetRelease(T expectedValue, T newValue, T &currentValue) Q_DECL_NOTHROW
134 { return Ops::testAndSetRelease(_q_value, expectedValue, newValue, &currentValue); }
135 bool testAndSetOrdered(T expectedValue, T newValue, T &currentValue) Q_DECL_NOTHROW
136 { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue, &currentValue); }
137
138 static Q_DECL_CONSTEXPR bool isFetchAndStoreNative() Q_DECL_NOTHROW { return Ops::isFetchAndStoreNative(); }
139 static Q_DECL_CONSTEXPR bool isFetchAndStoreWaitFree() Q_DECL_NOTHROW { return Ops::isFetchAndStoreWaitFree(); }
140
141 T fetchAndStoreRelaxed(T newValue) Q_DECL_NOTHROW
142 { return Ops::fetchAndStoreRelaxed(_q_value, newValue); }
143 T fetchAndStoreAcquire(T newValue) Q_DECL_NOTHROW
144 { return Ops::fetchAndStoreAcquire(_q_value, newValue); }
145 T fetchAndStoreRelease(T newValue) Q_DECL_NOTHROW
146 { return Ops::fetchAndStoreRelease(_q_value, newValue); }
147 T fetchAndStoreOrdered(T newValue) Q_DECL_NOTHROW
148 { return Ops::fetchAndStoreOrdered(_q_value, newValue); }
149
150 static Q_DECL_CONSTEXPR bool isFetchAndAddNative() Q_DECL_NOTHROW { return Ops::isFetchAndAddNative(); }
151 static Q_DECL_CONSTEXPR bool isFetchAndAddWaitFree() Q_DECL_NOTHROW { return Ops::isFetchAndAddWaitFree(); }
152
153 T fetchAndAddRelaxed(T valueToAdd) Q_DECL_NOTHROW
154 { return Ops::fetchAndAddRelaxed(_q_value, valueToAdd); }
155 T fetchAndAddAcquire(T valueToAdd) Q_DECL_NOTHROW
156 { return Ops::fetchAndAddAcquire(_q_value, valueToAdd); }
157 T fetchAndAddRelease(T valueToAdd) Q_DECL_NOTHROW
158 { return Ops::fetchAndAddRelease(_q_value, valueToAdd); }
159 T fetchAndAddOrdered(T valueToAdd) Q_DECL_NOTHROW
160 { return Ops::fetchAndAddOrdered(_q_value, valueToAdd); }
161
162 T fetchAndSubRelaxed(T valueToAdd) Q_DECL_NOTHROW
163 { return Ops::fetchAndSubRelaxed(_q_value, valueToAdd); }
164 T fetchAndSubAcquire(T valueToAdd) Q_DECL_NOTHROW
165 { return Ops::fetchAndSubAcquire(_q_value, valueToAdd); }
166 T fetchAndSubRelease(T valueToAdd) Q_DECL_NOTHROW
167 { return Ops::fetchAndSubRelease(_q_value, valueToAdd); }
168 T fetchAndSubOrdered(T valueToAdd) Q_DECL_NOTHROW
169 { return Ops::fetchAndSubOrdered(_q_value, valueToAdd); }
170
171 T fetchAndAndRelaxed(T valueToAdd) Q_DECL_NOTHROW
172 { return Ops::fetchAndAndRelaxed(_q_value, valueToAdd); }
173 T fetchAndAndAcquire(T valueToAdd) Q_DECL_NOTHROW
174 { return Ops::fetchAndAndAcquire(_q_value, valueToAdd); }
175 T fetchAndAndRelease(T valueToAdd) Q_DECL_NOTHROW
176 { return Ops::fetchAndAndRelease(_q_value, valueToAdd); }
177 T fetchAndAndOrdered(T valueToAdd) Q_DECL_NOTHROW
178 { return Ops::fetchAndAndOrdered(_q_value, valueToAdd); }
179
180 T fetchAndOrRelaxed(T valueToAdd) Q_DECL_NOTHROW
181 { return Ops::fetchAndOrRelaxed(_q_value, valueToAdd); }
182 T fetchAndOrAcquire(T valueToAdd) Q_DECL_NOTHROW
183 { return Ops::fetchAndOrAcquire(_q_value, valueToAdd); }
184 T fetchAndOrRelease(T valueToAdd) Q_DECL_NOTHROW
185 { return Ops::fetchAndOrRelease(_q_value, valueToAdd); }
186 T fetchAndOrOrdered(T valueToAdd) Q_DECL_NOTHROW
187 { return Ops::fetchAndOrOrdered(_q_value, valueToAdd); }
188
189 T fetchAndXorRelaxed(T valueToAdd) Q_DECL_NOTHROW
190 { return Ops::fetchAndXorRelaxed(_q_value, valueToAdd); }
191 T fetchAndXorAcquire(T valueToAdd) Q_DECL_NOTHROW
192 { return Ops::fetchAndXorAcquire(_q_value, valueToAdd); }
193 T fetchAndXorRelease(T valueToAdd) Q_DECL_NOTHROW
194 { return Ops::fetchAndXorRelease(_q_value, valueToAdd); }
195 T fetchAndXorOrdered(T valueToAdd) Q_DECL_NOTHROW
196 { return Ops::fetchAndXorOrdered(_q_value, valueToAdd); }
197
198 T operator++() Q_DECL_NOTHROW
199 { return fetchAndAddOrdered(1) + 1; }
200 T operator++(int) Q_DECL_NOTHROW
201 { return fetchAndAddOrdered(1); }
202 T operator--() Q_DECL_NOTHROW
203 { return fetchAndSubOrdered(1) - 1; }
204 T operator--(int) Q_DECL_NOTHROW
205 { return fetchAndSubOrdered(1); }
206
207 T operator+=(T v) Q_DECL_NOTHROW
208 { return fetchAndAddOrdered(v) + v; }
209 T operator-=(T v) Q_DECL_NOTHROW
210 { return fetchAndSubOrdered(v) - v; }
211 T operator&=(T v) Q_DECL_NOTHROW
212 { return fetchAndAndOrdered(v) & v; }
213 T operator|=(T v) Q_DECL_NOTHROW
214 { return fetchAndOrOrdered(v) | v; }
215 T operator^=(T v) Q_DECL_NOTHROW
216 { return fetchAndXorOrdered(v) ^ v; }
217
218
219#ifdef QT_BASIC_ATOMIC_HAS_CONSTRUCTORS
220 QBasicAtomicInteger() = default;
221 constexpr QBasicAtomicInteger(T value) Q_DECL_NOTHROW : _q_value(value) {}
222 QBasicAtomicInteger(const QBasicAtomicInteger &) = delete;
223 QBasicAtomicInteger &operator=(const QBasicAtomicInteger &) = delete;
224 QBasicAtomicInteger &operator=(const QBasicAtomicInteger &) volatile = delete;
225#endif
226};
227typedef QBasicAtomicInteger<int> QBasicAtomicInt;
228
229template <typename X>
230class QBasicAtomicPointer
231{
232public:
233 typedef X *Type;
234 typedef QAtomicOps<Type> Ops;
235 typedef typename Ops::Type AtomicType;
236
237 AtomicType _q_value;
238
239 Type load() const Q_DECL_NOTHROW { return Ops::load(_q_value); }
240 void store(Type newValue) Q_DECL_NOTHROW { Ops::store(_q_value, newValue); }
241 operator Type() const Q_DECL_NOTHROW { return loadAcquire(); }
242 Type operator=(Type newValue) Q_DECL_NOTHROW { storeRelease(newValue); return newValue; }
243
244 // Atomic API, implemented in qatomic_XXX.h
245 Type loadAcquire() const Q_DECL_NOTHROW { return Ops::loadAcquire(_q_value); }
246 void storeRelease(Type newValue) Q_DECL_NOTHROW { Ops::storeRelease(_q_value, newValue); }
247
248 static Q_DECL_CONSTEXPR bool isTestAndSetNative() Q_DECL_NOTHROW { return Ops::isTestAndSetNative(); }
249 static Q_DECL_CONSTEXPR bool isTestAndSetWaitFree() Q_DECL_NOTHROW { return Ops::isTestAndSetWaitFree(); }
250
251 bool testAndSetRelaxed(Type expectedValue, Type newValue) Q_DECL_NOTHROW
252 { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue); }
253 bool testAndSetAcquire(Type expectedValue, Type newValue) Q_DECL_NOTHROW
254 { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue); }
255 bool testAndSetRelease(Type expectedValue, Type newValue) Q_DECL_NOTHROW
256 { return Ops::testAndSetRelease(_q_value, expectedValue, newValue); }
257 bool testAndSetOrdered(Type expectedValue, Type newValue) Q_DECL_NOTHROW
258 { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue); }
259
260 bool testAndSetRelaxed(Type expectedValue, Type newValue, Type &currentValue) Q_DECL_NOTHROW
261 { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue, &currentValue); }
262 bool testAndSetAcquire(Type expectedValue, Type newValue, Type &currentValue) Q_DECL_NOTHROW
263 { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue, &currentValue); }
264 bool testAndSetRelease(Type expectedValue, Type newValue, Type &currentValue) Q_DECL_NOTHROW
265 { return Ops::testAndSetRelease(_q_value, expectedValue, newValue, &currentValue); }
266 bool testAndSetOrdered(Type expectedValue, Type newValue, Type &currentValue) Q_DECL_NOTHROW
267 { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue, &currentValue); }
268
269 static Q_DECL_CONSTEXPR bool isFetchAndStoreNative() Q_DECL_NOTHROW { return Ops::isFetchAndStoreNative(); }
270 static Q_DECL_CONSTEXPR bool isFetchAndStoreWaitFree() Q_DECL_NOTHROW { return Ops::isFetchAndStoreWaitFree(); }
271
272 Type fetchAndStoreRelaxed(Type newValue) Q_DECL_NOTHROW
273 { return Ops::fetchAndStoreRelaxed(_q_value, newValue); }
274 Type fetchAndStoreAcquire(Type newValue) Q_DECL_NOTHROW
275 { return Ops::fetchAndStoreAcquire(_q_value, newValue); }
276 Type fetchAndStoreRelease(Type newValue) Q_DECL_NOTHROW
277 { return Ops::fetchAndStoreRelease(_q_value, newValue); }
278 Type fetchAndStoreOrdered(Type newValue) Q_DECL_NOTHROW
279 { return Ops::fetchAndStoreOrdered(_q_value, newValue); }
280
281 static Q_DECL_CONSTEXPR bool isFetchAndAddNative() Q_DECL_NOTHROW { return Ops::isFetchAndAddNative(); }
282 static Q_DECL_CONSTEXPR bool isFetchAndAddWaitFree() Q_DECL_NOTHROW { return Ops::isFetchAndAddWaitFree(); }
283
284 Type fetchAndAddRelaxed(qptrdiff valueToAdd) Q_DECL_NOTHROW
285 { return Ops::fetchAndAddRelaxed(_q_value, valueToAdd); }
286 Type fetchAndAddAcquire(qptrdiff valueToAdd) Q_DECL_NOTHROW
287 { return Ops::fetchAndAddAcquire(_q_value, valueToAdd); }
288 Type fetchAndAddRelease(qptrdiff valueToAdd) Q_DECL_NOTHROW
289 { return Ops::fetchAndAddRelease(_q_value, valueToAdd); }
290 Type fetchAndAddOrdered(qptrdiff valueToAdd) Q_DECL_NOTHROW
291 { return Ops::fetchAndAddOrdered(_q_value, valueToAdd); }
292
293 Type fetchAndSubRelaxed(qptrdiff valueToAdd) Q_DECL_NOTHROW
294 { return Ops::fetchAndSubRelaxed(_q_value, valueToAdd); }
295 Type fetchAndSubAcquire(qptrdiff valueToAdd) Q_DECL_NOTHROW
296 { return Ops::fetchAndSubAcquire(_q_value, valueToAdd); }
297 Type fetchAndSubRelease(qptrdiff valueToAdd) Q_DECL_NOTHROW
298 { return Ops::fetchAndSubRelease(_q_value, valueToAdd); }
299 Type fetchAndSubOrdered(qptrdiff valueToAdd) Q_DECL_NOTHROW
300 { return Ops::fetchAndSubOrdered(_q_value, valueToAdd); }
301
302 Type operator++() Q_DECL_NOTHROW
303 { return fetchAndAddOrdered(1) + 1; }
304 Type operator++(int) Q_DECL_NOTHROW
305 { return fetchAndAddOrdered(1); }
306 Type operator--() Q_DECL_NOTHROW
307 { return fetchAndSubOrdered(1) - 1; }
308 Type operator--(int) Q_DECL_NOTHROW
309 { return fetchAndSubOrdered(1); }
310 Type operator+=(qptrdiff valueToAdd) Q_DECL_NOTHROW
311 { return fetchAndAddOrdered(valueToAdd) + valueToAdd; }
312 Type operator-=(qptrdiff valueToSub) Q_DECL_NOTHROW
313 { return fetchAndSubOrdered(valueToSub) - valueToSub; }
314
315#ifdef QT_BASIC_ATOMIC_HAS_CONSTRUCTORS
316 QBasicAtomicPointer() = default;
317 constexpr QBasicAtomicPointer(Type value) Q_DECL_NOTHROW : _q_value(value) {}
318 QBasicAtomicPointer(const QBasicAtomicPointer &) = delete;
319 QBasicAtomicPointer &operator=(const QBasicAtomicPointer &) = delete;
320 QBasicAtomicPointer &operator=(const QBasicAtomicPointer &) volatile = delete;
321#endif
322};
323
324#ifndef Q_BASIC_ATOMIC_INITIALIZER
325# define Q_BASIC_ATOMIC_INITIALIZER(a) { (a) }
326#endif
327
328QT_END_NAMESPACE
329
330QT_WARNING_POP
331
332#endif // QBASICATOMIC_H
333