-
Notifications
You must be signed in to change notification settings - Fork 771
/
Copy pathatomics.tex
3851 lines (3364 loc) · 142 KB
/
atomics.tex
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
%!TEX root = std.tex
\rSec0[atomics]{Atomic operations library}
\rSec1[atomics.general]{General}
\pnum
This Clause describes components for fine-grained atomic access. This access is
provided via operations on atomic objects.
\pnum
The following subclauses describe atomics requirements and components for types
and operations, as summarized in \tref{atomics.summary}.
\begin{libsumtab}{Atomics library summary}{atomics.summary}
\ref{atomics.alias} & Type aliases & \tcode{<atomic>} \\
\ref{atomics.order} & Order and consistency & \\
\ref{atomics.lockfree} & Lock-free property & \\
\ref{atomics.wait} & Waiting and notifying & \\
\ref{atomics.ref.generic} & Class template \tcode{atomic_ref} & \\
\ref{atomics.types.generic} & Class template \tcode{atomic} & \\
\ref{atomics.nonmembers} & Non-member functions & \\
\ref{atomics.flag} & Flag type and operations & \\
\ref{atomics.fences} & Fences & \\ \rowsep
\ref{stdatomic.h.syn} & C compatibility & \tcode{<stdatomic.h>} \\
\end{libsumtab}
\rSec1[atomics.syn]{Header \tcode{<atomic>} synopsis}
\indexheader{atomic}%
\begin{codeblock}
namespace std {
// \ref{atomics.order}, order and consistency
enum class memory_order : @\unspec@;
template<class T>
T kill_dependency(T y) noexcept;
}
// \ref{atomics.lockfree}, lock-free property
#define ATOMIC_BOOL_LOCK_FREE @\unspec@
#define ATOMIC_CHAR_LOCK_FREE @\unspec@
#define ATOMIC_CHAR8_T_LOCK_FREE @\unspec@
#define ATOMIC_CHAR16_T_LOCK_FREE @\unspec@
#define ATOMIC_CHAR32_T_LOCK_FREE @\unspec@
#define ATOMIC_WCHAR_T_LOCK_FREE @\unspec@
#define ATOMIC_SHORT_LOCK_FREE @\unspec@
#define ATOMIC_INT_LOCK_FREE @\unspec@
#define ATOMIC_LONG_LOCK_FREE @\unspec@
#define ATOMIC_LLONG_LOCK_FREE @\unspec@
#define ATOMIC_POINTER_LOCK_FREE @\unspec@
namespace std {
// \ref{atomics.ref.generic}, class template \tcode{atomic_ref}
template<class T> struct atomic_ref;
// \ref{atomics.ref.pointer}, partial specialization for pointers
template<class T> struct atomic_ref<T*>;
// \ref{atomics.types.generic}, class template \tcode{atomic}
template<class T> struct atomic;
// \ref{atomics.types.pointer}, partial specialization for pointers
template<class T> struct atomic<T*>;
// \ref{atomics.nonmembers}, non-member functions
template<class T>
bool atomic_is_lock_free(const volatile atomic<T>*) noexcept;
template<class T>
bool atomic_is_lock_free(const atomic<T>*) noexcept;
template<class T>
void atomic_store(volatile atomic<T>*, typename atomic<T>::value_type) noexcept;
template<class T>
void atomic_store(atomic<T>*, typename atomic<T>::value_type) noexcept;
template<class T>
void atomic_store_explicit(volatile atomic<T>*, typename atomic<T>::value_type,
memory_order) noexcept;
template<class T>
void atomic_store_explicit(atomic<T>*, typename atomic<T>::value_type,
memory_order) noexcept;
template<class T>
T atomic_load(const volatile atomic<T>*) noexcept;
template<class T>
T atomic_load(const atomic<T>*) noexcept;
template<class T>
T atomic_load_explicit(const volatile atomic<T>*, memory_order) noexcept;
template<class T>
T atomic_load_explicit(const atomic<T>*, memory_order) noexcept;
template<class T>
T atomic_exchange(volatile atomic<T>*, typename atomic<T>::value_type) noexcept;
template<class T>
T atomic_exchange(atomic<T>*, typename atomic<T>::value_type) noexcept;
template<class T>
T atomic_exchange_explicit(volatile atomic<T>*, typename atomic<T>::value_type,
memory_order) noexcept;
template<class T>
T atomic_exchange_explicit(atomic<T>*, typename atomic<T>::value_type,
memory_order) noexcept;
template<class T>
bool atomic_compare_exchange_weak(volatile atomic<T>*,
typename atomic<T>::value_type*,
typename atomic<T>::value_type) noexcept;
template<class T>
bool atomic_compare_exchange_weak(atomic<T>*,
typename atomic<T>::value_type*,
typename atomic<T>::value_type) noexcept;
template<class T>
bool atomic_compare_exchange_strong(volatile atomic<T>*,
typename atomic<T>::value_type*,
typename atomic<T>::value_type) noexcept;
template<class T>
bool atomic_compare_exchange_strong(atomic<T>*,
typename atomic<T>::value_type*,
typename atomic<T>::value_type) noexcept;
template<class T>
bool atomic_compare_exchange_weak_explicit(volatile atomic<T>*,
typename atomic<T>::value_type*,
typename atomic<T>::value_type,
memory_order, memory_order) noexcept;
template<class T>
bool atomic_compare_exchange_weak_explicit(atomic<T>*,
typename atomic<T>::value_type*,
typename atomic<T>::value_type,
memory_order, memory_order) noexcept;
template<class T>
bool atomic_compare_exchange_strong_explicit(volatile atomic<T>*,
typename atomic<T>::value_type*,
typename atomic<T>::value_type,
memory_order, memory_order) noexcept;
template<class T>
bool atomic_compare_exchange_strong_explicit(atomic<T>*,
typename atomic<T>::value_type*,
typename atomic<T>::value_type,
memory_order, memory_order) noexcept;
template<class T>
T atomic_fetch_add(volatile atomic<T>*, typename atomic<T>::difference_type) noexcept;
template<class T>
T atomic_fetch_add(atomic<T>*, typename atomic<T>::difference_type) noexcept;
template<class T>
T atomic_fetch_add_explicit(volatile atomic<T>*, typename atomic<T>::difference_type,
memory_order) noexcept;
template<class T>
T atomic_fetch_add_explicit(atomic<T>*, typename atomic<T>::difference_type,
memory_order) noexcept;
template<class T>
T atomic_fetch_sub(volatile atomic<T>*, typename atomic<T>::difference_type) noexcept;
template<class T>
T atomic_fetch_sub(atomic<T>*, typename atomic<T>::difference_type) noexcept;
template<class T>
T atomic_fetch_sub_explicit(volatile atomic<T>*, typename atomic<T>::difference_type,
memory_order) noexcept;
template<class T>
T atomic_fetch_sub_explicit(atomic<T>*, typename atomic<T>::difference_type,
memory_order) noexcept;
template<class T>
T atomic_fetch_and(volatile atomic<T>*, typename atomic<T>::value_type) noexcept;
template<class T>
T atomic_fetch_and(atomic<T>*, typename atomic<T>::value_type) noexcept;
template<class T>
T atomic_fetch_and_explicit(volatile atomic<T>*, typename atomic<T>::value_type,
memory_order) noexcept;
template<class T>
T atomic_fetch_and_explicit(atomic<T>*, typename atomic<T>::value_type,
memory_order) noexcept;
template<class T>
T atomic_fetch_or(volatile atomic<T>*, typename atomic<T>::value_type) noexcept;
template<class T>
T atomic_fetch_or(atomic<T>*, typename atomic<T>::value_type) noexcept;
template<class T>
T atomic_fetch_or_explicit(volatile atomic<T>*, typename atomic<T>::value_type,
memory_order) noexcept;
template<class T>
T atomic_fetch_or_explicit(atomic<T>*, typename atomic<T>::value_type,
memory_order) noexcept;
template<class T>
T atomic_fetch_xor(volatile atomic<T>*, typename atomic<T>::value_type) noexcept;
template<class T>
T atomic_fetch_xor(atomic<T>*, typename atomic<T>::value_type) noexcept;
template<class T>
T atomic_fetch_xor_explicit(volatile atomic<T>*, typename atomic<T>::value_type,
memory_order) noexcept;
template<class T>
T atomic_fetch_xor_explicit(atomic<T>*, typename atomic<T>::value_type,
memory_order) noexcept;
template<class T>
void atomic_wait(const volatile atomic<T>*, typename atomic<T>::value_type);
template<class T>
void atomic_wait(const atomic<T>*, typename atomic<T>::value_type);
template<class T>
void atomic_wait_explicit(const volatile atomic<T>*, typename atomic<T>::value_type,
memory_order);
template<class T>
void atomic_wait_explicit(const atomic<T>*, typename atomic<T>::value_type,
memory_order);
template<class T>
void atomic_notify_one(volatile atomic<T>*);
template<class T>
void atomic_notify_one(atomic<T>*);
template<class T>
void atomic_notify_all(volatile atomic<T>*);
template<class T>
void atomic_notify_all(atomic<T>*);
// \ref{atomics.alias}, type aliases
using atomic_bool = atomic<bool>;
using atomic_char = atomic<char>;
using atomic_schar = atomic<signed char>;
using atomic_uchar = atomic<unsigned char>;
using atomic_short = atomic<short>;
using atomic_ushort = atomic<unsigned short>;
using atomic_int = atomic<int>;
using atomic_uint = atomic<unsigned int>;
using atomic_long = atomic<long>;
using atomic_ulong = atomic<unsigned long>;
using atomic_llong = atomic<long long>;
using atomic_ullong = atomic<unsigned long long>;
using atomic_char8_t = atomic<char8_t>;
using atomic_char16_t = atomic<char16_t>;
using atomic_char32_t = atomic<char32_t>;
using atomic_wchar_t = atomic<wchar_t>;
using atomic_int8_t = atomic<int8_t>;
using atomic_uint8_t = atomic<uint8_t>;
using atomic_int16_t = atomic<int16_t>;
using atomic_uint16_t = atomic<uint16_t>;
using atomic_int32_t = atomic<int32_t>;
using atomic_uint32_t = atomic<uint32_t>;
using atomic_int64_t = atomic<int64_t>;
using atomic_uint64_t = atomic<uint64_t>;
using atomic_int_least8_t = atomic<int_least8_t>;
using atomic_uint_least8_t = atomic<uint_least8_t>;
using atomic_int_least16_t = atomic<int_least16_t>;
using atomic_uint_least16_t = atomic<uint_least16_t>;
using atomic_int_least32_t = atomic<int_least32_t>;
using atomic_uint_least32_t = atomic<uint_least32_t>;
using atomic_int_least64_t = atomic<int_least64_t>;
using atomic_uint_least64_t = atomic<uint_least64_t>;
using atomic_int_fast8_t = atomic<int_fast8_t>;
using atomic_uint_fast8_t = atomic<uint_fast8_t>;
using atomic_int_fast16_t = atomic<int_fast16_t>;
using atomic_uint_fast16_t = atomic<uint_fast16_t>;
using atomic_int_fast32_t = atomic<int_fast32_t>;
using atomic_uint_fast32_t = atomic<uint_fast32_t>;
using atomic_int_fast64_t = atomic<int_fast64_t>;
using atomic_uint_fast64_t = atomic<uint_fast64_t>;
using atomic_intptr_t = atomic<intptr_t>;
using atomic_uintptr_t = atomic<uintptr_t>;
using atomic_size_t = atomic<size_t>;
using atomic_ptrdiff_t = atomic<ptrdiff_t>;
using atomic_intmax_t = atomic<intmax_t>;
using atomic_uintmax_t = atomic<uintmax_t>;
using atomic_signed_lock_free = @\seebelow@;
using atomic_unsigned_lock_free = @\seebelow@;
// \ref{atomics.flag}, flag type and operations
struct atomic_flag;
bool atomic_flag_test(const volatile atomic_flag*) noexcept;
bool atomic_flag_test(const atomic_flag*) noexcept;
bool atomic_flag_test_explicit(const volatile atomic_flag*, memory_order) noexcept;
bool atomic_flag_test_explicit(const atomic_flag*, memory_order) noexcept;
bool atomic_flag_test_and_set(volatile atomic_flag*) noexcept;
bool atomic_flag_test_and_set(atomic_flag*) noexcept;
bool atomic_flag_test_and_set_explicit(volatile atomic_flag*, memory_order) noexcept;
bool atomic_flag_test_and_set_explicit(atomic_flag*, memory_order) noexcept;
void atomic_flag_clear(volatile atomic_flag*) noexcept;
void atomic_flag_clear(atomic_flag*) noexcept;
void atomic_flag_clear_explicit(volatile atomic_flag*, memory_order) noexcept;
void atomic_flag_clear_explicit(atomic_flag*, memory_order) noexcept;
void atomic_flag_wait(const volatile atomic_flag*, bool) noexcept;
void atomic_flag_wait(const atomic_flag*, bool) noexcept;
void atomic_flag_wait_explicit(const volatile atomic_flag*,
bool, memory_order) noexcept;
void atomic_flag_wait_explicit(const atomic_flag*,
bool, memory_order) noexcept;
void atomic_flag_notify_one(volatile atomic_flag*) noexcept;
void atomic_flag_notify_one(atomic_flag*) noexcept;
void atomic_flag_notify_all(volatile atomic_flag*) noexcept;
void atomic_flag_notify_all(atomic_flag*) noexcept;
// \ref{atomics.fences}, fences
extern "C" void atomic_thread_fence(memory_order) noexcept;
extern "C" void atomic_signal_fence(memory_order) noexcept;
}
\end{codeblock}
\rSec1[atomics.alias]{Type aliases}
\indexlibraryglobal{atomic_bool}%
\indexlibraryglobal{atomic_char}%
\indexlibraryglobal{atomic_schar}%
\indexlibraryglobal{atomic_uchar}%
\indexlibraryglobal{atomic_short}%
\indexlibraryglobal{atomic_ushort}%
\indexlibraryglobal{atomic_int}%
\indexlibraryglobal{atomic_uint}%
\indexlibraryglobal{atomic_long}%
\indexlibraryglobal{atomic_ulong}%
\indexlibraryglobal{atomic_llong}%
\indexlibraryglobal{atomic_ullong}%
\indexlibraryglobal{atomic_char8_t}%
\indexlibraryglobal{atomic_char16_t}%
\indexlibraryglobal{atomic_char32_t}%
\indexlibraryglobal{atomic_wchar_t}%
\indexlibraryglobal{atomic_int8_t}%
\indexlibraryglobal{atomic_uint8_t}%
\indexlibraryglobal{atomic_int16_t}%
\indexlibraryglobal{atomic_uint16_t}%
\indexlibraryglobal{atomic_int32_t}%
\indexlibraryglobal{atomic_uint32_t}%
\indexlibraryglobal{atomic_int64_t}%
\indexlibraryglobal{atomic_uint64_t}%
\indexlibraryglobal{atomic_int_least8_t}%
\indexlibraryglobal{atomic_uint_least8_t}%
\indexlibraryglobal{atomic_int_least16_t}%
\indexlibraryglobal{atomic_uint_least16_t}%
\indexlibraryglobal{atomic_int_least32_t}%
\indexlibraryglobal{atomic_uint_least32_t}%
\indexlibraryglobal{atomic_int_least64_t}%
\indexlibraryglobal{atomic_uint_least64_t}%
\indexlibraryglobal{atomic_int_fast8_t}%
\indexlibraryglobal{atomic_uint_fast8_t}%
\indexlibraryglobal{atomic_int_fast16_t}%
\indexlibraryglobal{atomic_uint_fast16_t}%
\indexlibraryglobal{atomic_int_fast32_t}%
\indexlibraryglobal{atomic_uint_fast32_t}%
\indexlibraryglobal{atomic_int_fast64_t}%
\indexlibraryglobal{atomic_uint_fast64_t}%
\indexlibraryglobal{atomic_intptr_t}%
\indexlibraryglobal{atomic_uintptr_t}%
\indexlibraryglobal{atomic_size_t}%
\indexlibraryglobal{atomic_ptrdiff_t}%
\indexlibraryglobal{atomic_intmax_t}%
\indexlibraryglobal{atomic_uintmax_t}%
\pnum
The type aliases \tcode{atomic_int$N$_t}, \tcode{atomic_uint$N$_t},
\tcode{atomic_intptr_t}, and \tcode{atomic_uintptr_t}
are defined if and only if
\tcode{int$N$_t}, \tcode{uint$N$_t},
\tcode{intptr_t}, and \tcode{uintptr_t}
are defined, respectively.
\pnum
\indexlibraryglobal{atomic_signed_lock_free}%
\indexlibraryglobal{atomic_unsigned_lock_free}%
The type aliases
\tcode{atomic_signed_lock_free} and \tcode{atomic_unsigned_lock_free}
name specializations of \tcode{atomic}
whose template arguments are integral types, respectively signed and unsigned,
and whose \tcode{is_always_lock_free} property is \tcode{true}.
\begin{note}
\indextext{implementation!freestanding}%
These aliases are optional in freestanding implementations\iref{compliance}.
\end{note}
Implementations should choose for these aliases
the integral specializations of \tcode{atomic}
for which the atomic waiting and notifying operations\iref{atomics.wait}
are most efficient.
\rSec1[atomics.order]{Order and consistency}
\indexlibraryglobal{memory_order}%
\indexlibrarymember{relaxed}{memory_order}%
\indexlibrarymember{consume}{memory_order}%
\indexlibrarymember{acquire}{memory_order}%
\indexlibrarymember{release}{memory_order}%
\indexlibrarymember{acq_rel}{memory_order}%
\indexlibrarymember{seq_cst}{memory_order}%
\indexlibraryglobal{memory_order_relaxed}%
\indexlibraryglobal{memory_order_consume}%
\indexlibraryglobal{memory_order_acquire}%
\indexlibraryglobal{memory_order_release}%
\indexlibraryglobal{memory_order_acq_rel}%
\indexlibraryglobal{memory_order_seq_cst}%
\begin{codeblock}
namespace std {
enum class memory_order : @\unspec@ {
relaxed, consume, acquire, release, acq_rel, seq_cst
};
inline constexpr memory_order memory_order_relaxed = memory_order::relaxed;
inline constexpr memory_order memory_order_consume = memory_order::consume;
inline constexpr memory_order memory_order_acquire = memory_order::acquire;
inline constexpr memory_order memory_order_release = memory_order::release;
inline constexpr memory_order memory_order_acq_rel = memory_order::acq_rel;
inline constexpr memory_order memory_order_seq_cst = memory_order::seq_cst;
}
\end{codeblock}
\pnum
The enumeration \tcode{memory_order} specifies the detailed regular
(non-atomic) memory synchronization order as defined in
\ref{intro.multithread} and may provide for operation ordering. Its
enumerated values and their meanings are as follows:
\begin{itemize}
\item \tcode{memory_order::relaxed}: no operation orders memory.
\item \tcode{memory_order::release}, \tcode{memory_order::acq_rel}, and
\tcode{memory_order::seq_cst}: a store operation performs a release operation on the
affected memory location.
\item \tcode{memory_order::consume}: a load operation performs a consume operation on the
affected memory location.
\begin{note}
Prefer \tcode{memory_order::acquire}, which provides stronger guarantees
than \tcode{memory_order::consume}. Implementations have found it infeasible
to provide performance better than that of \tcode{memory_order::acquire}.
Specification revisions are under consideration.
\end{note}
\item \tcode{memory_order::acquire}, \tcode{memory_order::acq_rel}, and
\tcode{memory_order::seq_cst}: a load operation performs an acquire operation on the
affected memory location.
\end{itemize}
\begin{note}
Atomic operations specifying \tcode{memory_order::relaxed} are relaxed
with respect to memory ordering. Implementations must still guarantee that any
given atomic access to a particular atomic object be indivisible with respect
to all other atomic accesses to that object.
\end{note}
\pnum
An atomic operation $A$ that performs a release operation on an atomic
object $M$ synchronizes with an atomic operation $B$ that performs
an acquire operation on $M$ and takes its value from any side effect in the
release sequence headed by $A$.
\pnum
An atomic operation $A$ on some atomic object $M$ is
\defn{coherence-ordered before}
another atomic operation $B$ on $M$ if
\begin{itemize}
\item $A$ is a modification, and
$B$ reads the value stored by $A$, or
\item $A$ precedes $B$
in the modification order of $M$, or
\item $A$ and $B$ are not
the same atomic read-modify-write operation, and
there exists an atomic modification $X$ of $M$
such that $A$ reads the value stored by $X$ and
$X$ precedes $B$
in the modification order of $M$, or
\item there exists an atomic modification $X$ of $M$
such that $A$ is coherence-ordered before $X$ and
$X$ is coherence-ordered before $B$.
\end{itemize}
\pnum
There is a single total order $S$
on all \tcode{memory_order::seq_cst} operations, including fences,
that satisfies the following constraints.
First, if $A$ and $B$ are
\tcode{memory_order::seq_cst} operations and
$A$ strongly happens before $B$,
then $A$ precedes $B$ in $S$.
Second, for every pair of atomic operations $A$ and
$B$ on an object $M$,
where $A$ is coherence-ordered before $B$,
the following four conditions are required to be satisfied by $S$:
\begin{itemize}
\item if $A$ and $B$ are both
\tcode{memory_order::seq_cst} operations,
then $A$ precedes $B$ in $S$; and
\item if $A$ is a \tcode{memory_order::seq_cst} operation and
$B$ happens before
a \tcode{memory_order::seq_cst} fence $Y$,
then $A$ precedes $Y$ in $S$; and
\item if a \tcode{memory_order::seq_cst} fence $X$
happens before $A$ and
$B$ is a \tcode{memory_order::seq_cst} operation,
then $X$ precedes $B$ in $S$; and
\item if a \tcode{memory_order::seq_cst} fence $X$
happens before $A$ and
$B$ happens before
a \tcode{memory_order::seq_cst} fence $Y$,
then $X$ precedes $Y$ in $S$.
\end{itemize}
\pnum
\begin{note}
This definition ensures that $S$ is consistent with
the modification order of any atomic object $M$.
It also ensures that
a \tcode{memory_order::seq_cst} load $A$ of $M$
gets its value either from the last modification of $M$
that precedes $A$ in $S$ or
from some non-\tcode{memory_order::seq_cst} modification of $M$
that does not happen before any modification of $M$
that precedes $A$ in $S$.
\end{note}
\pnum
\begin{note}
We do not require that $S$ be consistent with
``happens before''\iref{intro.races}.
This allows more efficient implementation
of \tcode{memory_order::acquire} and \tcode{memory_order::release}
on some machine architectures.
It can produce surprising results
when these are mixed with \tcode{memory_order::seq_cst} accesses.
\end{note}
\pnum
\begin{note}
\tcode{memory_order::seq_cst} ensures sequential consistency only
for a program that is free of data races and
uses exclusively \tcode{memory_order::seq_cst} atomic operations.
Any use of weaker ordering will invalidate this guarantee
unless extreme care is used.
In many cases, \tcode{memory_order::seq_cst} atomic operations are reorderable
with respect to other atomic operations performed by the same thread.
\end{note}
\pnum
Implementations should ensure that no ``out-of-thin-air'' values are computed that
circularly depend on their own computation.
\begin{note}
For example, with \tcode{x} and \tcode{y} initially zero,
\begin{codeblock}
// Thread 1:
r1 = y.load(memory_order::relaxed);
x.store(r1, memory_order::relaxed);
\end{codeblock}
\begin{codeblock}
// Thread 2:
r2 = x.load(memory_order::relaxed);
y.store(r2, memory_order::relaxed);
\end{codeblock}
this recommendation discourages producing \tcode{r1 == r2 == 42}, since the store of 42 to \tcode{y} is only
possible if the store to \tcode{x} stores \tcode{42}, which circularly depends on the
store to \tcode{y} storing \tcode{42}. Note that without this restriction, such an
execution is possible.
\end{note}
\pnum
\begin{note}
The recommendation similarly disallows \tcode{r1 == r2 == 42} in the
following example, with \tcode{x} and \tcode{y} again initially zero:
\begin{codeblock}
// Thread 1:
r1 = x.load(memory_order::relaxed);
if (r1 == 42) y.store(42, memory_order::relaxed);
\end{codeblock}
\begin{codeblock}
// Thread 2:
r2 = y.load(memory_order::relaxed);
if (r2 == 42) x.store(42, memory_order::relaxed);
\end{codeblock}
\end{note}
\pnum
Atomic read-modify-write operations shall always read the last value
(in the modification order) written before the write associated with
the read-modify-write operation.
\pnum
Implementations should make atomic stores visible to atomic loads within a reasonable
amount of time.
\indexlibraryglobal{kill_dependency}%
\begin{itemdecl}
template<class T>
T kill_dependency(T y) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
The argument does not carry a dependency to the return
value\iref{intro.multithread}.
\pnum
\returns
\tcode{y}.
\end{itemdescr}
\rSec1[atomics.lockfree]{Lock-free property}
\indexlibraryglobal{ATOMIC_BOOL_LOCK_FREE}%
\indexlibraryglobal{ATOMIC_CHAR_LOCK_FREE}%
\indexlibraryglobal{ATOMIC_CHAR8_T_LOCK_FREE}%
\indexlibraryglobal{ATOMIC_CHAR16_T_LOCK_FREE}%
\indexlibraryglobal{ATOMIC_CHAR32_T_LOCK_FREE}%
\indexlibraryglobal{ATOMIC_WCHAR_T_LOCK_FREE}%
\indexlibraryglobal{ATOMIC_SHORT_LOCK_FREE}%
\indexlibraryglobal{ATOMIC_INT_LOCK_FREE}%
\indexlibraryglobal{ATOMIC_LONG_LOCK_FREE}%
\indexlibraryglobal{ATOMIC_LLONG_LOCK_FREE}%
\indexlibraryglobal{ATOMIC_POINTER_LOCK_FREE}%
\indeximpldef{values of various \tcode{ATOMIC_..._LOCK_FREE} macros}
\begin{codeblock}
#define ATOMIC_BOOL_LOCK_FREE @\unspec@
#define ATOMIC_CHAR_LOCK_FREE @\unspec@
#define ATOMIC_CHAR8_T_LOCK_FREE @\unspec@
#define ATOMIC_CHAR16_T_LOCK_FREE @\unspec@
#define ATOMIC_CHAR32_T_LOCK_FREE @\unspec@
#define ATOMIC_WCHAR_T_LOCK_FREE @\unspec@
#define ATOMIC_SHORT_LOCK_FREE @\unspec@
#define ATOMIC_INT_LOCK_FREE @\unspec@
#define ATOMIC_LONG_LOCK_FREE @\unspec@
#define ATOMIC_LLONG_LOCK_FREE @\unspec@
#define ATOMIC_POINTER_LOCK_FREE @\unspec@
\end{codeblock}
\pnum
The \tcode{ATOMIC_..._LOCK_FREE} macros indicate the lock-free property of the
corresponding atomic types, with the signed and unsigned variants grouped
together. The properties also apply to the corresponding (partial) specializations of the
\tcode{atomic} template. A value of 0 indicates that the types are never
lock-free. A value of 1 indicates that the types are sometimes lock-free. A
value of 2 indicates that the types are always lock-free.
\pnum
At least one signed integral specialization of the \tcode{atomic} template,
along with the specialization
for the corresponding unsigned type\iref{basic.fundamental},
is always lock-free.
\begin{note}
\indextext{implementation!freestanding}%
This requirement is optional in freestanding implementations\iref{compliance}.
\end{note}
\pnum
The functions \tcode{atomic<T>::is_lock_free} and
\tcode{atomic_is_lock_free}\iref{atomics.types.operations}
indicate whether the object is lock-free. In any given program execution, the
result of the lock-free query
is the same for all atomic objects of the same type.
\pnum
Atomic operations that are not lock-free are considered to potentially
block\iref{intro.progress}.
\pnum
\recommended
Operations that are lock-free should also be address-free.
\begin{footnote}
That is,
atomic operations on the same memory location via two different addresses will
communicate atomically.
\end{footnote}
The implementation of these operations should not depend on any per-process state.
\begin{note}
This restriction enables communication by memory that is
mapped into a process more than once and by memory that is shared between two
processes.
\end{note}
\rSec1[atomics.wait]{Waiting and notifying}
\pnum
\defnx{Atomic waiting operations}{atomic!waiting operation}
and \defnx{atomic notifying operations}{atomic!notifying operation}
provide a mechanism to wait for the value of an atomic object to change
more efficiently than can be achieved with polling.
An atomic waiting operation may block until it is unblocked
by an atomic notifying operation, according to each function's effects.
\begin{note}
Programs are not guaranteed to observe transient atomic values,
an issue known as the A-B-A problem,
resulting in continued blocking if a condition is only temporarily met.
\end{note}
\pnum
\begin{note}
The following functions are atomic waiting operations:
\begin{itemize}
\item \tcode{atomic<T>::wait},
\item \tcode{atomic_flag::wait},
\item \tcode{atomic_wait} and \tcode{atomic_wait_explicit},
\item \tcode{atomic_flag_wait} and \tcode{atomic_flag_wait_explicit}, and
\item \tcode{atomic_ref<T>::wait}.
\end{itemize}
\end{note}
\pnum
\begin{note}
The following functions are atomic notifying operations:
\begin{itemize}
\item \tcode{atomic<T>::notify_one} and \tcode{atomic<T>::notify_all},
\item \tcode{atomic_flag::notify_one} and \tcode{atomic_flag::notify_all},
\item \tcode{atomic_notify_one} and \tcode{atomic_notify_all},
\item \tcode{atomic_flag_notify_one} and \tcode{atomic_flag_notify_all}, and
\item \tcode{atomic_ref<T>::notify_one} and \tcode{atomic_ref<T>::notify_all}.
\end{itemize}
\end{note}
\indextext{atomic!waiting operation!eligible to be unblocked}%
\pnum
A call to an atomic waiting operation on an atomic object \tcode{M}
is \defn{eligible to be unblocked}
by a call to an atomic notifying operation on \tcode{M}
if there exist side effects \tcode{X} and \tcode{Y} on \tcode{M} such that:
\begin{itemize}
\item the atomic waiting operation has blocked after observing the result of \tcode{X},
\item \tcode{X} precedes \tcode{Y} in the modification order of \tcode{M}, and
\item \tcode{Y} happens before the call to the atomic notifying operation.
\end{itemize}
\rSec1[atomics.ref.generic]{Class template \tcode{atomic_ref}}
\rSec2[atomics.ref.generic.general]{General}
\indexlibraryglobal{atomic_ref}%
\indexlibrarymember{value_type}{atomic_ref}%
\begin{codeblock}
namespace std {
template<class T> struct atomic_ref {
private:
T* ptr; // \expos
public:
using value_type = T;
static constexpr size_t required_alignment = @\impdefx{required alignment for \tcode{atomic_ref} type's operations}@;
static constexpr bool is_always_lock_free = @\impdefx{whether a given \tcode{atomic_ref} type's operations are always lock free}@;
bool is_lock_free() const noexcept;
explicit atomic_ref(T&);
atomic_ref(const atomic_ref&) noexcept;
atomic_ref& operator=(const atomic_ref&) = delete;
void store(T, memory_order = memory_order::seq_cst) const noexcept;
T operator=(T) const noexcept;
T load(memory_order = memory_order::seq_cst) const noexcept;
operator T() const noexcept;
T exchange(T, memory_order = memory_order::seq_cst) const noexcept;
bool compare_exchange_weak(T&, T,
memory_order, memory_order) const noexcept;
bool compare_exchange_strong(T&, T,
memory_order, memory_order) const noexcept;
bool compare_exchange_weak(T&, T,
memory_order = memory_order::seq_cst) const noexcept;
bool compare_exchange_strong(T&, T,
memory_order = memory_order::seq_cst) const noexcept;
void wait(T, memory_order = memory_order::seq_cst) const noexcept;
void notify_one() const noexcept;
void notify_all() const noexcept;
};
}
\end{codeblock}
\pnum
An \tcode{atomic_ref} object applies atomic operations\iref{atomics.general} to
the object referenced by \tcode{*ptr} such that,
for the lifetime\iref{basic.life} of the \tcode{atomic_ref} object,
the object referenced by \tcode{*ptr} is an atomic object\iref{intro.races}.
\pnum
The program is ill-formed if \tcode{is_trivially_copyable_v<T>} is \tcode{false}.
\pnum
The lifetime\iref{basic.life} of an object referenced by \tcode{*ptr}
shall exceed the lifetime of all \tcode{atomic_ref}s that reference the object.
While any \tcode{atomic_ref} instances exist
that reference the \tcode{*ptr} object,
all accesses to that object shall exclusively occur
through those \tcode{atomic_ref} instances.
No subobject of the object referenced by \tcode{atomic_ref}
shall be concurrently referenced by any other \tcode{atomic_ref} object.
\pnum
Atomic operations applied to an object
through a referencing \tcode{atomic_ref} are atomic with respect to
atomic operations applied through any other \tcode{atomic_ref}
referencing the same object.
\begin{note}
Atomic operations or the \tcode{atomic_ref} constructor can acquire
a shared resource, such as a lock associated with the referenced object,
to enable atomic operations to be applied to the referenced object.
\end{note}
\rSec2[atomics.ref.ops]{Operations}
\indexlibrarymember{required_alignment}{atomic_ref}%
\indexlibrarymember{required_alignment}{atomic_ref<T*>}%
\indexlibrarymember{required_alignment}{atomic_ref<\placeholder{integral}>}%
\indexlibrarymember{required_alignment}{atomic_ref<\placeholder{floating-point}>}%
\begin{itemdecl}
static constexpr size_t required_alignment;
\end{itemdecl}
\begin{itemdescr}
\pnum
The alignment required for an object to be referenced by an atomic reference,
which is at least \tcode{alignof(T)}.
\pnum
\begin{note}
Hardware could require an object
referenced by an \tcode{atomic_ref}
to have stricter alignment\iref{basic.align}
than other objects of type \tcode{T}.
Further, whether operations on an \tcode{atomic_ref}
are lock-free could depend on the alignment of the referenced object.
For example, lock-free operations on \tcode{std::complex<double>}
could be supported only if aligned to \tcode{2*alignof(double)}.
\end{note}
\end{itemdescr}
\indexlibrarymember{is_always_lock_free}{atomic_ref}%
\indexlibrarymember{is_always_lock_free}{atomic_ref<T*>}%
\indexlibrarymember{is_always_lock_free}{atomic_ref<\placeholder{integral}>}%
\indexlibrarymember{is_always_lock_free}{atomic_ref<\placeholder{floating-point}>}%
\begin{itemdecl}
static constexpr bool is_always_lock_free;
\end{itemdecl}
\begin{itemdescr}
\pnum
The static data member \tcode{is_always_lock_free} is \tcode{true}
if the \tcode{atomic_ref} type's operations are always lock-free,
and \tcode{false} otherwise.
\end{itemdescr}
\indexlibrarymember{is_lock_free}{atomic_ref}%
\indexlibrarymember{is_lock_free}{atomic_ref<T*>}%
\indexlibrarymember{is_lock_free}{atomic_ref<\placeholder{integral}>}%
\indexlibrarymember{is_lock_free}{atomic_ref<\placeholder{floating-point}>}%
\begin{itemdecl}
bool is_lock_free() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{true} if operations on all objects of the type \tcode{atomic_ref<T>}
are lock-free,
\tcode{false} otherwise.
\end{itemdescr}
\indexlibraryctor{atomic_ref}%
\indexlibraryctor{atomic_ref<T*>}%
\indexlibrary{\idxcode{atomic_ref<\placeholder{integral}>}!constructor}%
\indexlibrary{\idxcode{atomic_ref<\placeholder{floating-point}>}!constructor}%
\begin{itemdecl}
atomic_ref(T& obj);
\end{itemdecl}
\begin{itemdescr}
\pnum
\expects
The referenced object is aligned to \tcode{required_alignment}.
\pnum
\ensures
\tcode{*this} references \tcode{obj}.
\pnum
\throws
Nothing.
\end{itemdescr}
\indexlibraryctor{atomic_ref}%
\indexlibraryctor{atomic_ref<T*>}%
\indexlibrary{\idxcode{atomic_ref<\placeholder{integral}>}!constructor}%
\indexlibrary{\idxcode{atomic_ref<\placeholder{floating-point}>}!constructor}%
\begin{itemdecl}
atomic_ref(const atomic_ref& ref) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\ensures
\tcode{*this} references the object referenced by \tcode{ref}.
\end{itemdescr}
\indexlibrarymember{store}{atomic_ref}%
\indexlibrarymember{store}{atomic_ref<T*>}%
\indexlibrarymember{store}{atomic_ref<\placeholder{integral}>}%
\indexlibrarymember{store}{atomic_ref<\placeholder{floating-point}>}%
\begin{itemdecl}
void store(T desired, memory_order order = memory_order::seq_cst) const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\expects
The \tcode{order} argument is neither
\tcode{memory_order::consume},
\tcode{memory_order::acquire}, nor
\tcode{memory_order::acq_rel}.
\pnum
\effects
Atomically replaces the value referenced by \tcode{*ptr}
with the value of \tcode{desired}.
Memory is affected according to the value of \tcode{order}.
\end{itemdescr}
\indexlibrarymember{operator=}{atomic_ref}%
\indexlibrarymember{operator=}{atomic_ref<T*>}%
\indexlibrarymember{operator=}{atomic_ref<\placeholder{integral}>}%
\indexlibrarymember{operator=}{atomic_ref<\placeholder{floating-point}>}%
\begin{itemdecl}
T operator=(T desired) const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Equivalent to:
\begin{codeblock}
store(desired);
return desired;
\end{codeblock}
\end{itemdescr}
\indexlibrarymember{load}{atomic_ref}%
\indexlibrarymember{load}{atomic_ref<T*>}%
\indexlibrarymember{load}{atomic_ref<\placeholder{integral}>}%
\indexlibrarymember{load}{atomic_ref<\placeholder{floating-point}>}%
\begin{itemdecl}
T load(memory_order order = memory_order::seq_cst) const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\expects
The \tcode{order} argument is neither
\tcode{memory_order::release} nor \tcode{memory_order::acq_rel}.
\pnum
\effects
Memory is affected according to the value of \tcode{order}.
\pnum
\returns
Atomically returns the value referenced by \tcode{*ptr}.
\end{itemdescr}
\indexlibrarymember{operator \placeholder{type}}{atomic_ref}%
\indexlibrarymember{operator T*}{atomic_ref<T*>}%
\indexlibrarymember{operator \placeholder{integral}}{atomic_ref<\placeholder{integral}>}%
\indexlibrarymember{operator \placeholder{floating-point}}{atomic_ref<\placeholder{floating-point}>}%
\begin{itemdecl}
operator T() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Equivalent to: \tcode{return load();}
\end{itemdescr}
\indexlibrarymember{exchange}{atomic_ref}%
\indexlibrarymember{exchange}{atomic_ref<T*>}%
\indexlibrarymember{exchange}{atomic_ref<\placeholder{integral}>}%
\indexlibrarymember{exchange}{atomic_ref<\placeholder{floating-point}>}%
\begin{itemdecl}
T exchange(T desired, memory_order order = memory_order::seq_cst) const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Atomically replaces the value referenced by \tcode{*ptr}
with \tcode{desired}.
Memory is affected according to the value of \tcode{order}.
This operation is an atomic read-modify-write operation\iref{intro.multithread}.
\pnum
\returns
Atomically returns the value referenced by \tcode{*ptr}
immediately before the effects.
\end{itemdescr}
\indexlibrarymember{compare_exchange_weak}{atomic_ref}%
\indexlibrarymember{compare_exchange_weak}{atomic_ref<T*>}%
\indexlibrarymember{compare_exchange_weak}{atomic_ref<\placeholder{integral}>}%
\indexlibrarymember{compare_exchange_weak}{atomic_ref<\placeholder{floating-point}>}%
\indexlibrarymember{compare_exchange_strong}{atomic_ref}%
\indexlibrarymember{compare_exchange_strong}{atomic_ref<T*>}%
\indexlibrarymember{compare_exchange_strong}{atomic_ref<\placeholder{integral}>}%
\indexlibrarymember{compare_exchange_strong}{atomic_ref<\placeholder{floating-point}>}%
\begin{itemdecl}
bool compare_exchange_weak(T& expected, T desired,
memory_order success, memory_order failure) const noexcept;
bool compare_exchange_strong(T& expected, T desired,
memory_order success, memory_order failure) const noexcept;
bool compare_exchange_weak(T& expected, T desired,
memory_order order = memory_order::seq_cst) const noexcept;
bool compare_exchange_strong(T& expected, T desired,
memory_order order = memory_order::seq_cst) const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum