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/qglobal.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 or INTEGRITY builds,
51// but their libraries have <atomic>.
52#elif defined(Q_COMPILER_ATOMICS) && (defined(Q_COMPILER_CONSTEXPR) || defined(Q_OS_QNX) || defined(Q_OS_INTEGRITY))
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)
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 static_assert(QTypeInfo<T>::isIntegral, "template parameter is not an integral type");
97 static_assert(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
102 // fallback) in qgenericatomic.h
103 T loadRelaxed() const noexcept { return Ops::loadRelaxed(_q_value); }
104 void storeRelaxed(T newValue) noexcept { Ops::storeRelaxed(_q_value, newValue); }
105
106 T loadAcquire() const noexcept { return Ops::loadAcquire(_q_value); }
107 void storeRelease(T newValue) noexcept { Ops::storeRelease(_q_value, newValue); }
108 operator T() const noexcept { return loadAcquire(); }
109 T operator=(T newValue) noexcept { storeRelease(newValue); return newValue; }
110
111 static constexpr bool isReferenceCountingNative() noexcept { return Ops::isReferenceCountingNative(); }
112 static constexpr bool isReferenceCountingWaitFree() noexcept { return Ops::isReferenceCountingWaitFree(); }
113
114 bool ref() noexcept { return Ops::ref(_q_value); }
115 bool deref() noexcept { return Ops::deref(_q_value); }
116
117 static constexpr bool isTestAndSetNative() noexcept { return Ops::isTestAndSetNative(); }
118 static constexpr bool isTestAndSetWaitFree() noexcept { return Ops::isTestAndSetWaitFree(); }
119
120 bool testAndSetRelaxed(T expectedValue, T newValue) noexcept
121 { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue); }
122 bool testAndSetAcquire(T expectedValue, T newValue) noexcept
123 { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue); }
124 bool testAndSetRelease(T expectedValue, T newValue) noexcept
125 { return Ops::testAndSetRelease(_q_value, expectedValue, newValue); }
126 bool testAndSetOrdered(T expectedValue, T newValue) noexcept
127 { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue); }
128
129 bool testAndSetRelaxed(T expectedValue, T newValue, T &currentValue) noexcept
130 { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue, &currentValue); }
131 bool testAndSetAcquire(T expectedValue, T newValue, T &currentValue) noexcept
132 { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue, &currentValue); }
133 bool testAndSetRelease(T expectedValue, T newValue, T &currentValue) noexcept
134 { return Ops::testAndSetRelease(_q_value, expectedValue, newValue, &currentValue); }
135 bool testAndSetOrdered(T expectedValue, T newValue, T &currentValue) noexcept
136 { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue, &currentValue); }
137
138 static constexpr bool isFetchAndStoreNative() noexcept { return Ops::isFetchAndStoreNative(); }
139 static constexpr bool isFetchAndStoreWaitFree() noexcept { return Ops::isFetchAndStoreWaitFree(); }
140
141 T fetchAndStoreRelaxed(T newValue) noexcept
142 { return Ops::fetchAndStoreRelaxed(_q_value, newValue); }
143 T fetchAndStoreAcquire(T newValue) noexcept
144 { return Ops::fetchAndStoreAcquire(_q_value, newValue); }
145 T fetchAndStoreRelease(T newValue) noexcept
146 { return Ops::fetchAndStoreRelease(_q_value, newValue); }
147 T fetchAndStoreOrdered(T newValue) noexcept
148 { return Ops::fetchAndStoreOrdered(_q_value, newValue); }
149
150 static constexpr bool isFetchAndAddNative() noexcept { return Ops::isFetchAndAddNative(); }
151 static constexpr bool isFetchAndAddWaitFree() noexcept { return Ops::isFetchAndAddWaitFree(); }
152
153 T fetchAndAddRelaxed(T valueToAdd) noexcept
154 { return Ops::fetchAndAddRelaxed(_q_value, valueToAdd); }
155 T fetchAndAddAcquire(T valueToAdd) noexcept
156 { return Ops::fetchAndAddAcquire(_q_value, valueToAdd); }
157 T fetchAndAddRelease(T valueToAdd) noexcept
158 { return Ops::fetchAndAddRelease(_q_value, valueToAdd); }
159 T fetchAndAddOrdered(T valueToAdd) noexcept
160 { return Ops::fetchAndAddOrdered(_q_value, valueToAdd); }
161
162 T fetchAndSubRelaxed(T valueToAdd) noexcept
163 { return Ops::fetchAndSubRelaxed(_q_value, valueToAdd); }
164 T fetchAndSubAcquire(T valueToAdd) noexcept
165 { return Ops::fetchAndSubAcquire(_q_value, valueToAdd); }
166 T fetchAndSubRelease(T valueToAdd) noexcept
167 { return Ops::fetchAndSubRelease(_q_value, valueToAdd); }
168 T fetchAndSubOrdered(T valueToAdd) noexcept
169 { return Ops::fetchAndSubOrdered(_q_value, valueToAdd); }
170
171 T fetchAndAndRelaxed(T valueToAdd) noexcept
172 { return Ops::fetchAndAndRelaxed(_q_value, valueToAdd); }
173 T fetchAndAndAcquire(T valueToAdd) noexcept
174 { return Ops::fetchAndAndAcquire(_q_value, valueToAdd); }
175 T fetchAndAndRelease(T valueToAdd) noexcept
176 { return Ops::fetchAndAndRelease(_q_value, valueToAdd); }
177 T fetchAndAndOrdered(T valueToAdd) noexcept
178 { return Ops::fetchAndAndOrdered(_q_value, valueToAdd); }
179
180 T fetchAndOrRelaxed(T valueToAdd) noexcept
181 { return Ops::fetchAndOrRelaxed(_q_value, valueToAdd); }
182 T fetchAndOrAcquire(T valueToAdd) noexcept
183 { return Ops::fetchAndOrAcquire(_q_value, valueToAdd); }
184 T fetchAndOrRelease(T valueToAdd) noexcept
185 { return Ops::fetchAndOrRelease(_q_value, valueToAdd); }
186 T fetchAndOrOrdered(T valueToAdd) noexcept
187 { return Ops::fetchAndOrOrdered(_q_value, valueToAdd); }
188
189 T fetchAndXorRelaxed(T valueToAdd) noexcept
190 { return Ops::fetchAndXorRelaxed(_q_value, valueToAdd); }
191 T fetchAndXorAcquire(T valueToAdd) noexcept
192 { return Ops::fetchAndXorAcquire(_q_value, valueToAdd); }
193 T fetchAndXorRelease(T valueToAdd) noexcept
194 { return Ops::fetchAndXorRelease(_q_value, valueToAdd); }
195 T fetchAndXorOrdered(T valueToAdd) noexcept
196 { return Ops::fetchAndXorOrdered(_q_value, valueToAdd); }
197
198 T operator++() noexcept
199 { return fetchAndAddOrdered(1) + 1; }
200 T operator++(int) noexcept
201 { return fetchAndAddOrdered(1); }
202 T operator--() noexcept
203 { return fetchAndSubOrdered(1) - 1; }
204 T operator--(int) noexcept
205 { return fetchAndSubOrdered(1); }
206
207 T operator+=(T v) noexcept
208 { return fetchAndAddOrdered(v) + v; }
209 T operator-=(T v) noexcept
210 { return fetchAndSubOrdered(v) - v; }
211 T operator&=(T v) noexcept
212 { return fetchAndAndOrdered(v) & v; }
213 T operator|=(T v) noexcept
214 { return fetchAndOrOrdered(v) | v; }
215 T operator^=(T v) noexcept
216 { return fetchAndXorOrdered(v) ^ v; }
217
218
219#ifdef QT_BASIC_ATOMIC_HAS_CONSTRUCTORS
220 QBasicAtomicInteger() = default;
221 constexpr QBasicAtomicInteger(T value) noexcept : _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 loadRelaxed() const noexcept { return Ops::loadRelaxed(_q_value); }
240 void storeRelaxed(Type newValue) noexcept { Ops::storeRelaxed(_q_value, newValue); }
241
242 operator Type() const noexcept { return loadAcquire(); }
243 Type operator=(Type newValue) noexcept { storeRelease(newValue); return newValue; }
244
245 // Atomic API, implemented in qatomic_XXX.h
246 Type loadAcquire() const noexcept { return Ops::loadAcquire(_q_value); }
247 void storeRelease(Type newValue) noexcept { Ops::storeRelease(_q_value, newValue); }
248
249 static constexpr bool isTestAndSetNative() noexcept { return Ops::isTestAndSetNative(); }
250 static constexpr bool isTestAndSetWaitFree() noexcept { return Ops::isTestAndSetWaitFree(); }
251
252 bool testAndSetRelaxed(Type expectedValue, Type newValue) noexcept
253 { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue); }
254 bool testAndSetAcquire(Type expectedValue, Type newValue) noexcept
255 { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue); }
256 bool testAndSetRelease(Type expectedValue, Type newValue) noexcept
257 { return Ops::testAndSetRelease(_q_value, expectedValue, newValue); }
258 bool testAndSetOrdered(Type expectedValue, Type newValue) noexcept
259 { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue); }
260
261 bool testAndSetRelaxed(Type expectedValue, Type newValue, Type &currentValue) noexcept
262 { return Ops::testAndSetRelaxed(_q_value, expectedValue, newValue, &currentValue); }
263 bool testAndSetAcquire(Type expectedValue, Type newValue, Type &currentValue) noexcept
264 { return Ops::testAndSetAcquire(_q_value, expectedValue, newValue, &currentValue); }
265 bool testAndSetRelease(Type expectedValue, Type newValue, Type &currentValue) noexcept
266 { return Ops::testAndSetRelease(_q_value, expectedValue, newValue, &currentValue); }
267 bool testAndSetOrdered(Type expectedValue, Type newValue, Type &currentValue) noexcept
268 { return Ops::testAndSetOrdered(_q_value, expectedValue, newValue, &currentValue); }
269
270 static constexpr bool isFetchAndStoreNative() noexcept { return Ops::isFetchAndStoreNative(); }
271 static constexpr bool isFetchAndStoreWaitFree() noexcept { return Ops::isFetchAndStoreWaitFree(); }
272
273 Type fetchAndStoreRelaxed(Type newValue) noexcept
274 { return Ops::fetchAndStoreRelaxed(_q_value, newValue); }
275 Type fetchAndStoreAcquire(Type newValue) noexcept
276 { return Ops::fetchAndStoreAcquire(_q_value, newValue); }
277 Type fetchAndStoreRelease(Type newValue) noexcept
278 { return Ops::fetchAndStoreRelease(_q_value, newValue); }
279 Type fetchAndStoreOrdered(Type newValue) noexcept
280 { return Ops::fetchAndStoreOrdered(_q_value, newValue); }
281
282 static constexpr bool isFetchAndAddNative() noexcept { return Ops::isFetchAndAddNative(); }
283 static constexpr bool isFetchAndAddWaitFree() noexcept { return Ops::isFetchAndAddWaitFree(); }
284
285 Type fetchAndAddRelaxed(qptrdiff valueToAdd) noexcept
286 { return Ops::fetchAndAddRelaxed(_q_value, valueToAdd); }
287 Type fetchAndAddAcquire(qptrdiff valueToAdd) noexcept
288 { return Ops::fetchAndAddAcquire(_q_value, valueToAdd); }
289 Type fetchAndAddRelease(qptrdiff valueToAdd) noexcept
290 { return Ops::fetchAndAddRelease(_q_value, valueToAdd); }
291 Type fetchAndAddOrdered(qptrdiff valueToAdd) noexcept
292 { return Ops::fetchAndAddOrdered(_q_value, valueToAdd); }
293
294 Type fetchAndSubRelaxed(qptrdiff valueToAdd) noexcept
295 { return Ops::fetchAndSubRelaxed(_q_value, valueToAdd); }
296 Type fetchAndSubAcquire(qptrdiff valueToAdd) noexcept
297 { return Ops::fetchAndSubAcquire(_q_value, valueToAdd); }
298 Type fetchAndSubRelease(qptrdiff valueToAdd) noexcept
299 { return Ops::fetchAndSubRelease(_q_value, valueToAdd); }
300 Type fetchAndSubOrdered(qptrdiff valueToAdd) noexcept
301 { return Ops::fetchAndSubOrdered(_q_value, valueToAdd); }
302
303 Type operator++() noexcept
304 { return fetchAndAddOrdered(1) + 1; }
305 Type operator++(int) noexcept
306 { return fetchAndAddOrdered(1); }
307 Type operator--() noexcept
308 { return fetchAndSubOrdered(1) - 1; }
309 Type operator--(int) noexcept
310 { return fetchAndSubOrdered(1); }
311 Type operator+=(qptrdiff valueToAdd) noexcept
312 { return fetchAndAddOrdered(valueToAdd) + valueToAdd; }
313 Type operator-=(qptrdiff valueToSub) noexcept
314 { return fetchAndSubOrdered(valueToSub) - valueToSub; }
315
316#ifdef QT_BASIC_ATOMIC_HAS_CONSTRUCTORS
317 QBasicAtomicPointer() = default;
318 constexpr QBasicAtomicPointer(Type value) noexcept : _q_value(value) {}
319 QBasicAtomicPointer(const QBasicAtomicPointer &) = delete;
320 QBasicAtomicPointer &operator=(const QBasicAtomicPointer &) = delete;
321 QBasicAtomicPointer &operator=(const QBasicAtomicPointer &) volatile = delete;
322#endif
323};
324
325#ifndef Q_BASIC_ATOMIC_INITIALIZER
326# define Q_BASIC_ATOMIC_INITIALIZER(a) { (a) }
327#endif
328
329QT_END_NAMESPACE
330
331QT_WARNING_POP
332
333#endif // QBASICATOMIC_H
334