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