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