Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qatomic.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#include "qatomic.h"
7
8/*!
9 \class QAtomicInt
10 \inmodule QtCore
11 \brief The QAtomicInt class provides platform-independent atomic operations on int.
12 \since 4.4
13
14 This class is a equivalent to \c{QAtomicInteger<int>}. All other
15 functionality is equivalent. Please see that class for more information.
16
17 \sa QAtomicInteger, QAtomicPointer
18*/
19
20/*!
21 \class QAtomicInteger
22 \inmodule QtCore
23 \brief The QAtomicInteger class provides platform-independent atomic operations on integers.
24 \ingroup thread
25 \since 5.3
26
27 For atomic operations on pointers, see the QAtomicPointer class.
28
29 An \e atomic operation is a complex operation that completes without interruption.
30 The QAtomicInteger class provides atomic reference counting, test-and-set, fetch-and-store,
31 and fetch-and-add for integers.
32
33 The template parameter \c T must be a C++ integer type:
34 \list
35 \li 8-bit: bool, char, signed char, unsigned char, qint8, quint8, char8_t (C++20)
36 \li 16-bit: short, unsigned short, qint16, quint16, char16_t
37 \li 32-bit: int, unsigned int, qint32, quint32, char32_t
38 \li 64-bit: long long, unsigned long long, qint64, quint64
39 \li platform-specific size: long, unsigned long
40 \li pointer size: qintptr, quintptr, qptrdiff
41 \endlist
42
43 Of the list above, only the 8-bit, 16-bit, 32-bit- and pointer-sized
44 instantiations are guaranteed to work on all platforms. Support for other
45 sizes depends on the compiler and processor architecture the code is being
46 compiled for. To test whether the 64-bit types are supported on 32-bit
47 platforms, check the macro \c Q_ATOMIC_INT64_IS_SUPPORTED.
48
49 \section1 The Atomic API
50
51 \section2 Reference counting
52
53 The ref() and deref() functions provide an efficient reference
54 counting API. The return value of these functions are used to
55 indicate when the last reference has been released. These
56 functions allow you to implement your own implicitly shared
57 classes.
58
59 \snippet code/src_corelib_thread_qatomic.cpp 0
60
61 \section2 Memory ordering
62
63 QAtomicInteger provides several implementations of the atomic
64 test-and-set, fetch-and-store, and fetch-and-add functions. Each
65 implementation defines a memory ordering semantic that describes
66 how memory accesses surrounding the atomic instruction are
67 executed by the processor. Since many modern architectures allow
68 out-of-order execution and memory ordering, using the correct
69 semantic is necessary to ensure that your application functions
70 properly on all processors.
71
72 \list
73
74 \li Relaxed - memory ordering is unspecified, leaving the compiler
75 and processor to freely reorder memory accesses.
76
77 \li Acquire - memory access following the atomic operation (in
78 program order) may not be re-ordered before the atomic operation.
79
80 \li Release - memory access before the atomic operation (in program
81 order) may not be re-ordered after the atomic operation.
82
83 \li Ordered - the same Acquire and Release semantics combined.
84
85 \endlist
86
87 \section2 Test-and-set
88
89 If the current value of the QAtomicInteger is an expected value, the
90 test-and-set functions assign a new value to the QAtomicInteger and
91 return true. If values are \a not the same, these functions do
92 nothing and return false. This operation equates to the following
93 code:
94
95 \snippet code/src_corelib_thread_qatomic.cpp 1
96
97 There are 4 test-and-set functions: testAndSetRelaxed(),
98 testAndSetAcquire(), testAndSetRelease(), and
99 testAndSetOrdered(). See above for an explanation of the different
100 memory ordering semantics.
101
102 \section2 Fetch-and-store
103
104 The atomic fetch-and-store functions read the current value of the
105 QAtomicInteger and then assign a new value, returning the original
106 value. This operation equates to the following code:
107
108 \snippet code/src_corelib_thread_qatomic.cpp 2
109
110 There are 4 fetch-and-store functions: fetchAndStoreRelaxed(),
111 fetchAndStoreAcquire(), fetchAndStoreRelease(), and
112 fetchAndStoreOrdered(). See above for an explanation of the
113 different memory ordering semantics.
114
115 \section2 Fetch-and-add
116
117 The atomic fetch-and-add functions read the current value of the
118 QAtomicInteger and then add the given value to the current value,
119 returning the original value. This operation equates to the
120 following code:
121
122 \snippet code/src_corelib_thread_qatomic.cpp 3
123
124 There are 4 fetch-and-add functions: fetchAndAddRelaxed(),
125 fetchAndAddAcquire(), fetchAndAddRelease(), and
126 fetchAndAddOrdered(). See above for an explanation of the
127 different memory ordering semantics.
128
129 \section1 Feature Tests for the Atomic API
130
131 Providing a platform-independent atomic API that works on all
132 processors is challenging. The API provided by QAtomicInteger is
133 guaranteed to work atomically on all processors. However, since
134 not all processors implement support for every operation provided
135 by QAtomicInteger, it is necessary to expose information about the
136 processor.
137
138 You can check at compile time which features are supported on your
139 hardware using various macros. These will tell you if your
140 hardware always, sometimes, or does not support a particular
141 operation. The macros have the form
142 Q_ATOMIC_INT\e{nn}_\e{OPERATION}_IS_\e{HOW}_NATIVE. \e{nn} is the
143 size of the integer (in bits), \e{OPERATION}
144 is one of REFERENCE_COUNTING, TEST_AND_SET,
145 FETCH_AND_STORE, or FETCH_AND_ADD, and \e{HOW} is one of
146 ALWAYS, SOMETIMES, or NOT. There will always be exactly one
147 defined macro per operation. For example, if
148 Q_ATOMIC_INT32_REFERENCE_COUNTING_IS_ALWAYS_NATIVE is defined,
149 neither Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE nor
150 Q_ATOMIC_INT32_REFERENCE_COUNTING_IS_NOT_NATIVE will be defined.
151
152 An operation that completes in constant time is said to be
153 wait-free. Such operations are not implemented using locks or
154 loops of any kind. For atomic operations that are always
155 supported, and that are wait-free, Qt defines the
156 Q_ATOMIC_INT\e{nn}_\e{OPERATION}_IS_WAIT_FREE in addition to the
157 Q_ATOMIC_INT\e{nn}_\e{OPERATION}_IS_ALWAYS_NATIVE.
158
159 In cases where an atomic operation is only supported in newer
160 generations of the processor, QAtomicInteger also provides a way to
161 check at runtime what your hardware supports with the
162 isReferenceCountingNative(), isTestAndSetNative(),
163 isFetchAndStoreNative(), and isFetchAndAddNative()
164 functions. Wait-free implementations can be detected using the
165 isReferenceCountingWaitFree(), isTestAndSetWaitFree(),
166 isFetchAndStoreWaitFree(), and isFetchAndAddWaitFree() functions.
167
168 Below is a complete list of all feature macros for QAtomicInteger:
169
170 \list
171
172 \li Q_ATOMIC_INT\e{nn}_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
173 \li Q_ATOMIC_INT\e{nn}_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE
174 \li Q_ATOMIC_INT\e{nn}_REFERENCE_COUNTING_IS_NOT_NATIVE
175 \li Q_ATOMIC_INT\e{nn}_REFERENCE_COUNTING_IS_WAIT_FREE
176
177 \li Q_ATOMIC_INT\e{nn}_TEST_AND_SET_IS_ALWAYS_NATIVE
178 \li Q_ATOMIC_INT\e{nn}_TEST_AND_SET_IS_SOMETIMES_NATIVE
179 \li Q_ATOMIC_INT\e{nn}_TEST_AND_SET_IS_NOT_NATIVE
180 \li Q_ATOMIC_INT\e{nn}_TEST_AND_SET_IS_WAIT_FREE
181
182 \li Q_ATOMIC_INT\e{nn}_FETCH_AND_STORE_IS_ALWAYS_NATIVE
183 \li Q_ATOMIC_INT\e{nn}_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
184 \li Q_ATOMIC_INT\e{nn}_FETCH_AND_STORE_IS_NOT_NATIVE
185 \li Q_ATOMIC_INT\e{nn}_FETCH_AND_STORE_IS_WAIT_FREE
186
187 \li Q_ATOMIC_INT\e{nn}_FETCH_AND_ADD_IS_ALWAYS_NATIVE
188 \li Q_ATOMIC_INT\e{nn}_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
189 \li Q_ATOMIC_INT\e{nn}_FETCH_AND_ADD_IS_NOT_NATIVE
190 \li Q_ATOMIC_INT\e{nn}_FETCH_AND_ADD_IS_WAIT_FREE
191
192 \endlist
193
194 For compatibility with previous versions of Qt, macros with an empty \e{nn}
195 are equivalent to the 32-bit macros. For example,
196 Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE is the same as
197 Q_ATOMIC_INT32_REFERENCE_COUNTING_IS_WAIT_FREE.
198
199 \sa QAtomicPointer
200*/
201
202/*!
203 \fn QAtomicInt::QAtomicInt(int value)
204
205 Constructs a QAtomicInt with the given \a value.
206*/
207
208/*!
209 \fn template <typename T> QAtomicInteger<T>::QAtomicInteger(T value)
210
211 Constructs a QAtomicInteger with the given \a value.
212*/
213
214/*!
215 \fn template <typename T> QAtomicInteger<T>::QAtomicInteger(const QAtomicInteger &other)
216
217 Constructs a copy of \a other.
218*/
219
220/*!
221 \fn template <typename T> QAtomicInteger &QAtomicInteger<T>::operator=(const QAtomicInteger &other)
222
223 Assigns \a other to this QAtomicInteger and returns a reference to
224 this QAtomicInteger.
225*/
226
227
228/*!
229 \fn template <typename T> T QAtomicInteger<T>::loadRelaxed() const
230 \since 5.14
231
232 Atomically loads the value of this QAtomicInteger using relaxed memory
233 ordering. The value is not modified in any way, but note that there's no
234 guarantee that it remains so.
235
236 \sa storeRelaxed(), loadAcquire()
237*/
238
239/*!
240 \fn template <typename T> T QAtomicInteger<T>::loadAcquire() const
241
242 Atomically loads the value of this QAtomicInteger using the "Acquire" memory
243 ordering. The value is not modified in any way, but note that there's no
244 guarantee that it remains so.
245
246 \sa storeRelaxed(), loadRelaxed()
247*/
248
249/*!
250 \fn template <typename T> void QAtomicInteger<T>::storeRelaxed(T newValue)
251 \since 5.14
252
253 Atomically stores the \a newValue value into this atomic type, using
254 relaxed memory ordering.
255
256 \sa storeRelease(), loadRelaxed()
257*/
258
259/*!
260 \fn template <typename T> void QAtomicInteger<T>::storeRelease(T newValue)
261
262 Atomically stores the \a newValue value into this atomic type, using
263 the "Release" memory ordering.
264
265 \sa storeRelaxed(), loadAcquire()
266*/
267
268/*!
269 \fn template <typename T> QAtomicInteger<T>::operator T() const
270 \since 5.3
271
272 Atomically loads the value of this QAtomicInteger using a sequentially
273 consistent memory ordering if possible; or "Acquire" ordering if not. The
274 value is not modified in any way, but note that there's no guarantee that
275 it remains so.
276
277 \sa loadRelaxed(), loadAcquire()
278*/
279
280/*!
281 \fn template <typename T> QAtomicInteger &QAtomicInteger<T>::operator=(T)
282 \since 5.3
283
284 Atomically stores the other value into this atomic type using a
285 sequentially consistent memory ordering if possible; or "Release" ordering
286 if not. This function returns a reference to this object.
287
288 \sa storeRelaxed(), storeRelease()
289*/
290
291/*!
292 \fn template <typename T> bool QAtomicInteger<T>::isReferenceCountingNative()
293
294 Returns \c true if reference counting is implemented using atomic
295 processor instructions, false otherwise.
296*/
297
298/*!
299 \fn template <typename T> bool QAtomicInteger<T>::isReferenceCountingWaitFree()
300
301 Returns \c true if atomic reference counting is wait-free, false
302 otherwise.
303*/
304
305/*!
306 \fn template <typename T> bool QAtomicInteger<T>::ref()
307 Atomically increments the value of this QAtomicInteger. Returns \c true
308 if the new value is non-zero, false otherwise.
309
310 This function uses \e ordered \l {QAtomicInteger#Memory
311 ordering}{memory ordering} semantics, which ensures that memory
312 access before and after the atomic operation (in program order)
313 may not be re-ordered.
314
315 \sa deref(), operator++()
316*/
317
318/*!
319 \fn template <typename T> T QAtomicInteger<T>::operator++()
320 \since 5.3
321
322 Atomically pre-increments the value of this QAtomicInteger. Returns the new
323 value of this atomic.
324
325 This function uses a sequentially consistent memory ordering if possible;
326 or "Ordered" ordering if not.
327
328 \sa ref(), operator++(int), operator--()
329*/
330
331/*!
332 \fn template <typename T> T QAtomicInteger<T>::operator++(int)
333 \since 5.3
334
335 Atomically post-increments the value of this QAtomicInteger. Returns the old
336 value of this atomic.
337
338 This function uses a sequentially consistent memory ordering if possible;
339 or "Ordered" ordering if not.
340
341 \sa ref(), operator++(), operator--(int)
342*/
343
344/*!
345 \fn template <typename T> bool QAtomicInteger<T>::deref()
346 Atomically decrements the value of this QAtomicInteger. Returns \c true
347 if the new value is non-zero, false otherwise.
348
349 This function uses \e ordered \l {QAtomicInteger#Memory
350 ordering}{memory ordering} semantics, which ensures that memory
351 access before and after the atomic operation (in program order)
352 may not be re-ordered.
353
354 \sa ref(), operator--()
355*/
356
357/*!
358 \fn template <typename T> T QAtomicInteger<T>::operator--()
359 \since 5.3
360
361 Atomically pre-decrements the value of this QAtomicInteger. Returns the new
362 value of this atomic.
363
364 This function uses a sequentially consistent memory ordering if possible;
365 or "Ordered" ordering if not.
366
367 \sa deref(), operator--(int), operator++()
368*/
369
370/*!
371 \fn template <typename T> T QAtomicInteger<T>::operator--(int)
372 \since 5.3
373
374 Atomically post-decrements the value of this QAtomicInteger. Returns the old
375 value of this atomic.
376
377 This function uses a sequentially consistent memory ordering if possible;
378 or "Ordered" ordering if not.
379
380 \sa deref(), operator--(), operator++(int)
381*/
382
383/*!
384 \fn template <typename T> bool QAtomicInteger<T>::isTestAndSetNative()
385
386 Returns \c true if test-and-set is implemented using atomic processor
387 instructions, false otherwise.
388*/
389
390/*!
391 \fn template <typename T> bool QAtomicInteger<T>::isTestAndSetWaitFree()
392
393 Returns \c true if atomic test-and-set is wait-free, false otherwise.
394*/
395
396/*!
397 \fn template <typename T> bool QAtomicInteger<T>::testAndSetRelaxed(T expectedValue, T newValue)
398
399 Atomic test-and-set.
400
401 \note If you use this function in a loop, consider using the overload with the
402 additional \c{T &currentValue} argument instead, which avoids the extra loadRelaxed() on
403 failure.
404
405 If the current value of this QAtomicInteger is the \a expectedValue,
406 the test-and-set functions assign the \a newValue to this
407 QAtomicInteger and return true. If the values are \e not the same,
408 this function does nothing and returns \c false.
409
410//![memory-order-relaxed]
411 This function uses \e relaxed \l {QAtomicInteger#Memory
412 ordering}{memory ordering} semantics, leaving the compiler and
413 processor to freely reorder memory accesses.
414//![memory-order-relaxed]
415
416*/
417
418/*!
419 \fn template <typename T> bool QAtomicInteger<T>::testAndSetAcquire(T expectedValue, T newValue)
420
421 Atomic test-and-set.
422
423 \note If you use this function in a loop, consider using the overload with the
424 additional \c{T &currentValue} argument instead, which avoids the extra loadAcquire() on
425 failure.
426
427 If the current value of this QAtomicInteger is the \a expectedValue,
428 the test-and-set functions assign the \a newValue to this
429 QAtomicInteger and return true. If the values are \e not the same,
430 this function does nothing and returns \c false.
431
432//![memory-order-acquire]
433 This function uses \e acquire \l {QAtomicInteger#Memory
434 ordering}{memory ordering} semantics, which ensures that memory
435 access following the atomic operation (in program order) may not
436 be re-ordered before the atomic operation.
437//![memory-order-acquire]
438*/
439
440/*!
441 \fn template <typename T> bool QAtomicInteger<T>::testAndSetRelease(T expectedValue, T newValue)
442
443 Atomic test-and-set.
444
445 \note If you use this function in a loop, consider using the overload with the
446 additional \c{T &currentValue} argument instead, which avoids the extra loadRelaxed() on
447 failure.
448
449 If the current value of this QAtomicInteger is the \a expectedValue,
450 the test-and-set functions assign the \a newValue to this
451 QAtomicInteger and return true. If the values are \e not the same,
452 this function does nothing and returns \c false.
453
454//![memory-order-release]
455 This function uses \e release \l {QAtomicInteger#Memory
456 ordering}{memory ordering} semantics, which ensures that memory
457 access before the atomic operation (in program order) may not be
458 re-ordered after the atomic operation.
459//![memory-order-release]
460*/
461
462/*!
463 \fn template <typename T> bool QAtomicInteger<T>::testAndSetOrdered(T expectedValue, T newValue)
464
465 Atomic test-and-set.
466
467 \note If you use this function in a loop, consider using the overload with the
468 additional \c{T &currentValue} argument instead, which avoids the extra loadAcquire() on
469 failure.
470
471 If the current value of this QAtomicInteger is the \a expectedValue,
472 the test-and-set functions assign the \a newValue to this
473 QAtomicInteger and return true. If the values are \e not the same,
474 this function does nothing and returns \c false.
475
476//![memory-order-ordered]
477 This function uses \e ordered \l {QAtomicInteger#Memory
478 ordering}{memory ordering} semantics, which ensures that memory
479 access before and after the atomic operation (in program order)
480 may not be re-ordered.
481//![memory-order-ordered]
482
483*/
484
485/*!
486 \fn template <typename T> bool QAtomicInteger<T>::testAndSetRelaxed(T expectedValue, T newValue, T &currentValue)
487 \since 5.3
488
489 Atomic test-and-set.
490
491 If the current value of this QAtomicInteger is the \a expectedValue, the
492 test-and-set functions assign the \a newValue to this QAtomicInteger and
493 return \c true. If the values are \e not the same, the functions load the
494 current value of this QAtomicInteger into \a currentValue and return \c false.
495
496 \include qatomic.cpp memory-order-relaxed
497*/
498
499/*!
500 \fn template <typename T> bool QAtomicInteger<T>::testAndSetAcquire(T expectedValue, T newValue, T &currentValue)
501 \since 5.3
502
503 Atomic test-and-set.
504
505 If the current value of this QAtomicInteger is the \a expectedValue, the
506 test-and-set functions assign the \a newValue to this QAtomicInteger and
507 return \c true. If the values are \e not the same, the functions load the
508 current value of this QAtomicInteger into \a currentValue and return \c false.
509
510 \include qatomic.cpp memory-order-acquire
511*/
512
513/*!
514 \fn template <typename T> bool QAtomicInteger<T>::testAndSetRelease(T expectedValue, T newValue, T &currentValue)
515 \since 5.3
516
517 Atomic test-and-set.
518
519 If the current value of this QAtomicInteger is the \a expectedValue, the
520 test-and-set functions assign the \a newValue to this QAtomicInteger and
521 return \c true. If the values are \e not the same, the functions loads the
522 current value of this QAtomicInteger into \a currentValue and return \c false.
523
524 \include qatomic.cpp memory-order-release
525*/
526
527/*!
528 \fn template <typename T> bool QAtomicInteger<T>::testAndSetOrdered(T expectedValue, T newValue, T &currentValue)
529 \since 5.3
530
531 Atomic test-and-set.
532
533 If the current value of this QAtomicInteger is the \a expectedValue, the
534 test-and-set functions assign the \a newValue to this QAtomicInteger and
535 return \c true. If the values are \e not the same, it loads the current
536 value of this QAtomicInteger into \a currentValue and return \c false.
537
538 \include qatomic.cpp memory-order-ordered
539*/
540
541/*!
542 \fn template <typename T> bool QAtomicInteger<T>::isFetchAndStoreNative()
543
544 Returns \c true if fetch-and-store is implemented using atomic
545 processor instructions, false otherwise.
546*/
547
548/*!
549 \fn template <typename T> bool QAtomicInteger<T>::isFetchAndStoreWaitFree()
550
551 Returns \c true if atomic fetch-and-store is wait-free, false
552 otherwise.
553*/
554
555/*!
556 \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreRelaxed(T newValue)
557
558 Atomic fetch-and-store.
559
560 Reads the current value of this QAtomicInteger and then assigns it the
561 \a newValue, returning the original value.
562
563 This function uses \e relaxed \l {QAtomicInteger#Memory
564 ordering}{memory ordering} semantics, leaving the compiler and
565 processor to freely reorder memory accesses.
566*/
567
568/*!
569 \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreAcquire(T newValue)
570
571 Atomic fetch-and-store.
572
573 Reads the current value of this QAtomicInteger and then assigns it the
574 \a newValue, returning the original value.
575
576 This function uses \e acquire \l {QAtomicInteger#Memory
577 ordering}{memory ordering} semantics, which ensures that memory
578 access following the atomic operation (in program order) may not
579 be re-ordered before the atomic operation.
580*/
581
582/*!
583 \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreRelease(T newValue)
584
585 Atomic fetch-and-store.
586
587 Reads the current value of this QAtomicInteger and then assigns it the
588 \a newValue, returning the original value.
589
590 This function uses \e release \l {QAtomicInteger#Memory
591 ordering}{memory ordering} semantics, which ensures that memory
592 access before the atomic operation (in program order) may not be
593 re-ordered after the atomic operation.
594*/
595
596/*!
597 \fn template <typename T> T QAtomicInteger<T>::fetchAndStoreOrdered(T newValue)
598
599 Atomic fetch-and-store.
600
601 Reads the current value of this QAtomicInteger and then assigns it the
602 \a newValue, returning the original value.
603
604 This function uses \e ordered \l {QAtomicInteger#Memory
605 ordering}{memory ordering} semantics, which ensures that memory
606 access before and after the atomic operation (in program order)
607 may not be re-ordered.
608*/
609
610/*!
611 \fn template <typename T> bool QAtomicInteger<T>::isFetchAndAddNative()
612
613 Returns \c true if fetch-and-add is implemented using atomic
614 processor instructions, false otherwise.
615*/
616
617/*!
618 \fn template <typename T> bool QAtomicInteger<T>::isFetchAndAddWaitFree()
619
620 Returns \c true if atomic fetch-and-add is wait-free, false
621 otherwise.
622*/
623
624/*!
625 \fn template <typename T> T QAtomicInteger<T>::fetchAndAddRelaxed(T valueToAdd)
626
627 Atomic fetch-and-add.
628
629 Reads the current value of this QAtomicInteger and then adds
630 \a valueToAdd to the current value, returning the original value.
631
632 This function uses \e relaxed \l {QAtomicInteger#Memory
633 ordering}{memory ordering} semantics, leaving the compiler and
634 processor to freely reorder memory accesses.
635
636 \sa operator+=(), fetchAndSubRelaxed()
637*/
638
639/*!
640 \fn template <typename T> T QAtomicInteger<T>::fetchAndAddAcquire(T valueToAdd)
641
642 Atomic fetch-and-add.
643
644 Reads the current value of this QAtomicInteger and then adds
645 \a valueToAdd to the current value, returning the original value.
646
647 This function uses \e acquire \l {QAtomicInteger#Memory
648 ordering}{memory ordering} semantics, which ensures that memory
649 access following the atomic operation (in program order) may not
650 be re-ordered before the atomic operation.
651
652 \sa operator+=(), fetchAndSubAcquire()
653*/
654
655/*!
656 \fn template <typename T> T QAtomicInteger<T>::fetchAndAddRelease(T valueToAdd)
657
658 Atomic fetch-and-add.
659
660 Reads the current value of this QAtomicInteger and then adds
661 \a valueToAdd to the current value, returning the original value.
662
663 This function uses \e release \l {QAtomicInteger#Memory
664 ordering}{memory ordering} semantics, which ensures that memory
665 access before the atomic operation (in program order) may not be
666 re-ordered after the atomic operation.
667
668 \sa operator+=(), fetchAndSubRelease()
669*/
670
671/*!
672 \fn template <typename T> T QAtomicInteger<T>::fetchAndAddOrdered(T valueToAdd)
673
674 Atomic fetch-and-add.
675
676 Reads the current value of this QAtomicInteger and then adds
677 \a valueToAdd to the current value, returning the original value.
678
679 This function uses \e ordered \l {QAtomicInteger#Memory
680 ordering}{memory ordering} semantics, which ensures that memory
681 access before and after the atomic operation (in program order)
682 may not be re-ordered.
683
684 \sa operator+=(), fetchAndSubOrdered()
685*/
686
687/*!
688 \fn template <typename T> T QAtomicInteger<T>::operator+=(T value)
689 \since 5.3
690
691 Atomic add-and-fetch.
692
693 Reads the current value of this QAtomicInteger and then adds
694 \a value to the current value, returning the new value.
695
696 This function uses a sequentially consistent memory ordering if possible;
697 or "Ordered" ordering if not.
698
699 \sa fetchAndAddOrdered(), operator-=()
700*/
701
702/*!
703 \fn template <typename T> T QAtomicInteger<T>::fetchAndSubRelaxed(T valueToSub)
704 \since 5.3
705
706 Atomic fetch-and-sub.
707
708 Reads the current value of this QAtomicInteger and then subtracts
709 \a valueToSub to the current value, returning the original value.
710
711 This function uses \e relaxed \l {QAtomicInteger#Memory
712 ordering}{memory ordering} semantics, leaving the compiler and
713 processor to freely reorder memory accesses.
714
715 \sa operator-=(), fetchAndAddRelaxed()
716*/
717
718/*!
719 \fn template <typename T> T QAtomicInteger<T>::fetchAndSubAcquire(T valueToSub)
720 \since 5.3
721
722 Atomic fetch-and-sub.
723
724 Reads the current value of this QAtomicInteger and then subtracts
725 \a valueToSub to the current value, returning the original value.
726
727 This function uses \e acquire \l {QAtomicInteger#Memory
728 ordering}{memory ordering} semantics, which ensures that memory
729 access following the atomic operation (in program order) may not
730 be re-ordered before the atomic operation.
731
732 \sa operator-=(), fetchAndAddAcquire()
733*/
734
735/*!
736 \fn template <typename T> T QAtomicInteger<T>::fetchAndSubRelease(T valueToSub)
737 \since 5.3
738
739 Atomic fetch-and-sub.
740
741 Reads the current value of this QAtomicInteger and then subtracts
742 \a valueToSub to the current value, returning the original value.
743
744 This function uses \e release \l {QAtomicInteger#Memory
745 ordering}{memory ordering} semantics, which ensures that memory
746 access before the atomic operation (in program order) may not be
747 re-ordered after the atomic operation.
748
749 \sa operator-=(), fetchAndAddRelease()
750*/
751
752/*!
753 \fn template <typename T> T QAtomicInteger<T>::fetchAndSubOrdered(T valueToSub)
754 \since 5.3
755
756 Atomic fetch-and-sub.
757
758 Reads the current value of this QAtomicInteger and then subtracts
759 \a valueToSub to the current value, returning the original value.
760
761 This function uses \e ordered \l {QAtomicInteger#Memory
762 ordering}{memory ordering} semantics, which ensures that memory
763 access before and after the atomic operation (in program order)
764 may not be re-ordered.
765
766 \sa operator-=(), fetchAndAddOrdered()
767*/
768
769/*!
770 \fn template <typename T> T QAtomicInteger<T>::operator-=(T value)
771 \since 5.3
772
773 Atomic sub-and-fetch.
774
775 Reads the current value of this QAtomicInteger and then subtracts
776 \a value to the current value, returning the new value.
777
778 This function uses a sequentially consistent memory ordering if possible;
779 or "Ordered" ordering if not.
780
781 \sa fetchAndSubOrdered(), operator+=()
782*/
783
784/*!
785 \fn template <typename T> T QAtomicInteger<T>::fetchAndOrRelaxed(T valueToOr)
786 \since 5.3
787
788 Atomic fetch-and-or.
789
790 Reads the current value of this QAtomicInteger and then bitwise-ORs
791 \a valueToOr to the current value, returning the original value.
792
793 This function uses \e relaxed \l {QAtomicInteger#Memory
794 ordering}{memory ordering} semantics, leaving the compiler and
795 processor to freely reorder memory accesses.
796
797 \sa operator|=()
798*/
799
800/*!
801 \fn template <typename T> T QAtomicInteger<T>::fetchAndOrAcquire(T valueToOr)
802 \since 5.3
803
804 Atomic fetch-and-or.
805
806 Reads the current value of this QAtomicInteger and then bitwise-ORs
807 \a valueToOr to the current value, returning the original value.
808
809 This function uses \e acquire \l {QAtomicInteger#Memory
810 ordering}{memory ordering} semantics, which ensures that memory
811 access following the atomic operation (in program order) may not
812 be re-ordered before the atomic operation.
813
814 \sa operator|=()
815*/
816
817/*!
818 \fn template <typename T> T QAtomicInteger<T>::fetchAndOrRelease(T valueToOr)
819 \since 5.3
820
821 Atomic fetch-and-or.
822
823 Reads the current value of this QAtomicInteger and then bitwise-ORs
824 \a valueToOr to the current value, returning the original value.
825
826 This function uses \e release \l {QAtomicInteger#Memory
827 ordering}{memory ordering} semantics, which ensures that memory
828 access before the atomic operation (in program order) may not be
829 re-ordered after the atomic operation.
830
831 \sa operator|=()
832*/
833
834/*!
835 \fn template <typename T> T QAtomicInteger<T>::fetchAndOrOrdered(T valueToOr)
836 \since 5.3
837
838 Atomic fetch-and-or.
839
840 Reads the current value of this QAtomicInteger and then bitwise-ORs
841 \a valueToOr to the current value, returning the original value.
842
843 This function uses \e ordered \l {QAtomicInteger#Memory
844 ordering}{memory ordering} semantics, which ensures that memory
845 access before and after the atomic operation (in program order)
846 may not be re-ordered.
847
848 \sa operator|=()
849*/
850
851/*!
852 \fn template <typename T> T QAtomicInteger<T>::operator|=(T value)
853 \since 5.3
854
855 Atomic or-and-fetch.
856
857 Reads the current value of this QAtomicInteger and then bitwise-ORs
858 \a value to the current value, returning the new value.
859
860 This function uses a sequentially consistent memory ordering if possible;
861 or "Ordered" ordering if not.
862
863 \sa fetchAndOrOrdered()
864*/
865
866/*!
867 \fn template <typename T> T QAtomicInteger<T>::fetchAndXorRelaxed(T valueToXor)
868 \since 5.3
869
870 Atomic fetch-and-xor.
871
872 Reads the current value of this QAtomicInteger and then bitwise-XORs
873 \a valueToXor to the current value, returning the original value.
874
875 This function uses \e relaxed \l {QAtomicInteger#Memory
876 ordering}{memory ordering} semantics, leaving the compiler and
877 processor to freely reorder memory accesses.
878
879 \sa operator^=()
880*/
881
882/*!
883 \fn template <typename T> T QAtomicInteger<T>::fetchAndXorAcquire(T valueToXor)
884 \since 5.3
885
886 Atomic fetch-and-xor.
887
888 Reads the current value of this QAtomicInteger and then bitwise-XORs
889 \a valueToXor to the current value, returning the original value.
890
891 This function uses \e acquire \l {QAtomicInteger#Memory
892 ordering}{memory ordering} semantics, which ensures that memory
893 access following the atomic operation (in program order) may not
894 be re-ordered before the atomic operation.
895
896 \sa operator^=()
897*/
898
899/*!
900 \fn template <typename T> T QAtomicInteger<T>::fetchAndXorRelease(T valueToXor)
901 \since 5.3
902
903 Atomic fetch-and-xor.
904
905 Reads the current value of this QAtomicInteger and then bitwise-XORs
906 \a valueToXor to the current value, returning the original value.
907
908 This function uses \e release \l {QAtomicInteger#Memory
909 ordering}{memory ordering} semantics, which ensures that memory
910 access before the atomic operation (in program order) may not be
911 re-ordered after the atomic operation.
912
913 \sa operator^=()
914*/
915
916/*!
917 \fn template <typename T> T QAtomicInteger<T>::fetchAndXorOrdered(T valueToXor)
918 \since 5.3
919
920 Atomic fetch-and-xor.
921
922 Reads the current value of this QAtomicInteger and then bitwise-XORs
923 \a valueToXor to the current value, returning the original value.
924
925 This function uses \e ordered \l {QAtomicInteger#Memory
926 ordering}{memory ordering} semantics, which ensures that memory
927 access before and after the atomic operation (in program order)
928 may not be re-ordered.
929
930 \sa operator^=()
931*/
932
933/*!
934 \fn template <typename T> T QAtomicInteger<T>::operator^=(T value)
935 \since 5.3
936
937 Atomic xor-and-fetch.
938
939 Reads the current value of this QAtomicInteger and then bitwise-XORs
940 \a value to the current value, returning the new value.
941
942 This function uses a sequentially consistent memory ordering if possible;
943 or "Ordered" ordering if not.
944
945 \sa fetchAndXorOrdered()
946*/
947
948/*!
949 \fn template <typename T> T QAtomicInteger<T>::fetchAndAndRelaxed(T valueToAnd)
950 \since 5.3
951
952 Atomic fetch-and-and.
953
954 Reads the current value of this QAtomicInteger and then bitwise-ANDs
955 \a valueToAnd to the current value, returning the original value.
956
957 This function uses \e relaxed \l {QAtomicInteger#Memory
958 ordering}{memory ordering} semantics, leaving the compiler and
959 processor to freely reorder memory accesses.
960
961 \sa operator&=()
962*/
963
964/*!
965 \fn template <typename T> T QAtomicInteger<T>::fetchAndAndAcquire(T valueToAnd)
966 \since 5.3
967
968 Atomic fetch-and-and.
969
970 Reads the current value of this QAtomicInteger and then bitwise-ANDs
971 \a valueToAnd to the current value, returning the original value.
972
973 This function uses \e acquire \l {QAtomicInteger#Memory
974 ordering}{memory ordering} semantics, which ensures that memory
975 access following the atomic operation (in program order) may not
976 be re-ordered before the atomic operation.
977
978 \sa operator&=()
979*/
980
981/*!
982 \fn template <typename T> T QAtomicInteger<T>::fetchAndAndRelease(T valueToAnd)
983 \since 5.3
984
985 Atomic fetch-and-and.
986
987 Reads the current value of this QAtomicInteger and then bitwise-ANDs
988 \a valueToAnd to the current value, returning the original value.
989
990 This function uses \e release \l {QAtomicInteger#Memory
991 ordering}{memory ordering} semantics, which ensures that memory
992 access before the atomic operation (in program order) may not be
993 re-ordered after the atomic operation.
994
995 \sa operator&=()
996*/
997
998/*!
999 \fn template <typename T> T QAtomicInteger<T>::fetchAndAndOrdered(T valueToAnd)
1000 \since 5.3
1001
1002 Atomic fetch-and-and.
1003
1004 Reads the current value of this QAtomicInteger and then bitwise-ANDs
1005 \a valueToAnd to the current value, returning the original value.
1006
1007 This function uses \e ordered \l {QAtomicInteger#Memory
1008 ordering}{memory ordering} semantics, which ensures that memory
1009 access before and after the atomic operation (in program order)
1010 may not be re-ordered.
1011
1012 \sa operator&=()
1013*/
1014
1015/*!
1016 \fn template <typename T> T QAtomicInteger<T>::operator&=(T value)
1017 \since 5.3
1018
1019 Atomic add-and-fetch.
1020
1021 Reads the current value of this QAtomicInteger and then bitwise-ANDs
1022 \a value to the current value, returning the new value.
1023
1024 This function uses a sequentially consistent memory ordering if possible;
1025 or "Ordered" ordering if not.
1026
1027 \sa fetchAndAndOrdered()
1028*/
1029
1030/*!
1031 \macro Q_ATOMIC_INTnn_IS_SUPPORTED
1032 \relates QAtomicInteger
1033
1034 This macro is defined if atomic integers of size \e{nn} (in bits) are
1035 supported in this compiler / architecture combination.
1036
1037 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1038
1039 The following macros always defined:
1040
1041 \list
1042 \li Q_ATOMIC_INT8_IS_SUPPORTED
1043 \li Q_ATOMIC_INT16_IS_SUPPORTED
1044 \li Q_ATOMIC_INT32_IS_SUPPORTED
1045 \endlist
1046*/
1047
1048/*!
1049 \macro Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
1050 \relates QAtomicInteger
1051
1052 This macro is defined if and only if all generations of your
1053 processor support atomic reference counting.
1054
1055 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1056*/
1057
1058/*!
1059 \macro Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE
1060 \relates QAtomicInteger
1061
1062 This macro is defined when only certain generations of the
1063 processor support atomic reference counting. Use the
1064 QAtomicInteger::isReferenceCountingNative() function to check what
1065 your processor supports.
1066
1067 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1068*/
1069
1070/*!
1071 \macro Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_NOT_NATIVE
1072 \relates QAtomicInteger
1073
1074 This macro is defined when the hardware does not support atomic
1075 reference counting.
1076
1077 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1078*/
1079
1080/*!
1081 \macro Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_WAIT_FREE
1082 \relates QAtomicInteger
1083
1084 This macro is defined together with
1085 Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_ALWAYS_NATIVE to indicate that
1086 the reference counting is wait-free.
1087
1088 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1089*/
1090
1091/*!
1092 \macro Q_ATOMIC_INTnn_TEST_AND_SET_IS_ALWAYS_NATIVE
1093 \relates QAtomicInteger
1094
1095 This macro is defined if and only if your processor supports
1096 atomic test-and-set on integers.
1097
1098 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1099*/
1100
1101/*!
1102 \macro Q_ATOMIC_INTnn_TEST_AND_SET_IS_SOMETIMES_NATIVE
1103 \relates QAtomicInteger
1104
1105 This macro is defined when only certain generations of the
1106 processor support atomic test-and-set on integers. Use the
1107 QAtomicInteger::isTestAndSetNative() function to check what your
1108 processor supports.
1109
1110 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1111*/
1112
1113/*!
1114 \macro Q_ATOMIC_INTnn_TEST_AND_SET_IS_NOT_NATIVE
1115 \relates QAtomicInteger
1116
1117 This macro is defined when the hardware does not support atomic
1118 test-and-set on integers.
1119
1120 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1121*/
1122
1123/*!
1124 \macro Q_ATOMIC_INTnn_TEST_AND_SET_IS_WAIT_FREE
1125 \relates QAtomicInteger
1126
1127 This macro is defined together with
1128 Q_ATOMIC_INTnn_TEST_AND_SET_IS_ALWAYS_NATIVE to indicate that the
1129 atomic test-and-set on integers is wait-free.
1130
1131 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1132*/
1133
1134/*!
1135 \macro Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_ALWAYS_NATIVE
1136 \relates QAtomicInteger
1137
1138 This macro is defined if and only if your processor supports
1139 atomic fetch-and-store on integers.
1140
1141 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1142*/
1143
1144/*!
1145 \macro Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
1146 \relates QAtomicInteger
1147
1148 This macro is defined when only certain generations of the
1149 processor support atomic fetch-and-store on integers. Use the
1150 QAtomicInteger::isFetchAndStoreNative() function to check what your
1151 processor supports.
1152
1153 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1154*/
1155
1156/*!
1157 \macro Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_NOT_NATIVE
1158 \relates QAtomicInteger
1159
1160 This macro is defined when the hardware does not support atomic
1161 fetch-and-store on integers.
1162
1163 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1164*/
1165
1166/*!
1167 \macro Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_WAIT_FREE
1168 \relates QAtomicInteger
1169
1170 This macro is defined together with
1171 Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_ALWAYS_NATIVE to indicate that the
1172 atomic fetch-and-store on integers is wait-free.
1173
1174 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1175*/
1176
1177/*!
1178 \macro Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_ALWAYS_NATIVE
1179 \relates QAtomicInteger
1180
1181 This macro is defined if and only if your processor supports
1182 atomic fetch-and-add on integers.
1183
1184 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1185*/
1186
1187/*!
1188 \macro Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
1189 \relates QAtomicInteger
1190
1191 This macro is defined when only certain generations of the
1192 processor support atomic fetch-and-add on integers. Use the
1193 QAtomicInteger::isFetchAndAddNative() function to check what your
1194 processor supports.
1195
1196 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1197*/
1198
1199/*!
1200 \macro Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_NOT_NATIVE
1201 \relates QAtomicInteger
1202
1203 This macro is defined when the hardware does not support atomic
1204 fetch-and-add on integers.
1205
1206 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1207*/
1208
1209/*!
1210 \macro Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_WAIT_FREE
1211 \relates QAtomicInteger
1212
1213 This macro is defined together with
1214 Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_ALWAYS_NATIVE to indicate that the
1215 atomic fetch-and-add on integers is wait-free.
1216
1217 \e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
1218*/
1219
1220
1221
1222
1223/*!
1224 \class QAtomicPointer
1225 \inmodule QtCore
1226 \brief The QAtomicPointer class is a template class that provides platform-independent atomic operations on pointers.
1227 \since 4.4
1228
1229 \ingroup thread
1230
1231 For atomic operations on integers, see the QAtomicInteger class.
1232
1233 An \e atomic operation is a complex operation that completes without interruption.
1234 The QAtomicPointer class provides atomic test-and-set, fetch-and-store, and fetch-and-add for pointers.
1235
1236 \section1 The Atomic API
1237
1238 \section2 Memory ordering
1239
1240 QAtomicPointer provides several implementations of the atomic
1241 test-and-set, fetch-and-store, and fetch-and-add functions. Each
1242 implementation defines a memory ordering semantic that describes
1243 how memory accesses surrounding the atomic instruction are
1244 executed by the processor. Since many modern architectures allow
1245 out-of-order execution and memory ordering, using the correct
1246 semantic is necessary to ensure that your application functions
1247 properly on all processors.
1248
1249 \list
1250
1251 \li Relaxed - memory ordering is unspecified, leaving the compiler
1252 and processor to freely reorder memory accesses.
1253
1254 \li Acquire - memory access following the atomic operation (in
1255 program order) may not be re-ordered before the atomic operation.
1256
1257 \li Release - memory access before the atomic operation (in program
1258 order) may not be re-ordered after the atomic operation.
1259
1260 \li Ordered - the same Acquire and Release semantics combined.
1261
1262 \endlist
1263
1264 \section2 Test-and-set
1265
1266 If the current value of the QAtomicPointer is an expected value,
1267 the test-and-set functions assign a new value to the
1268 QAtomicPointer and return true. If values are \a not the same,
1269 these functions do nothing and return false. This operation
1270 equates to the following code:
1271
1272 \snippet code/src_corelib_thread_qatomic.cpp 4
1273
1274 There are 4 test-and-set functions: testAndSetRelaxed(),
1275 testAndSetAcquire(), testAndSetRelease(), and
1276 testAndSetOrdered(). See above for an explanation of the different
1277 memory ordering semantics.
1278
1279 \section2 Fetch-and-store
1280
1281 The atomic fetch-and-store functions read the current value of the
1282 QAtomicPointer and then assign a new value, returning the original
1283 value. This operation equates to the following code:
1284
1285 \snippet code/src_corelib_thread_qatomic.cpp 5
1286
1287 There are 4 fetch-and-store functions: fetchAndStoreRelaxed(),
1288 fetchAndStoreAcquire(), fetchAndStoreRelease(), and
1289 fetchAndStoreOrdered(). See above for an explanation of the
1290 different memory ordering semantics.
1291
1292 \section2 Fetch-and-add
1293
1294 The atomic fetch-and-add functions read the current value of the
1295 QAtomicPointer and then add the given value to the current value,
1296 returning the original value. This operation equates to the
1297 following code:
1298
1299 \snippet code/src_corelib_thread_qatomic.cpp 6
1300
1301 There are 4 fetch-and-add functions: fetchAndAddRelaxed(),
1302 fetchAndAddAcquire(), fetchAndAddRelease(), and
1303 fetchAndAddOrdered(). See above for an explanation of the
1304 different memory ordering semantics.
1305
1306 \section1 Feature Tests for the Atomic API
1307
1308 Providing a platform-independent atomic API that works on all
1309 processors is challenging. The API provided by QAtomicPointer is
1310 guaranteed to work atomically on all processors. However, since
1311 not all processors implement support for every operation provided
1312 by QAtomicPointer, it is necessary to expose information about the
1313 processor.
1314
1315 You can check at compile time which features are supported on your
1316 hardware using various macros. These will tell you if your
1317 hardware always, sometimes, or does not support a particular
1318 operation. The macros have the form
1319 Q_ATOMIC_POINTER_\e{OPERATION}_IS_\e{HOW}_NATIVE. \e{OPERATION} is
1320 one of TEST_AND_SET, FETCH_AND_STORE, or FETCH_AND_ADD, and
1321 \e{HOW} is one of ALWAYS, SOMETIMES, or NOT. There will always be
1322 exactly one defined macro per operation. For example, if
1323 Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE is defined, neither
1324 Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE nor
1325 Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE will be defined.
1326
1327 An operation that completes in constant time is said to be
1328 wait-free. Such operations are not implemented using locks or
1329 loops of any kind. For atomic operations that are always
1330 supported, and that are wait-free, Qt defines the
1331 Q_ATOMIC_POINTER_\e{OPERATION}_IS_WAIT_FREE in addition to the
1332 Q_ATOMIC_POINTER_\e{OPERATION}_IS_ALWAYS_NATIVE.
1333
1334 In cases where an atomic operation is only supported in newer
1335 generations of the processor, QAtomicPointer also provides a way
1336 to check at runtime what your hardware supports with the
1337 isTestAndSetNative(), isFetchAndStoreNative(), and
1338 isFetchAndAddNative() functions. Wait-free implementations can be
1339 detected using the isTestAndSetWaitFree(),
1340 isFetchAndStoreWaitFree(), and isFetchAndAddWaitFree() functions.
1341
1342 Below is a complete list of all feature macros for QAtomicPointer:
1343
1344 \list
1345
1346 \li Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
1347 \li Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE
1348 \li Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE
1349 \li Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
1350
1351 \li Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
1352 \li Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
1353 \li Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_NOT_NATIVE
1354 \li Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
1355
1356 \li Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
1357 \li Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
1358 \li Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_NOT_NATIVE
1359 \li Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
1360
1361 \endlist
1362
1363 \sa QAtomicInteger, qYieldCpu()
1364*/
1365
1366/*!
1367 \fn template <typename T> QAtomicPointer<T>::QAtomicPointer(T *value)
1368
1369 Constructs a QAtomicPointer with the given \a value.
1370*/
1371
1372/*!
1373 \fn template <typename T> QAtomicPointer<T>::QAtomicPointer(const QAtomicPointer<T> &other)
1374
1375 Constructs a copy of \a other.
1376*/
1377
1378/*!
1379 \fn template <typename T> QAtomicPointer &QAtomicPointer<T>::operator=(const QAtomicPointer &other)
1380
1381 Assigns \a other to this QAtomicPointer and returns a reference to
1382 this QAtomicPointer.
1383*/
1384
1385/*!
1386 \fn template <typename T> T *QAtomicPointer<T>::loadRelaxed() const
1387 \since 5.14
1388
1389 Atomically loads the value of this QAtomicPointer using relaxed memory
1390 ordering. The value is not modified in any way, but note that there's no
1391 guarantee that it remains so.
1392
1393 \sa storeRelaxed(), loadAcquire()
1394*/
1395
1396
1397/*!
1398 \fn template <typename T> T *QAtomicPointer<T>::loadAcquire() const
1399
1400 Atomically loads the value of this QAtomicPointer using the "Acquire" memory
1401 ordering. The value is not modified in any way, but note that there's no
1402 guarantee that it remains so.
1403
1404 \sa storeRelease(), loadRelaxed()
1405*/
1406
1407/*!
1408 \fn template <typename T> void QAtomicPointer<T>::storeRelaxed(T *newValue)
1409 \since 5.14
1410
1411 Atomically stores the \a newValue value into this atomic type, using
1412 relaxed memory ordering.
1413
1414 \sa storeRelease(), loadRelaxed()
1415*/
1416
1417/*!
1418 \fn template <typename T> void QAtomicPointer<T>::storeRelease(T *newValue)
1419
1420 Atomically stores the \a newValue value into this atomic type, using
1421 the "Release" memory ordering.
1422
1423 \sa storeRelaxed(), loadRelaxed()
1424*/
1425
1426/*!
1427 \fn template <typename T> bool QAtomicPointer<T>::isTestAndSetNative()
1428
1429 Returns \c true if test-and-set is implemented using atomic processor
1430 instructions, false otherwise.
1431*/
1432
1433/*!
1434 \fn template <typename T> bool QAtomicPointer<T>::isTestAndSetWaitFree()
1435
1436 Returns \c true if atomic test-and-set is wait-free, false otherwise.
1437*/
1438
1439/*!
1440 \fn template <typename T> bool QAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
1441
1442 Atomic test-and-set.
1443
1444 If the current value of this QAtomicPointer is the \a expectedValue,
1445 the test-and-set functions assign the \a newValue to this
1446 QAtomicPointer and return true. If the values are \e not the same,
1447 this function does nothing and returns \c false.
1448
1449 This function uses \e relaxed \l {QAtomicPointer#Memory
1450 ordering}{memory ordering} semantics, leaving the compiler and
1451 processor to freely reorder memory accesses.
1452*/
1453
1454/*!
1455 \fn template <typename T> bool QAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
1456
1457 Atomic test-and-set.
1458
1459 If the current value of this QAtomicPointer is the \a expectedValue,
1460 the test-and-set functions assign the \a newValue to this
1461 QAtomicPointer and return true. If the values are \e not the same,
1462 this function does nothing and returns \c false.
1463
1464 This function uses \e acquire \l {QAtomicPointer#Memory
1465 ordering}{memory ordering} semantics, which ensures that memory
1466 access following the atomic operation (in program order) may not
1467 be re-ordered before the atomic operation.
1468*/
1469
1470/*!
1471 \fn template <typename T> bool QAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
1472
1473 Atomic test-and-set.
1474
1475 If the current value of this QAtomicPointer is the \a expectedValue,
1476 the test-and-set functions assign the \a newValue to this
1477 QAtomicPointer and return true. If the values are \e not the same,
1478 this function does nothing and returns \c false.
1479
1480 This function uses \e release \l {QAtomicPointer#Memory
1481 ordering}{memory ordering} semantics, which ensures that memory
1482 access before the atomic operation (in program order) may not be
1483 re-ordered after the atomic operation.
1484*/
1485
1486/*!
1487 \fn template <typename T> bool QAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
1488
1489 Atomic test-and-set.
1490
1491 If the current value of this QAtomicPointer is the \a expectedValue,
1492 the test-and-set functions assign the \a newValue to this
1493 QAtomicPointer and return true. If the values are \e not the same,
1494 this function does nothing and returns \c false.
1495
1496 This function uses \e ordered \l {QAtomicPointer#Memory
1497 ordering}{memory ordering} semantics, which ensures that memory
1498 access before and after the atomic operation (in program order)
1499 may not be re-ordered.
1500*/
1501
1502/*!
1503 \fn template <typename T> bool QAtomicPointer<T>::isFetchAndStoreNative()
1504
1505 Returns \c true if fetch-and-store is implemented using atomic
1506 processor instructions, false otherwise.
1507*/
1508
1509/*!
1510 \fn template <typename T> bool QAtomicPointer<T>::isFetchAndStoreWaitFree()
1511
1512 Returns \c true if atomic fetch-and-store is wait-free, false
1513 otherwise.
1514*/
1515
1516/*!
1517 \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreRelaxed(T *newValue)
1518
1519 Atomic fetch-and-store.
1520
1521 Reads the current value of this QAtomicPointer and then assigns it the
1522 \a newValue, returning the original value.
1523
1524 This function uses \e relaxed \l {QAtomicPointer#Memory
1525 ordering}{memory ordering} semantics, leaving the compiler and
1526 processor to freely reorder memory accesses.
1527*/
1528
1529/*!
1530 \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreAcquire(T *newValue)
1531
1532 Atomic fetch-and-store.
1533
1534 Reads the current value of this QAtomicPointer and then assigns it the
1535 \a newValue, returning the original value.
1536
1537 This function uses \e acquire \l {QAtomicPointer#Memory
1538 ordering}{memory ordering} semantics, which ensures that memory
1539 access following the atomic operation (in program order) may not
1540 be re-ordered before the atomic operation.
1541*/
1542
1543/*!
1544 \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreRelease(T *newValue)
1545
1546 Atomic fetch-and-store.
1547
1548 Reads the current value of this QAtomicPointer and then assigns it the
1549 \a newValue, returning the original value.
1550
1551 This function uses \e release \l {QAtomicPointer#Memory
1552 ordering}{memory ordering} semantics, which ensures that memory
1553 access before the atomic operation (in program order) may not be
1554 re-ordered after the atomic operation.
1555*/
1556
1557/*!
1558 \fn template <typename T> T *QAtomicPointer<T>::fetchAndStoreOrdered(T *newValue)
1559
1560 Atomic fetch-and-store.
1561
1562 Reads the current value of this QAtomicPointer and then assigns it the
1563 \a newValue, returning the original value.
1564
1565 This function uses \e ordered \l {QAtomicPointer#Memory
1566 ordering}{memory ordering} semantics, which ensures that memory
1567 access before and after the atomic operation (in program order)
1568 may not be re-ordered.
1569*/
1570
1571/*!
1572 \fn template <typename T> bool QAtomicPointer<T>::isFetchAndAddNative()
1573
1574 Returns \c true if fetch-and-add is implemented using atomic
1575 processor instructions, false otherwise.
1576*/
1577
1578/*!
1579 \fn template <typename T> bool QAtomicPointer<T>::isFetchAndAddWaitFree()
1580
1581 Returns \c true if atomic fetch-and-add is wait-free, false
1582 otherwise.
1583*/
1584
1585/*!
1586 \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddRelaxed(qptrdiff valueToAdd)
1587
1588 Atomic fetch-and-add.
1589
1590 Reads the current value of this QAtomicPointer and then adds
1591 \a valueToAdd to the current value, returning the original value.
1592
1593 This function uses \e relaxed \l {QAtomicPointer#Memory
1594 ordering}{memory ordering} semantics, leaving the compiler and
1595 processor to freely reorder memory accesses.
1596*/
1597
1598/*!
1599 \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddAcquire(qptrdiff valueToAdd)
1600
1601 Atomic fetch-and-add.
1602
1603 Reads the current value of this QAtomicPointer and then adds
1604 \a valueToAdd to the current value, returning the original value.
1605
1606 This function uses \e acquire \l {QAtomicPointer#Memory
1607 ordering}{memory ordering} semantics, which ensures that memory
1608 access following the atomic operation (in program order) may not
1609 be re-ordered before the atomic operation.
1610*/
1611
1612/*!
1613 \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddRelease(qptrdiff valueToAdd)
1614
1615 Atomic fetch-and-add.
1616
1617 Reads the current value of this QAtomicPointer and then adds
1618 \a valueToAdd to the current value, returning the original value.
1619
1620 This function uses \e release \l {QAtomicPointer#Memory
1621 ordering}{memory ordering} semantics, which ensures that memory
1622 access before the atomic operation (in program order) may not be
1623 re-ordered after the atomic operation.
1624*/
1625
1626/*!
1627 \fn template <typename T> T *QAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
1628
1629 Atomic fetch-and-add.
1630
1631 Reads the current value of this QAtomicPointer and then adds
1632 \a valueToAdd to the current value, returning the original value.
1633
1634 This function uses \e ordered \l {QAtomicPointer#Memory
1635 ordering}{memory ordering} semantics, which ensures that memory
1636 access before and after the atomic operation (in program order)
1637 may not be re-ordered.
1638*/
1639
1640/*!
1641 \macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
1642 \relates QAtomicPointer
1643
1644 This macro is defined if and only if your processor supports
1645 atomic test-and-set on pointers.
1646*/
1647
1648/*!
1649 \macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE
1650 \relates QAtomicPointer
1651
1652 This macro is defined when only certain generations of the
1653 processor support atomic test-and-set on pointers. Use the
1654 QAtomicPointer::isTestAndSetNative() function to check what your
1655 processor supports.
1656*/
1657
1658/*!
1659 \macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE
1660 \relates QAtomicPointer
1661
1662 This macro is defined when the hardware does not support atomic
1663 test-and-set on pointers.
1664*/
1665
1666/*!
1667 \macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
1668 \relates QAtomicPointer
1669
1670 This macro is defined together with
1671 Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE to indicate that
1672 the atomic test-and-set on pointers is wait-free.
1673*/
1674
1675/*!
1676 \macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
1677 \relates QAtomicPointer
1678
1679 This macro is defined if and only if your processor supports
1680 atomic fetch-and-store on pointers.
1681*/
1682
1683/*!
1684 \macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
1685 \relates QAtomicPointer
1686
1687 This macro is defined when only certain generations of the
1688 processor support atomic fetch-and-store on pointers. Use the
1689 QAtomicPointer::isFetchAndStoreNative() function to check what
1690 your processor supports.
1691*/
1692
1693/*!
1694 \macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_NOT_NATIVE
1695 \relates QAtomicPointer
1696
1697 This macro is defined when the hardware does not support atomic
1698 fetch-and-store on pointers.
1699*/
1700
1701/*!
1702 \macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
1703 \relates QAtomicPointer
1704
1705 This macro is defined together with
1706 Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE to indicate that
1707 the atomic fetch-and-store on pointers is wait-free.
1708*/
1709
1710/*!
1711 \macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
1712 \relates QAtomicPointer
1713
1714 This macro is defined if and only if your processor supports
1715 atomic fetch-and-add on pointers.
1716*/
1717
1718/*!
1719 \macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
1720 \relates QAtomicPointer
1721
1722 This macro is defined when only certain generations of the
1723 processor support atomic fetch-and-add on pointers. Use the
1724 QAtomicPointer::isFetchAndAddNative() function to check what your
1725 processor supports.
1726*/
1727
1728/*!
1729 \macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_NOT_NATIVE
1730 \relates QAtomicPointer
1731
1732 This macro is defined when the hardware does not support atomic
1733 fetch-and-add on pointers.
1734*/
1735
1736/*!
1737 \macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
1738 \relates QAtomicPointer
1739
1740 This macro is defined together with
1741 Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE to indicate that
1742 the atomic fetch-and-add on pointers is wait-free.
1743*/
1744
1745// static checks
1746#ifndef Q_ATOMIC_INT8_IS_SUPPORTED
1747# error "Q_ATOMIC_INT8_IS_SUPPORTED must be defined"
1748#endif
1749#ifndef Q_ATOMIC_INT16_IS_SUPPORTED
1750# error "Q_ATOMIC_INT16_IS_SUPPORTED must be defined"
1751#endif
1752#ifndef Q_ATOMIC_INT32_IS_SUPPORTED
1753# error "Q_ATOMIC_INT32_IS_SUPPORTED must be defined"
1754#endif
1755#if !defined(Q_ATOMIC_INT64_IS_SUPPORTED) && QT_POINTER_SIZE == 8
1756// 64-bit platform
1757# error "Q_ATOMIC_INT64_IS_SUPPORTED must be defined on a 64-bit platform"
1758#endif
1759
1761
1762// The following specializations must always be defined
1763static_assert(sizeof(QAtomicInteger<unsigned>));
1764static_assert(sizeof(QAtomicInteger<long>));
1765static_assert(sizeof(QAtomicInteger<unsigned long>));
1766static_assert(sizeof(QAtomicInteger<quintptr>));
1767static_assert(sizeof(QAtomicInteger<qptrdiff>));
1768static_assert(sizeof(QAtomicInteger<char32_t>));
1769
1770static_assert(sizeof(QAtomicInteger<short>));
1771static_assert(sizeof(QAtomicInteger<unsigned short>));
1772static_assert(sizeof(QAtomicInteger<wchar_t>));
1773static_assert(sizeof(QAtomicInteger<char16_t>));
1774
1775static_assert(sizeof(QAtomicInteger<char>));
1776static_assert(sizeof(QAtomicInteger<unsigned char>));
1777static_assert(sizeof(QAtomicInteger<signed char>));
1778static_assert(sizeof(QAtomicInteger<bool>));
1779
1780#ifdef Q_ATOMIC_INT64_IS_SUPPORTED
1781static_assert(sizeof(QAtomicInteger<qint64>));
1782static_assert(sizeof(QAtomicInteger<quint64>));
1783#endif
1784
1785QT_END_NAMESPACE