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