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