1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\class QAtomicInt
\brief The QAtomicInt class provides platform-independent atomic operations on integers.
\since 4.4
\ingroup thread
For atomic operations on pointers, see the QAtomicPointer class.
An complex operation that completes without interruption is said
to be \e atomic. The QAtomicInt class provides atomic reference
counting, test-and-set, fetch-and-store, and fetch-and-add for
integers.
\section1 Non-atomic convenience operators
For convenience, QAtomicInt provides integer comparison, cast, and
assignment operators. Note that a combination of these operators
is \e not an atomic operation.
\section1 The Atomic API
\section2 Reference counting
The ref() and deref() functions provide an efficient reference
counting API. The return value of these functions are used to
indicate when the last reference has been released. These
functions allow you to implement your own implicitly shared
classes.
\snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 0
\section2 Memory ordering
QAtomicInt provides several implementations of the atomic
test-and-set, fetch-and-store, and fetch-and-add functions. Each
implementation defines a memory ordering semantic that describes
how memory accesses surrounding the atomic instruction are
executed by the processor. Since many modern architectures allow
out-of-order execution and memory ordering, using the correct
semantic is necessary to ensure that your application functions
properly on all processors.
\list
\o Relaxed - memory ordering is unspecified, leaving the compiler
and processor to freely reorder memory accesses.
\o Acquire - memory access following the atomic operation (in
program order) may not be re-ordered before the atomic operation.
\o Release - memory access before the atomic operation (in program
order) may not be re-ordered after the atomic operation.
\o Ordered - the same Acquire and Release semantics combined.
\endlist
\section2 Test-and-set
If the current value of the QAtomicInt is an expected value, the
test-and-set functions assign a new value to the QAtomicInt and
return true. If values are \a not the same, these functions do
nothing and return false. This operation equates to the following
code:
\snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 1
There are 4 test-and-set functions: testAndSetRelaxed(),
testAndSetAcquire(), testAndSetRelease(), and
testAndSetOrdered(). See above for an explanation of the different
memory ordering semantics.
\section2 Fetch-and-store
The atomic fetch-and-store functions read the current value of the
QAtomicInt and then assign a new value, returning the original
value. This operation equates to the following code:
\snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 2
There are 4 fetch-and-store functions: fetchAndStoreRelaxed(),
fetchAndStoreAcquire(), fetchAndStoreRelease(), and
fetchAndStoreOrdered(). See above for an explanation of the
different memory ordering semantics.
\section2 Fetch-and-add
The atomic fetch-and-add functions read the current value of the
QAtomicInt and then add the given value to the current value,
returning the original value. This operation equates to the
following code:
\snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 3
There are 4 fetch-and-add functions: fetchAndAddRelaxed(),
fetchAndAddAcquire(), fetchAndAddRelease(), and
fetchAndAddOrdered(). See above for an explanation of the
different memory ordering semantics.
\section1 Feature Tests for the Atomic API
Providing a platform-independent atomic API that works on all
processors is challenging. The API provided by QAtomicInt is
guaranteed to work atomically on all processors. However, since
not all processors implement support for every operation provided
by QAtomicInt, it is necessary to expose information about the
processor.
You can check at compile time which features are supported on your
hardware using various macros. These will tell you if your
hardware always, sometimes, or does not support a particular
operation. The macros have the form
Q_ATOMIC_INT_\e{OPERATION}_IS_\e{HOW}_NATIVE. \e{OPERATION}
is one of REFERENCE_COUNTING, TEST_AND_SET,
FETCH_AND_STORE, or FETCH_AND_ADD, and \e{HOW} is one of
ALWAYS, SOMETIMES, or NOT. There will always be exactly one
defined macro per operation. For example, if
Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE is defined,
neither Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE nor
Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE will be defined.
An operation that completes in constant time is said to be
wait-free. Such operations are not implemented using locks or
loops of any kind. For atomic operations that are always
supported, and that are wait-free, Qt defines the
Q_ATOMIC_INT_\e{OPERATION}_IS_WAIT_FREE in addition to the
Q_ATOMIC_INT_\e{OPERATION}_IS_ALWAYS_NATIVE.
In cases where an atomic operation is only supported in newer
generations of the processor, QAtomicInt also provides a way to
check at runtime what your hardware supports with the
isReferenceCountingNative(), isTestAndSetNative(),
isFetchAndStoreNative(), and isFetchAndAddNative()
functions. Wait-free implementations can be detected using the
isReferenceCountingWaitFree(), isTestAndSetWaitFree(),
isFetchAndStoreWaitFree(), and isFetchAndAddWaitFree() functions.
Below is a complete list of all feature macros for QAtomicInt:
\list
\o Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
\o Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE
\o Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE
\o Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
\o Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
\o Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE
\o Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE
\o Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
\o Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
\o Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
\o Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE
\o Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
\o Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
\o Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
\o Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE
\o Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
\endlist
\sa QAtomicPointer
*/
/*! \fn QAtomicInt::QAtomicInt(int value)
Constructs a QAtomicInt with the given \a value.
*/
/*! \fn QAtomicInt::QAtomicInt(const QAtomicInt &other)
Constructs a copy of \a other.
*/
/*! \fn QAtomicInt &QAtomicInt::operator=(int value)
Assigns the \a value to this QAtomicInt and returns a reference to
this QAtomicInt.
*/
/*! \fn QAtomicInt &QAtomicInt::operator=(const QAtomicInt &other)
Assigns \a other to this QAtomicInt and returns a reference to
this QAtomicInt.
*/
/*! \fn bool QAtomicInt::operator==(int value) const
Returns true if the \a value is equal to the value in this
QAtomicInt; otherwise returns false.
*/
/*! \fn bool QAtomicInt::operator!=(int value) const
Returns true if the value of this QAtomicInt is not equal to \a
value; otherwise returns false.
*/
/*! \fn bool QAtomicInt::operator!() const
Returns true is the value of this QAtomicInt is zero; otherwise
returns false.
*/
/*! \fn QAtomicInt::operator int() const
Returns the value stored by the QAtomicInt object as an integer.
*/
/*! \fn bool QAtomicInt::isReferenceCountingNative()
Returns true if reference counting is implemented using atomic
processor instructions, false otherwise.
*/
/*! \fn bool QAtomicInt::isReferenceCountingWaitFree()
Returns true if atomic reference counting is wait-free, false
otherwise.
*/
/*! \fn bool QAtomicInt::ref()
Atomically increments the value of this QAtomicInt. Returns true
if the new value is non-zero, false otherwise.
This function uses \e ordered \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before and after the atomic operation (in program order)
may not be re-ordered.
\sa deref()
*/
/*! \fn bool QAtomicInt::deref()
Atomically decrements the value of this QAtomicInt. Returns true
if the new value is non-zero, false otherwise.
This function uses \e ordered \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before and after the atomic operation (in program order)
may not be re-ordered.
\sa ref()
*/
/*! \fn bool QAtomicInt::isTestAndSetNative()
Returns true if test-and-set is implemented using atomic processor
instructions, false otherwise.
*/
/*! \fn bool QAtomicInt::isTestAndSetWaitFree()
Returns true if atomic test-and-set is wait-free, false otherwise.
*/
/*! \fn bool QAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
Atomic test-and-set.
If the current value of this QAtomicInt is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicInt and return true. If the values are \e not the same,
this function does nothing and returns false.
This function uses \e relaxed \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, leaving the compiler and
processor to freely reorder memory accesses.
*/
/*! \fn bool QAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
Atomic test-and-set.
If the current value of this QAtomicInt is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicInt and return true. If the values are \e not the same,
this function does nothing and returns false.
This function uses \e acquire \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access following the atomic operation (in program order) may not
be re-ordered before the atomic operation.
*/
/*! \fn bool QAtomicInt::testAndSetRelease(int expectedValue, int newValue)
Atomic test-and-set.
If the current value of this QAtomicInt is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicInt and return true. If the values are \e not the same,
this function does nothing and returns false.
This function uses \e release \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before the atomic operation (in program order) may not be
re-ordered after the atomic operation.
*/
/*! \fn bool QAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
Atomic test-and-set.
If the current value of this QAtomicInt is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicInt and return true. If the values are \e not the same,
this function does nothing and returns false.
This function uses \e ordered \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before and after the atomic operation (in program order)
may not be re-ordered.
*/
/*! \fn bool QAtomicInt::isFetchAndStoreNative()
Returns true if fetch-and-store is implemented using atomic
processor instructions, false otherwise.
*/
/*! \fn bool QAtomicInt::isFetchAndStoreWaitFree()
Returns true if atomic fetch-and-store is wait-free, false
otherwise.
*/
/*! \fn int QAtomicInt::fetchAndStoreRelaxed(int newValue)
Atomic fetch-and-store.
Reads the current value of this QAtomicInt and then assigns it the
\a newValue, returning the original value.
This function uses \e relaxed \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, leaving the compiler and
processor to freely reorder memory accesses.
*/
/*! \fn int QAtomicInt::fetchAndStoreAcquire(int newValue)
Atomic fetch-and-store.
Reads the current value of this QAtomicInt and then assigns it the
\a newValue, returning the original value.
This function uses \e acquire \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access following the atomic operation (in program order) may not
be re-ordered before the atomic operation.
*/
/*! \fn int QAtomicInt::fetchAndStoreRelease(int newValue)
Atomic fetch-and-store.
Reads the current value of this QAtomicInt and then assigns it the
\a newValue, returning the original value.
This function uses \e release \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before the atomic operation (in program order) may not be
re-ordered after the atomic operation.
*/
/*! \fn int QAtomicInt::fetchAndStoreOrdered(int newValue)
Atomic fetch-and-store.
Reads the current value of this QAtomicInt and then assigns it the
\a newValue, returning the original value.
This function uses \e ordered \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before and after the atomic operation (in program order)
may not be re-ordered.
*/
/*! \fn bool QAtomicInt::isFetchAndAddNative()
Returns true if fetch-and-add is implemented using atomic
processor instructions, false otherwise.
*/
/*! \fn bool QAtomicInt::isFetchAndAddWaitFree()
Returns true if atomic fetch-and-add is wait-free, false
otherwise.
*/
/*! \fn int QAtomicInt::fetchAndAddRelaxed(int valueToAdd)
Atomic fetch-and-add.
Reads the current value of this QAtomicInt and then adds
\a valueToAdd to the current value, returning the original value.
This function uses \e relaxed \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, leaving the compiler and
processor to freely reorder memory accesses.
*/
/*! \fn int QAtomicInt::fetchAndAddAcquire(int valueToAdd)
Atomic fetch-and-add.
Reads the current value of this QAtomicInt and then adds
\a valueToAdd to the current value, returning the original value.
This function uses \e acquire \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access following the atomic operation (in program order) may not
be re-ordered before the atomic operation.
*/
/*! \fn int QAtomicInt::fetchAndAddRelease(int valueToAdd)
Atomic fetch-and-add.
Reads the current value of this QAtomicInt and then adds
\a valueToAdd to the current value, returning the original value.
This function uses \e release \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before the atomic operation (in program order) may not be
re-ordered after the atomic operation.
*/
/*! \fn int QAtomicInt::fetchAndAddOrdered(int valueToAdd)
Atomic fetch-and-add.
Reads the current value of this QAtomicInt and then adds
\a valueToAdd to the current value, returning the original value.
This function uses \e ordered \l {QAtomicInt#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before and after the atomic operation (in program order)
may not be re-ordered.
*/
/*!
\macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
\relates QAtomicInt
This macro is defined if and only if all generations of your
processor support atomic reference counting.
*/
/*!
\macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE
\relates QAtomicInt
This macro is defined when only certain generations of the
processor support atomic reference counting. Use the
QAtomicInt::isReferenceCountingNative() function to check what
your processor supports.
*/
/*!
\macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE
\relates QAtomicInt
This macro is defined when the hardware does not support atomic
reference counting.
*/
/*!
\macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
\relates QAtomicInt
This macro is defined together with
Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE to indicate that
the reference counting is wait-free.
*/
/*!
\macro Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
\relates QAtomicInt
This macro is defined if and only if your processor supports
atomic test-and-set on integers.
*/
/*!
\macro Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE
\relates QAtomicInt
This macro is defined when only certain generations of the
processor support atomic test-and-set on integers. Use the
QAtomicInt::isTestAndSetNative() function to check what your
processor supports.
*/
/*!
\macro Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE
\relates QAtomicInt
This macro is defined when the hardware does not support atomic
test-and-set on integers.
*/
/*!
\macro Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
\relates QAtomicInt
This macro is defined together with
Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE to indicate that the
atomic test-and-set on integers is wait-free.
*/
/*!
\macro Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
\relates QAtomicInt
This macro is defined if and only if your processor supports
atomic fetch-and-store on integers.
*/
/*!
\macro Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
\relates QAtomicInt
This macro is defined when only certain generations of the
processor support atomic fetch-and-store on integers. Use the
QAtomicInt::isFetchAndStoreNative() function to check what your
processor supports.
*/
/*!
\macro Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE
\relates QAtomicInt
This macro is defined when the hardware does not support atomic
fetch-and-store on integers.
*/
/*!
\macro Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
\relates QAtomicInt
This macro is defined together with
Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE to indicate that the
atomic fetch-and-store on integers is wait-free.
*/
/*!
\macro Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
\relates QAtomicInt
This macro is defined if and only if your processor supports
atomic fetch-and-add on integers.
*/
/*!
\macro Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
\relates QAtomicInt
This macro is defined when only certain generations of the
processor support atomic fetch-and-add on integers. Use the
QAtomicInt::isFetchAndAddNative() function to check what your
processor supports.
*/
/*!
\macro Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE
\relates QAtomicInt
This macro is defined when the hardware does not support atomic
fetch-and-add on integers.
*/
/*!
\macro Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
\relates QAtomicInt
This macro is defined together with
Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE to indicate that the
atomic fetch-and-add on integers is wait-free.
*/
/*!
\class QAtomicPointer
\brief The QAtomicPointer class is a template class that provides platform-independent atomic operations on pointers.
\since 4.4
\ingroup thread
For atomic operations on integers, see the QAtomicInt class.
An complex operation that completes without interruption is said
to be \e atomic. The QAtomicPointer class provides atomic
test-and-set, fetch-and-store, and fetch-and-add for pointers.
\section1 Non-atomic convenience operators
For convenience, QAtomicPointer provides pointer comparison, cast,
dereference, and assignment operators. Note that these operators
are \e not atomic.
\section1 The Atomic API
\section2 Memory ordering
QAtomicPointer provides several implementations of the atomic
test-and-set, fetch-and-store, and fetch-and-add functions. Each
implementation defines a memory ordering semantic that describes
how memory accesses surrounding the atomic instruction are
executed by the processor. Since many modern architectures allow
out-of-order execution and memory ordering, using the correct
semantic is necessary to ensure that your application functions
properly on all processors.
\list
\o Relaxed - memory ordering is unspecified, leaving the compiler
and processor to freely reorder memory accesses.
\o Acquire - memory access following the atomic operation (in
program order) may not be re-ordered before the atomic operation.
\o Release - memory access before the atomic operation (in program
order) may not be re-ordered after the atomic operation.
\o Ordered - the same Acquire and Release semantics combined.
\endlist
\section2 Test-and-set
If the current value of the QAtomicPointer is an expected value,
the test-and-set functions assign a new value to the
QAtomicPointer and return true. If values are \a not the same,
these functions do nothing and return false. This operation
equates to the following code:
\snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 4
There are 4 test-and-set functions: testAndSetRelaxed(),
testAndSetAcquire(), testAndSetRelease(), and
testAndSetOrdered(). See above for an explanation of the different
memory ordering semantics.
\section2 Fetch-and-store
The atomic fetch-and-store functions read the current value of the
QAtomicPointer and then assign a new value, returning the original
value. This operation equates to the following code:
\snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 5
There are 4 fetch-and-store functions: fetchAndStoreRelaxed(),
fetchAndStoreAcquire(), fetchAndStoreRelease(), and
fetchAndStoreOrdered(). See above for an explanation of the
different memory ordering semantics.
\section2 Fetch-and-add
The atomic fetch-and-add functions read the current value of the
QAtomicPointer and then add the given value to the current value,
returning the original value. This operation equates to the
following code:
\snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 6
There are 4 fetch-and-add functions: fetchAndAddRelaxed(),
fetchAndAddAcquire(), fetchAndAddRelease(), and
fetchAndAddOrdered(). See above for an explanation of the
different memory ordering semantics.
\section1 Feature Tests for the Atomic API
Providing a platform-independent atomic API that works on all
processors is challenging. The API provided by QAtomicPointer is
guaranteed to work atomically on all processors. However, since
not all processors implement support for every operation provided
by QAtomicPointer, it is necessary to expose information about the
processor.
You can check at compile time which features are supported on your
hardware using various macros. These will tell you if your
hardware always, sometimes, or does not support a particular
operation. The macros have the form
Q_ATOMIC_POINTER_\e{OPERATION}_IS_\e{HOW}_NATIVE. \e{OPERATION} is
one of TEST_AND_SET, FETCH_AND_STORE, or FETCH_AND_ADD, and
\e{HOW} is one of ALWAYS, SOMETIMES, or NOT. There will always be
exactly one defined macro per operation. For example, if
Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE is defined, neither
Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE nor
Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE will be defined.
An operation that completes in constant time is said to be
wait-free. Such operations are not implemented using locks or
loops of any kind. For atomic operations that are always
supported, and that are wait-free, Qt defines the
Q_ATOMIC_POINTER_\e{OPERATION}_IS_WAIT_FREE in addition to the
Q_ATOMIC_POINTER_\e{OPERATION}_IS_ALWAYS_NATIVE.
In cases where an atomic operation is only supported in newer
generations of the processor, QAtomicPointer also provides a way
to check at runtime what your hardware supports with the
isTestAndSetNative(), isFetchAndStoreNative(), and
isFetchAndAddNative() functions. Wait-free implementations can be
detected using the isTestAndSetWaitFree(),
isFetchAndStoreWaitFree(), and isFetchAndAddWaitFree() functions.
Below is a complete list of all feature macros for QAtomicPointer:
\list
\o Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
\o Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE
\o Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE
\o Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
\o Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
\o Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
\o Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_NOT_NATIVE
\o Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
\o Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
\o Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
\o Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_NOT_NATIVE
\o Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
\endlist
\sa QAtomicInt
*/
/*! \fn QAtomicPointer::QAtomicPointer(T *value)
Constructs a QAtomicPointer with the given \a value.
*/
/*! \fn QAtomicPointer::QAtomicPointer(const QAtomicPointer<T> &other)
Constructs a copy of \a other.
*/
/*! \fn QAtomicPointer<T> &QAtomicPointer::operator=(T *value)
Assigns the \a value to this QAtomicPointer and returns a
reference to this QAtomicPointer.
*/
/*! \fn QAtomicPointer<T> &QAtomicPointer::operator=(const QAtomicPointer<T> &other)
Assigns \a other to this QAtomicPointer and returns a reference to
this QAtomicPointer.
*/
/*! \fn bool QAtomicPointer::operator==(T *value) const
Returns true if the \a value is equal to the value in this
QAtomicPointer; otherwise returns false.
*/
/*! \fn bool QAtomicPointer::operator!=(T *value) const
Returns true if the value of this QAtomicPointer is not equal to
\a value; otherwise returns false.
*/
/*! \fn bool QAtomicPointer::operator!() const
Returns true is the current value of this QAtomicPointer is zero;
otherwise returns false.
*/
/*! \fn QAtomicPointer::operator T *() const
Returns the current pointer value stored by this QAtomicPointer
object.
*/
/*! \fn T *QAtomicPointer::operator->() const
*/
/*! \fn bool QAtomicPointer::isTestAndSetNative()
Returns true if test-and-set is implemented using atomic processor
instructions, false otherwise.
*/
/*! \fn bool QAtomicPointer::isTestAndSetWaitFree()
Returns true if atomic test-and-set is wait-free, false otherwise.
*/
/*! \fn bool QAtomicPointer::testAndSetRelaxed(T *expectedValue, T *newValue)
Atomic test-and-set.
If the current value of this QAtomicPointer is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicPointer and return true. If the values are \e not the same,
this function does nothing and returns false.
This function uses \e relaxed \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, leaving the compiler and
processor to freely reorder memory accesses.
*/
/*! \fn bool QAtomicPointer::testAndSetAcquire(T *expectedValue, T *newValue)
Atomic test-and-set.
If the current value of this QAtomicPointer is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicPointer and return true. If the values are \e not the same,
this function does nothing and returns false.
This function uses \e acquire \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, which ensures that memory
access following the atomic operation (in program order) may not
be re-ordered before the atomic operation.
*/
/*! \fn bool QAtomicPointer::testAndSetRelease(T *expectedValue, T *newValue)
Atomic test-and-set.
If the current value of this QAtomicPointer is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicPointer and return true. If the values are \e not the same,
this function does nothing and returns false.
This function uses \e release \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before the atomic operation (in program order) may not be
re-ordered after the atomic operation.
*/
/*! \fn bool QAtomicPointer::testAndSetOrdered(T *expectedValue, T *newValue)
Atomic test-and-set.
If the current value of this QAtomicPointer is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicPointer and return true. If the values are \e not the same,
this function does nothing and returns false.
This function uses \e ordered \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before and after the atomic operation (in program order)
may not be re-ordered.
*/
/*! \fn bool QAtomicPointer::isFetchAndStoreNative()
Returns true if fetch-and-store is implemented using atomic
processor instructions, false otherwise.
*/
/*! \fn bool QAtomicPointer::isFetchAndStoreWaitFree()
Returns true if atomic fetch-and-store is wait-free, false
otherwise.
*/
/*! \fn T *QAtomicPointer::fetchAndStoreRelaxed(T *newValue)
Atomic fetch-and-store.
Reads the current value of this QAtomicPointer and then assigns it the
\a newValue, returning the original value.
This function uses \e relaxed \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, leaving the compiler and
processor to freely reorder memory accesses.
*/
/*! \fn T *QAtomicPointer::fetchAndStoreAcquire(T *newValue)
Atomic fetch-and-store.
Reads the current value of this QAtomicPointer and then assigns it the
\a newValue, returning the original value.
This function uses \e acquire \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, which ensures that memory
access following the atomic operation (in program order) may not
be re-ordered before the atomic operation.
*/
/*! \fn T *QAtomicPointer::fetchAndStoreRelease(T *newValue)
Atomic fetch-and-store.
Reads the current value of this QAtomicPointer and then assigns it the
\a newValue, returning the original value.
This function uses \e release \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before the atomic operation (in program order) may not be
re-ordered after the atomic operation.
*/
/*! \fn T *QAtomicPointer::fetchAndStoreOrdered(T *newValue)
Atomic fetch-and-store.
Reads the current value of this QAtomicPointer and then assigns it the
\a newValue, returning the original value.
This function uses \e ordered \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before and after the atomic operation (in program order)
may not be re-ordered.
*/
/*! \fn bool QAtomicPointer::isFetchAndAddNative()
Returns true if fetch-and-add is implemented using atomic
processor instructions, false otherwise.
*/
/*! \fn bool QAtomicPointer::isFetchAndAddWaitFree()
Returns true if atomic fetch-and-add is wait-free, false
otherwise.
*/
/*! \fn T *QAtomicPointer::fetchAndAddRelaxed(qptrdiff valueToAdd)
Atomic fetch-and-add.
Reads the current value of this QAtomicPointer and then adds
\a valueToAdd to the current value, returning the original value.
This function uses \e relaxed \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, leaving the compiler and
processor to freely reorder memory accesses.
*/
/*! \fn T *QAtomicPointer::fetchAndAddAcquire(qptrdiff valueToAdd)
Atomic fetch-and-add.
Reads the current value of this QAtomicPointer and then adds
\a valueToAdd to the current value, returning the original value.
This function uses \e acquire \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, which ensures that memory
access following the atomic operation (in program order) may not
be re-ordered before the atomic operation.
*/
/*! \fn T *QAtomicPointer::fetchAndAddRelease(qptrdiff valueToAdd)
Atomic fetch-and-add.
Reads the current value of this QAtomicPointer and then adds
\a valueToAdd to the current value, returning the original value.
This function uses \e release \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before the atomic operation (in program order) may not be
re-ordered after the atomic operation.
*/
/*! \fn T *QAtomicPointer::fetchAndAddOrdered(qptrdiff valueToAdd)
Atomic fetch-and-add.
Reads the current value of this QAtomicPointer and then adds
\a valueToAdd to the current value, returning the original value.
This function uses \e ordered \l {QAtomicPointer#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before and after the atomic operation (in program order)
may not be re-ordered.
*/
/*!
\macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
\relates QAtomicPointer
This macro is defined if and only if your processor supports
atomic test-and-set on pointers.
*/
/*!
\macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE
\relates QAtomicPointer
This macro is defined when only certain generations of the
processor support atomic test-and-set on pointers. Use the
QAtomicPointer::isTestAndSetNative() function to check what your
processor supports.
*/
/*!
\macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE
\relates QAtomicPointer
This macro is defined when the hardware does not support atomic
test-and-set on pointers.
*/
/*!
\macro Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
\relates QAtomicPointer
This macro is defined together with
Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE to indicate that
the atomic test-and-set on pointers is wait-free.
*/
/*!
\macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
\relates QAtomicPointer
This macro is defined if and only if your processor supports
atomic fetch-and-store on pointers.
*/
/*!
\macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
\relates QAtomicPointer
This macro is defined when only certain generations of the
processor support atomic fetch-and-store on pointers. Use the
QAtomicPointer::isFetchAndStoreNative() function to check what
your processor supports.
*/
/*!
\macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_NOT_NATIVE
\relates QAtomicPointer
This macro is defined when the hardware does not support atomic
fetch-and-store on pointers.
*/
/*!
\macro Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
\relates QAtomicPointer
This macro is defined together with
Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE to indicate that
the atomic fetch-and-store on pointers is wait-free.
*/
/*!
\macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
\relates QAtomicPointer
This macro is defined if and only if your processor supports
atomic fetch-and-add on pointers.
*/
/*!
\macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
\relates QAtomicPointer
This macro is defined when only certain generations of the
processor support atomic fetch-and-add on pointers. Use the
QAtomicPointer::isFetchAndAddNative() function to check what your
processor supports.
*/
/*!
\macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_NOT_NATIVE
\relates QAtomicPointer
This macro is defined when the hardware does not support atomic
fetch-and-add on pointers.
*/
/*!
\macro Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
\relates QAtomicPointer
This macro is defined together with
Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE to indicate that
the atomic fetch-and-add on pointers is wait-free.
*/
|