1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 \brief The QAtomicInt class provides platform-independent atomic operations on integers.
49 For atomic operations on pointers, see the QAtomicPointer class.
51 An \e atomic operation is a complex operation that completes without interruption.
52 The QAtomicInt class provides atomic reference counting, test-and-set, fetch-and-store,
53 and fetch-and-add for integers.
55 \section1 Non-atomic convenience operators
57 For convenience, QAtomicInt provides integer comparison, cast, and
58 assignment operators. Note that a combination of these operators
59 is \e not an atomic operation.
61 \section1 The Atomic API
63 \section2 Reference counting
65 The ref() and deref() functions provide an efficient reference
66 counting API. The return value of these functions are used to
67 indicate when the last reference has been released. These
68 functions allow you to implement your own implicitly shared
71 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 0
73 \section2 Memory ordering
75 QAtomicInt provides several implementations of the atomic
76 test-and-set, fetch-and-store, and fetch-and-add functions. Each
77 implementation defines a memory ordering semantic that describes
78 how memory accesses surrounding the atomic instruction are
79 executed by the processor. Since many modern architectures allow
80 out-of-order execution and memory ordering, using the correct
81 semantic is necessary to ensure that your application functions
82 properly on all processors.
86 \li Relaxed - memory ordering is unspecified, leaving the compiler
87 and processor to freely reorder memory accesses.
89 \li Acquire - memory access following the atomic operation (in
90 program order) may not be re-ordered before the atomic operation.
92 \li Release - memory access before the atomic operation (in program
93 order) may not be re-ordered after the atomic operation.
95 \li Ordered - the same Acquire and Release semantics combined.
99 \section2 Test-and-set
101 If the current value of the QAtomicInt is an expected value, the
102 test-and-set functions assign a new value to the QAtomicInt and
103 return true. If values are \a not the same, these functions do
104 nothing and return false. This operation equates to the following
107 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 1
109 There are 4 test-and-set functions: testAndSetRelaxed(),
110 testAndSetAcquire(), testAndSetRelease(), and
111 testAndSetOrdered(). See above for an explanation of the different
112 memory ordering semantics.
114 \section2 Fetch-and-store
116 The atomic fetch-and-store functions read the current value of the
117 QAtomicInt and then assign a new value, returning the original
118 value. This operation equates to the following code:
120 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 2
122 There are 4 fetch-and-store functions: fetchAndStoreRelaxed(),
123 fetchAndStoreAcquire(), fetchAndStoreRelease(), and
124 fetchAndStoreOrdered(). See above for an explanation of the
125 different memory ordering semantics.
127 \section2 Fetch-and-add
129 The atomic fetch-and-add functions read the current value of the
130 QAtomicInt and then add the given value to the current value,
131 returning the original value. This operation equates to the
134 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 3
136 There are 4 fetch-and-add functions: fetchAndAddRelaxed(),
137 fetchAndAddAcquire(), fetchAndAddRelease(), and
138 fetchAndAddOrdered(). See above for an explanation of the
139 different memory ordering semantics.
141 \section1 Feature Tests for the Atomic API
143 Providing a platform-independent atomic API that works on all
144 processors is challenging. The API provided by QAtomicInt is
145 guaranteed to work atomically on all processors. However, since
146 not all processors implement support for every operation provided
147 by QAtomicInt, it is necessary to expose information about the
150 You can check at compile time which features are supported on your
151 hardware using various macros. These will tell you if your
152 hardware always, sometimes, or does not support a particular
153 operation. The macros have the form
154 Q_ATOMIC_INT_\e{OPERATION}_IS_\e{HOW}_NATIVE. \e{OPERATION}
155 is one of REFERENCE_COUNTING, TEST_AND_SET,
156 FETCH_AND_STORE, or FETCH_AND_ADD, and \e{HOW} is one of
157 ALWAYS, SOMETIMES, or NOT. There will always be exactly one
158 defined macro per operation. For example, if
159 Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE is defined,
160 neither Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE nor
161 Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE will be defined.
163 An operation that completes in constant time is said to be
164 wait-free. Such operations are not implemented using locks or
165 loops of any kind. For atomic operations that are always
166 supported, and that are wait-free, Qt defines the
167 Q_ATOMIC_INT_\e{OPERATION}_IS_WAIT_FREE in addition to the
168 Q_ATOMIC_INT_\e{OPERATION}_IS_ALWAYS_NATIVE.
170 In cases where an atomic operation is only supported in newer
171 generations of the processor, QAtomicInt also provides a way to
172 check at runtime what your hardware supports with the
173 isReferenceCountingNative(), isTestAndSetNative(),
174 isFetchAndStoreNative(), and isFetchAndAddNative()
175 functions. Wait-free implementations can be detected using the
176 isReferenceCountingWaitFree(), isTestAndSetWaitFree(),
177 isFetchAndStoreWaitFree(), and isFetchAndAddWaitFree() functions.
179 Below is a complete list of all feature macros for QAtomicInt:
183 \li Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
184 \li Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE
185 \li Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE
186 \li Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
188 \li Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
189 \li Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE
190 \li Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE
191 \li Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
193 \li Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
194 \li Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
195 \li Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE
196 \li Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
198 \li Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
199 \li Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
200 \li Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE
201 \li Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
208 /*! \fn QAtomicInt::QAtomicInt(int value)
210 Constructs a QAtomicInt with the given \a value.
213 /*! \fn QAtomicInt::QAtomicInt(const QAtomicInt &other)
215 Constructs a copy of \a other.
218 /*! \fn QAtomicInt &QAtomicInt::operator=(int value)
220 Assigns the \a value to this QAtomicInt and returns a reference to
224 /*! \fn QAtomicInt &QAtomicInt::operator=(const QAtomicInt &other)
226 Assigns \a other to this QAtomicInt and returns a reference to
230 /*! \fn bool QAtomicInt::operator==(int value) const
232 Returns true if the \a value is equal to the value in this
233 QAtomicInt; otherwise returns false.
236 /*! \fn bool QAtomicInt::operator!=(int value) const
238 Returns true if the value of this QAtomicInt is not equal to \a
239 value; otherwise returns false.
242 /*! \fn bool QAtomicInt::operator!() const
244 Returns true is the value of this QAtomicInt is zero; otherwise
248 /*! \fn QAtomicInt::operator int() const
250 Returns the value stored by the QAtomicInt object as an integer.
253 /*! \fn bool QAtomicInt::isReferenceCountingNative()
255 Returns true if reference counting is implemented using atomic
256 processor instructions, false otherwise.
259 /*! \fn bool QAtomicInt::isReferenceCountingWaitFree()
261 Returns true if atomic reference counting is wait-free, false
265 /*! \fn bool QAtomicInt::ref()
266 Atomically increments the value of this QAtomicInt. Returns true
267 if the new value is non-zero, false otherwise.
269 This function uses \e ordered \l {QAtomicInt#Memory
270 ordering}{memory ordering} semantics, which ensures that memory
271 access before and after the atomic operation (in program order)
272 may not be re-ordered.
277 /*! \fn bool QAtomicInt::deref()
278 Atomically decrements the value of this QAtomicInt. Returns true
279 if the new value is non-zero, false otherwise.
281 This function uses \e ordered \l {QAtomicInt#Memory
282 ordering}{memory ordering} semantics, which ensures that memory
283 access before and after the atomic operation (in program order)
284 may not be re-ordered.
289 /*! \fn bool QAtomicInt::isTestAndSetNative()
291 Returns true if test-and-set is implemented using atomic processor
292 instructions, false otherwise.
295 /*! \fn bool QAtomicInt::isTestAndSetWaitFree()
297 Returns true if atomic test-and-set is wait-free, false otherwise.
300 /*! \fn bool QAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
304 If the current value of this QAtomicInt is the \a expectedValue,
305 the test-and-set functions assign the \a newValue to this
306 QAtomicInt and return true. If the values are \e not the same,
307 this function does nothing and returns false.
309 This function uses \e relaxed \l {QAtomicInt#Memory
310 ordering}{memory ordering} semantics, leaving the compiler and
311 processor to freely reorder memory accesses.
314 /*! \fn bool QAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
318 If the current value of this QAtomicInt is the \a expectedValue,
319 the test-and-set functions assign the \a newValue to this
320 QAtomicInt and return true. If the values are \e not the same,
321 this function does nothing and returns false.
323 This function uses \e acquire \l {QAtomicInt#Memory
324 ordering}{memory ordering} semantics, which ensures that memory
325 access following the atomic operation (in program order) may not
326 be re-ordered before the atomic operation.
329 /*! \fn bool QAtomicInt::testAndSetRelease(int expectedValue, int newValue)
333 If the current value of this QAtomicInt is the \a expectedValue,
334 the test-and-set functions assign the \a newValue to this
335 QAtomicInt and return true. If the values are \e not the same,
336 this function does nothing and returns false.
338 This function uses \e release \l {QAtomicInt#Memory
339 ordering}{memory ordering} semantics, which ensures that memory
340 access before the atomic operation (in program order) may not be
341 re-ordered after the atomic operation.
344 /*! \fn bool QAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
348 If the current value of this QAtomicInt is the \a expectedValue,
349 the test-and-set functions assign the \a newValue to this
350 QAtomicInt and return true. If the values are \e not the same,
351 this function does nothing and returns false.
353 This function uses \e ordered \l {QAtomicInt#Memory
354 ordering}{memory ordering} semantics, which ensures that memory
355 access before and after the atomic operation (in program order)
356 may not be re-ordered.
359 /*! \fn bool QAtomicInt::isFetchAndStoreNative()
361 Returns true if fetch-and-store is implemented using atomic
362 processor instructions, false otherwise.
365 /*! \fn bool QAtomicInt::isFetchAndStoreWaitFree()
367 Returns true if atomic fetch-and-store is wait-free, false
371 /*! \fn int QAtomicInt::fetchAndStoreRelaxed(int newValue)
373 Atomic fetch-and-store.
375 Reads the current value of this QAtomicInt and then assigns it the
376 \a newValue, returning the original value.
378 This function uses \e relaxed \l {QAtomicInt#Memory
379 ordering}{memory ordering} semantics, leaving the compiler and
380 processor to freely reorder memory accesses.
383 /*! \fn int QAtomicInt::fetchAndStoreAcquire(int newValue)
385 Atomic fetch-and-store.
387 Reads the current value of this QAtomicInt and then assigns it the
388 \a newValue, returning the original value.
390 This function uses \e acquire \l {QAtomicInt#Memory
391 ordering}{memory ordering} semantics, which ensures that memory
392 access following the atomic operation (in program order) may not
393 be re-ordered before the atomic operation.
396 /*! \fn int QAtomicInt::fetchAndStoreRelease(int newValue)
398 Atomic fetch-and-store.
400 Reads the current value of this QAtomicInt and then assigns it the
401 \a newValue, returning the original value.
403 This function uses \e release \l {QAtomicInt#Memory
404 ordering}{memory ordering} semantics, which ensures that memory
405 access before the atomic operation (in program order) may not be
406 re-ordered after the atomic operation.
409 /*! \fn int QAtomicInt::fetchAndStoreOrdered(int newValue)
411 Atomic fetch-and-store.
413 Reads the current value of this QAtomicInt and then assigns it the
414 \a newValue, returning the original value.
416 This function uses \e ordered \l {QAtomicInt#Memory
417 ordering}{memory ordering} semantics, which ensures that memory
418 access before and after the atomic operation (in program order)
419 may not be re-ordered.
422 /*! \fn bool QAtomicInt::isFetchAndAddNative()
424 Returns true if fetch-and-add is implemented using atomic
425 processor instructions, false otherwise.
428 /*! \fn bool QAtomicInt::isFetchAndAddWaitFree()
430 Returns true if atomic fetch-and-add is wait-free, false
434 /*! \fn int QAtomicInt::fetchAndAddRelaxed(int valueToAdd)
436 Atomic fetch-and-add.
438 Reads the current value of this QAtomicInt and then adds
439 \a valueToAdd to the current value, returning the original value.
441 This function uses \e relaxed \l {QAtomicInt#Memory
442 ordering}{memory ordering} semantics, leaving the compiler and
443 processor to freely reorder memory accesses.
446 /*! \fn int QAtomicInt::fetchAndAddAcquire(int valueToAdd)
448 Atomic fetch-and-add.
450 Reads the current value of this QAtomicInt and then adds
451 \a valueToAdd to the current value, returning the original value.
453 This function uses \e acquire \l {QAtomicInt#Memory
454 ordering}{memory ordering} semantics, which ensures that memory
455 access following the atomic operation (in program order) may not
456 be re-ordered before the atomic operation.
459 /*! \fn int QAtomicInt::fetchAndAddRelease(int valueToAdd)
461 Atomic fetch-and-add.
463 Reads the current value of this QAtomicInt and then adds
464 \a valueToAdd to the current value, returning the original value.
466 This function uses \e release \l {QAtomicInt#Memory
467 ordering}{memory ordering} semantics, which ensures that memory
468 access before the atomic operation (in program order) may not be
469 re-ordered after the atomic operation.
472 /*! \fn int QAtomicInt::fetchAndAddOrdered(int valueToAdd)
474 Atomic fetch-and-add.
476 Reads the current value of this QAtomicInt and then adds
477 \a valueToAdd to the current value, returning the original value.
479 This function uses \e ordered \l {QAtomicInt#Memory
480 ordering}{memory ordering} semantics, which ensures that memory
481 access before and after the atomic operation (in program order)
482 may not be re-ordered.
486 \macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
489 This macro is defined if and only if all generations of your
490 processor support atomic reference counting.
494 \macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE
497 This macro is defined when only certain generations of the
498 processor support atomic reference counting. Use the
499 QAtomicInt::isReferenceCountingNative() function to check what
500 your processor supports.
504 \macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE
507 This macro is defined when the hardware does not support atomic
512 \macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
515 This macro is defined together with
516 Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE to indicate that
517 the reference counting is wait-free.
521 \macro Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
524 This macro is defined if and only if your processor supports
525 atomic test-and-set on integers.
529 \macro Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE
532 This macro is defined when only certain generations of the
533 processor support atomic test-and-set on integers. Use the
534 QAtomicInt::isTestAndSetNative() function to check what your
539 \macro Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE
542 This macro is defined when the hardware does not support atomic
543 test-and-set on integers.
547 \macro Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
550 This macro is defined together with
551 Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE to indicate that the
552 atomic test-and-set on integers is wait-free.
556 \macro Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
559 This macro is defined if and only if your processor supports
560 atomic fetch-and-store on integers.
564 \macro Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
567 This macro is defined when only certain generations of the
568 processor support atomic fetch-and-store on integers. Use the
569 QAtomicInt::isFetchAndStoreNative() function to check what your
574 \macro Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE
577 This macro is defined when the hardware does not support atomic
578 fetch-and-store on integers.
582 \macro Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
585 This macro is defined together with
586 Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE to indicate that the
587 atomic fetch-and-store on integers is wait-free.
591 \macro Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
594 This macro is defined if and only if your processor supports
595 atomic fetch-and-add on integers.
599 \macro Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
602 This macro is defined when only certain generations of the
603 processor support atomic fetch-and-add on integers. Use the
604 QAtomicInt::isFetchAndAddNative() function to check what your
609 \macro Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE
612 This macro is defined when the hardware does not support atomic
613 fetch-and-add on integers.
617 \macro Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
620 This macro is defined together with
621 Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE to indicate that the
622 atomic fetch-and-add on integers is wait-free.
629 \class QAtomicPointer
630 \brief The QAtomicPointer class is a template class that provides platform-independent atomic operations on pointers.
635 For atomic operations on integers, see the QAtomicInt class.
637 An \e atomic operation is a complex operation that completes without interruption.
638 The QAtomicPointer class provides atomic test-and-set, fetch-and-store, and fetch-and-add for pointers.
640 \section1 Non-atomic convenience operators
642 For convenience, QAtomicPointer provides pointer comparison, cast,
643 dereference, and assignment operators. Note that these operators
646 \section1 The Atomic API
648 \section2 Memory ordering
650 QAtomicPointer provides several implementations of the atomic
651 test-and-set, fetch-and-store, and fetch-and-add functions. Each
652 implementation defines a memory ordering semantic that describes
653 how memory accesses surrounding the atomic instruction are
654 executed by the processor. Since many modern architectures allow
655 out-of-order execution and memory ordering, using the correct
656 semantic is necessary to ensure that your application functions
657 properly on all processors.
661 \li Relaxed - memory ordering is unspecified, leaving the compiler
662 and processor to freely reorder memory accesses.
664 \li Acquire - memory access following the atomic operation (in
665 program order) may not be re-ordered before the atomic operation.
667 \li Release - memory access before the atomic operation (in program
668 order) may not be re-ordered after the atomic operation.
670 \li Ordered - the same Acquire and Release semantics combined.
674 \section2 Test-and-set
676 If the current value of the QAtomicPointer is an expected value,
677 the test-and-set functions assign a new value to the
678 QAtomicPointer and return true. If values are \a not the same,
679 these functions do nothing and return false. This operation
680 equates to the following code:
682 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 4
684 There are 4 test-and-set functions: testAndSetRelaxed(),
685 testAndSetAcquire(), testAndSetRelease(), and
686 testAndSetOrdered(). See above for an explanation of the different
687 memory ordering semantics.
689 \section2 Fetch-and-store
691 The atomic fetch-and-store functions read the current value of the
692 QAtomicPointer and then assign a new value, returning the original
693 value. This operation equates to the following code:
695 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 5
697 There are 4 fetch-and-store functions: fetchAndStoreRelaxed(),
698 fetchAndStoreAcquire(), fetchAndStoreRelease(), and
699 fetchAndStoreOrdered(). See above for an explanation of the
700 different memory ordering semantics.
702 \section2 Fetch-and-add
704 The atomic fetch-and-add functions read the current value of the
705 QAtomicPointer and then add the given value to the current value,
706 returning the original value. This operation equates to the
709 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 6
711 There are 4 fetch-and-add functions: fetchAndAddRelaxed(),
712 fetchAndAddAcquire(), fetchAndAddRelease(), and
713 fetchAndAddOrdered(). See above for an explanation of the
714 different memory ordering semantics.
716 \section1 Feature Tests for the Atomic API
718 Providing a platform-independent atomic API that works on all
719 processors is challenging. The API provided by QAtomicPointer is
720 guaranteed to work atomically on all processors. However, since
721 not all processors implement support for every operation provided
722 by QAtomicPointer, it is necessary to expose information about the
725 You can check at compile time which features are supported on your
726 hardware using various macros. These will tell you if your
727 hardware always, sometimes, or does not support a particular
728 operation. The macros have the form
729 Q_ATOMIC_POINTER_\e{OPERATION}_IS_\e{HOW}_NATIVE. \e{OPERATION} is
730 one of TEST_AND_SET, FETCH_AND_STORE, or FETCH_AND_ADD, and
731 \e{HOW} is one of ALWAYS, SOMETIMES, or NOT. There will always be
732 exactly one defined macro per operation. For example, if
733 Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE is defined, neither
734 Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE nor
735 Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE will be defined.
737 An operation that completes in constant time is said to be
738 wait-free. Such operations are not implemented using locks or
739 loops of any kind. For atomic operations that are always
740 supported, and that are wait-free, Qt defines the
741 Q_ATOMIC_POINTER_\e{OPERATION}_IS_WAIT_FREE in addition to the
742 Q_ATOMIC_POINTER_\e{OPERATION}_IS_ALWAYS_NATIVE.
744 In cases where an atomic operation is only supported in newer
745 generations of the processor, QAtomicPointer also provides a way
746 to check at runtime what your hardware supports with the
747 isTestAndSetNative(), isFetchAndStoreNative(), and
748 isFetchAndAddNative() functions. Wait-free implementations can be
749 detected using the isTestAndSetWaitFree(),
750 isFetchAndStoreWaitFree(), and isFetchAndAddWaitFree() functions.
752 Below is a complete list of all feature macros for QAtomicPointer:
756 \li Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
757 \li Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE
758 \li Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE
759 \li Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
761 \li Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
762 \li Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
763 \li Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_NOT_NATIVE
764 \li Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
766 \li Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
767 \li Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
768 \li Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_NOT_NATIVE
769 \li Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
776 /*! \fn QAtomicPointer::QAtomicPointer(T *value)
778 Constructs a QAtomicPointer with the given \a value.
781 /*! \fn QAtomicPointer::QAtomicPointer(const QAtomicPointer<T> &other)
783 Constructs a copy of \a other.
786 /*! \fn QAtomicPointer<T> &QAtomicPointer::operator=(T *value)
788 Assigns the \a value to this QAtomicPointer and returns a
789 reference to this QAtomicPointer.
792 /*! \fn QAtomicPointer<T> &QAtomicPointer::operator=(const QAtomicPointer<T> &other)
794 Assigns \a other to this QAtomicPointer and returns a reference to
798 /*! \fn bool QAtomicPointer::operator==(T *value) const
800 Returns true if the \a value is equal to the value in this
801 QAtomicPointer; otherwise returns false.
804 /*! \fn bool QAtomicPointer::operator!=(T *value) const
806 Returns true if the value of this QAtomicPointer is not equal to
807 \a value; otherwise returns false.
810 /*! \fn bool QAtomicPointer::operator!() const
812 Returns true is the current value of this QAtomicPointer is zero;
813 otherwise returns false.
816 /*! \fn QAtomicPointer::operator T *() const
818 Returns the current pointer value stored by this QAtomicPointer
822 /*! \fn T *QAtomicPointer::operator->() const
826 /*! \fn bool QAtomicPointer::isTestAndSetNative()
828 Returns true if test-and-set is implemented using atomic processor
829 instructions, false otherwise.
832 /*! \fn bool QAtomicPointer::isTestAndSetWaitFree()
834 Returns true if atomic test-and-set is wait-free, false otherwise.
837 /*! \fn bool QAtomicPointer::testAndSetRelaxed(T *expectedValue, T *newValue)
841 If the current value of this QAtomicPointer is the \a expectedValue,
842 the test-and-set functions assign the \a newValue to this
843 QAtomicPointer and return true. If the values are \e not the same,
844 this function does nothing and returns false.
846 This function uses \e relaxed \l {QAtomicPointer#Memory
847 ordering}{memory ordering} semantics, leaving the compiler and
848 processor to freely reorder memory accesses.
851 /*! \fn bool QAtomicPointer::testAndSetAcquire(T *expectedValue, T *newValue)
855 If the current value of this QAtomicPointer is the \a expectedValue,
856 the test-and-set functions assign the \a newValue to this
857 QAtomicPointer and return true. If the values are \e not the same,
858 this function does nothing and returns false.
860 This function uses \e acquire \l {QAtomicPointer#Memory
861 ordering}{memory ordering} semantics, which ensures that memory
862 access following the atomic operation (in program order) may not
863 be re-ordered before the atomic operation.
866 /*! \fn bool QAtomicPointer::testAndSetRelease(T *expectedValue, T *newValue)
870 If the current value of this QAtomicPointer is the \a expectedValue,
871 the test-and-set functions assign the \a newValue to this
872 QAtomicPointer and return true. If the values are \e not the same,
873 this function does nothing and returns false.
875 This function uses \e release \l {QAtomicPointer#Memory
876 ordering}{memory ordering} semantics, which ensures that memory
877 access before the atomic operation (in program order) may not be
878 re-ordered after the atomic operation.
881 /*! \fn bool QAtomicPointer::testAndSetOrdered(T *expectedValue, T *newValue)
885 If the current value of this QAtomicPointer is the \a expectedValue,
886 the test-and-set functions assign the \a newValue to this
887 QAtomicPointer and return true. If the values are \e not the same,
888 this function does nothing and returns false.
890 This function uses \e ordered \l {QAtomicPointer#Memory
891 ordering}{memory ordering} semantics, which ensures that memory
892 access before and after the atomic operation (in program order)
893 may not be re-ordered.
896 /*! \fn bool QAtomicPointer::isFetchAndStoreNative()
898 Returns true if fetch-and-store is implemented using atomic
899 processor instructions, false otherwise.
902 /*! \fn bool QAtomicPointer::isFetchAndStoreWaitFree()
904 Returns true if atomic fetch-and-store is wait-free, false
908 /*! \fn T *QAtomicPointer::fetchAndStoreRelaxed(T *newValue)
910 Atomic fetch-and-store.
912 Reads the current value of this QAtomicPointer and then assigns it the
913 \a newValue, returning the original value.
915 This function uses \e relaxed \l {QAtomicPointer#Memory
916 ordering}{memory ordering} semantics, leaving the compiler and
917 processor to freely reorder memory accesses.
920 /*! \fn T *QAtomicPointer::fetchAndStoreAcquire(T *newValue)
922 Atomic fetch-and-store.
924 Reads the current value of this QAtomicPointer and then assigns it the
925 \a newValue, returning the original value.
927 This function uses \e acquire \l {QAtomicPointer#Memory
928 ordering}{memory ordering} semantics, which ensures that memory
929 access following the atomic operation (in program order) may not
930 be re-ordered before the atomic operation.
933 /*! \fn T *QAtomicPointer::fetchAndStoreRelease(T *newValue)
935 Atomic fetch-and-store.
937 Reads the current value of this QAtomicPointer and then assigns it the
938 \a newValue, returning the original value.
940 This function uses \e release \l {QAtomicPointer#Memory
941 ordering}{memory ordering} semantics, which ensures that memory
942 access before the atomic operation (in program order) may not be
943 re-ordered after the atomic operation.
946 /*! \fn T *QAtomicPointer::fetchAndStoreOrdered(T *newValue)
948 Atomic fetch-and-store.
950 Reads the current value of this QAtomicPointer and then assigns it the
951 \a newValue, returning the original value.
953 This function uses \e ordered \l {QAtomicPointer#Memory
954 ordering}{memory ordering} semantics, which ensures that memory
955 access before and after the atomic operation (in program order)
956 may not be re-ordered.
959 /*! \fn bool QAtomicPointer::isFetchAndAddNative()
961 Returns true if fetch-and-add is implemented using atomic
962 processor instructions, false otherwise.
965 /*! \fn bool QAtomicPointer::isFetchAndAddWaitFree()
967 Returns true if atomic fetch-and-add is wait-free, false
971 /*! \fn T *QAtomicPointer::fetchAndAddRelaxed(qptrdiff valueToAdd)
973 Atomic fetch-and-add.
975 Reads the current value of this QAtomicPointer and then adds
976 \a valueToAdd to the current value, returning the original value.
978 This function uses \e relaxed \l {QAtomicPointer#Memory
979 ordering}{memory ordering} semantics, leaving the compiler and
980 processor to freely reorder memory accesses.
983 /*! \fn T *QAtomicPointer::fetchAndAddAcquire(qptrdiff valueToAdd)
985 Atomic fetch-and-add.
987 Reads the current value of this QAtomicPointer and then adds
988 \a valueToAdd to the current value, returning the original value.
990 This function uses \e acquire \l {QAtomicPointer#Memory
991 ordering}{memory ordering} semantics, which ensures that memory
992 access following the atomic operation (in program order) may not
993 be re-ordered before the atomic operation.
996 /*! \fn T *QAtomicPointer::fetchAndAddRelease(qptrdiff valueToAdd)
998 Atomic fetch-and-add.
1000 Reads the current value of this QAtomicPointer and then adds
1001 \a valueToAdd to the current value, returning the original value.
1003 This function uses \e release \l {QAtomicPointer#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.
1009 /*! \fn T *QAtomicPointer::fetchAndAddOrdered(qptrdiff valueToAdd)
1011 Atomic fetch-and-add.
1013 Reads the current value of this QAtomicPointer and then adds
1014 \a valueToAdd to the current value, returning the original value.
1016 This function uses \e ordered \l {QAtomicPointer#Memory
1017 ordering}{memory ordering} semantics, which ensures that memory
1018 access before and after the atomic operation (in program order)
1019 may not be re-ordered.
1023 \macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
1024 \relates QAtomicPointer
1026 This macro is defined if and only if your processor supports
1027 atomic test-and-set on pointers.
1031 \macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE
1032 \relates QAtomicPointer
1034 This macro is defined when only certain generations of the
1035 processor support atomic test-and-set on pointers. Use the
1036 QAtomicPointer::isTestAndSetNative() function to check what your
1041 \macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE
1042 \relates QAtomicPointer
1044 This macro is defined when the hardware does not support atomic
1045 test-and-set on pointers.
1049 \macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
1050 \relates QAtomicPointer
1052 This macro is defined together with
1053 Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE to indicate that
1054 the atomic test-and-set on pointers is wait-free.
1058 \macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
1059 \relates QAtomicPointer
1061 This macro is defined if and only if your processor supports
1062 atomic fetch-and-store on pointers.
1066 \macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
1067 \relates QAtomicPointer
1069 This macro is defined when only certain generations of the
1070 processor support atomic fetch-and-store on pointers. Use the
1071 QAtomicPointer::isFetchAndStoreNative() function to check what
1072 your processor supports.
1076 \macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_NOT_NATIVE
1077 \relates QAtomicPointer
1079 This macro is defined when the hardware does not support atomic
1080 fetch-and-store on pointers.
1084 \macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
1085 \relates QAtomicPointer
1087 This macro is defined together with
1088 Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE to indicate that
1089 the atomic fetch-and-store on pointers is wait-free.
1093 \macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
1094 \relates QAtomicPointer
1096 This macro is defined if and only if your processor supports
1097 atomic fetch-and-add on pointers.
1101 \macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
1102 \relates QAtomicPointer
1104 This macro is defined when only certain generations of the
1105 processor support atomic fetch-and-add on pointers. Use the
1106 QAtomicPointer::isFetchAndAddNative() function to check what your
1111 \macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_NOT_NATIVE
1112 \relates QAtomicPointer
1114 This macro is defined when the hardware does not support atomic
1115 fetch-and-add on pointers.
1119 \macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
1120 \relates QAtomicPointer
1122 This macro is defined together with
1123 Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE to indicate that
1124 the atomic fetch-and-add on pointers is wait-free.