@@ -891,6 +891,19 @@ impl AtomicBool {
891
891
/// Err(false));
892
892
/// assert_eq!(some_bool.load(Ordering::Relaxed), false);
893
893
/// ```
894
+ ///
895
+ /// # Considerations
896
+ ///
897
+ /// `compare_exchange` is a [compare-and-swap operation] and thus exhibits the usual downsides
898
+ /// of CAS operations. In particular, a load of the value followed by a successful
899
+ /// `compare_exchange` with the previous load *does not ensure* that other threads have not
900
+ /// changed the value in the interim. This is usually important when the *equality* check in
901
+ /// the `compare_exchange` is being used to check the *identity* of a value, but equality
902
+ /// does not necessarily imply identity. In this case, `compare_exchange` can lead to the
903
+ /// [ABA problem].
904
+ ///
905
+ /// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
906
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
894
907
#[ inline]
895
908
#[ stable( feature = "extended_compare_and_swap" , since = "1.10.0" ) ]
896
909
#[ doc( alias = "compare_and_swap" ) ]
@@ -973,6 +986,19 @@ impl AtomicBool {
973
986
/// }
974
987
/// }
975
988
/// ```
989
+ ///
990
+ /// # Considerations
991
+ ///
992
+ /// `compare_exchange` is a [compare-and-swap operation] and thus exhibits the usual downsides
993
+ /// of CAS operations. In particular, a load of the value followed by a successful
994
+ /// `compare_exchange` with the previous load *does not ensure* that other threads have not
995
+ /// changed the value in the interim. This is usually important when the *equality* check in
996
+ /// the `compare_exchange` is being used to check the *identity* of a value, but equality
997
+ /// does not necessarily imply identity. In this case, `compare_exchange` can lead to the
998
+ /// [ABA problem].
999
+ ///
1000
+ /// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1001
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
976
1002
#[ inline]
977
1003
#[ stable( feature = "extended_compare_and_swap" , since = "1.10.0" ) ]
978
1004
#[ doc( alias = "compare_and_swap" ) ]
@@ -1271,11 +1297,14 @@ impl AtomicBool {
1271
1297
///
1272
1298
/// # Considerations
1273
1299
///
1274
- /// This method is not magic; it is not provided by the hardware.
1275
- /// It is implemented in terms of [`AtomicBool::compare_exchange_weak`], and suffers from the same drawbacks.
1276
- /// In particular, this method will not circumvent the [ABA Problem].
1300
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
1301
+ /// critical section or mutex.
1302
+ ///
1303
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
1304
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem].
1277
1305
///
1278
1306
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1307
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1279
1308
///
1280
1309
/// # Examples
1281
1310
///
@@ -1338,11 +1367,14 @@ impl AtomicBool {
1338
1367
///
1339
1368
/// # Considerations
1340
1369
///
1341
- /// This method is not magic; it is not provided by the hardware.
1342
- /// It is implemented in terms of [`AtomicBool::compare_exchange_weak`], and suffers from the same drawbacks.
1343
- /// In particular, this method will not circumvent the [ABA Problem].
1370
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
1371
+ /// critical section or mutex.
1372
+ ///
1373
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
1374
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem].
1344
1375
///
1345
1376
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1377
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1346
1378
///
1347
1379
/// # Examples
1348
1380
///
@@ -1393,11 +1425,14 @@ impl AtomicBool {
1393
1425
///
1394
1426
/// # Considerations
1395
1427
///
1396
- /// This method is not magic; it is not provided by the hardware.
1397
- /// It is implemented in terms of [`AtomicBool::compare_exchange_weak`], and suffers from the same drawbacks.
1398
- /// In particular, this method will not circumvent the [ABA Problem].
1428
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
1429
+ /// critical section or mutex.
1430
+ ///
1431
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
1432
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem].
1399
1433
///
1400
1434
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1435
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1401
1436
///
1402
1437
/// # Examples
1403
1438
///
@@ -1825,6 +1860,20 @@ impl<T> AtomicPtr<T> {
1825
1860
/// let value = some_ptr.compare_exchange(ptr, other_ptr,
1826
1861
/// Ordering::SeqCst, Ordering::Relaxed);
1827
1862
/// ```
1863
+ ///
1864
+ /// # Considerations
1865
+ ///
1866
+ /// `compare_exchange` is a [compare-and-swap operation] and thus exhibits the usual downsides
1867
+ /// of CAS operations. In particular, a load of the value followed by a successful
1868
+ /// `compare_exchange` with the previous load *does not ensure* that other threads have not
1869
+ /// changed the value in the interim. This is usually important when the *equality* check in
1870
+ /// the `compare_exchange` is being used to check the *identity* of a value, but equality
1871
+ /// does not necessarily imply identity. This is a particularly common case for pointers, as
1872
+ /// a pointer holding the same address does not imply that the same object exists at that
1873
+ /// address! In this case, `compare_exchange` can lead to the [ABA problem].
1874
+ ///
1875
+ /// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1876
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1828
1877
#[ inline]
1829
1878
#[ stable( feature = "extended_compare_and_swap" , since = "1.10.0" ) ]
1830
1879
#[ cfg( target_has_atomic = "ptr" ) ]
@@ -1874,6 +1923,20 @@ impl<T> AtomicPtr<T> {
1874
1923
/// }
1875
1924
/// }
1876
1925
/// ```
1926
+ ///
1927
+ /// # Considerations
1928
+ ///
1929
+ /// `compare_exchange` is a [compare-and-swap operation] and thus exhibits the usual downsides
1930
+ /// of CAS operations. In particular, a load of the value followed by a successful
1931
+ /// `compare_exchange` with the previous load *does not ensure* that other threads have not
1932
+ /// changed the value in the interim. This is usually important when the *equality* check in
1933
+ /// the `compare_exchange` is being used to check the *identity* of a value, but equality
1934
+ /// does not necessarily imply identity. This is a particularly common case for pointers, as
1935
+ /// a pointer holding the same address does not imply that the same object exists at that
1936
+ /// address! In this case, `compare_exchange` can lead to the [ABA problem].
1937
+ ///
1938
+ /// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1939
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1877
1940
#[ inline]
1878
1941
#[ stable( feature = "extended_compare_and_swap" , since = "1.10.0" ) ]
1879
1942
#[ cfg( target_has_atomic = "ptr" ) ]
@@ -1917,11 +1980,15 @@ impl<T> AtomicPtr<T> {
1917
1980
///
1918
1981
/// # Considerations
1919
1982
///
1920
- /// This method is not magic; it is not provided by the hardware.
1921
- /// It is implemented in terms of [`AtomicPtr::compare_exchange_weak`], and suffers from the same drawbacks.
1922
- /// In particular, this method will not circumvent the [ABA Problem].
1983
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
1984
+ /// critical section or mutex.
1985
+ ///
1986
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
1987
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem],
1988
+ /// which is a particularly common pitfall for pointers!
1923
1989
///
1924
1990
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1991
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1925
1992
///
1926
1993
/// # Examples
1927
1994
///
@@ -1992,11 +2059,15 @@ impl<T> AtomicPtr<T> {
1992
2059
///
1993
2060
/// # Considerations
1994
2061
///
1995
- /// This method is not magic; it is not provided by the hardware.
1996
- /// It is implemented in terms of [`AtomicPtr::compare_exchange_weak`], and suffers from the same drawbacks.
1997
- /// In particular, this method will not circumvent the [ABA Problem].
2062
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
2063
+ /// critical section or mutex.
2064
+ ///
2065
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
2066
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem],
2067
+ /// which is a particularly common pitfall for pointers!
1998
2068
///
1999
2069
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
2070
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
2000
2071
///
2001
2072
/// # Examples
2002
2073
///
@@ -2057,11 +2128,15 @@ impl<T> AtomicPtr<T> {
2057
2128
///
2058
2129
/// # Considerations
2059
2130
///
2060
- /// This method is not magic; it is not provided by the hardware.
2061
- /// It is implemented in terms of [`AtomicPtr::compare_exchange_weak`], and suffers from the same drawbacks.
2062
- /// In particular, this method will not circumvent the [ABA Problem].
2131
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
2132
+ /// critical section or mutex.
2133
+ ///
2134
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
2135
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem],
2136
+ /// which is a particularly common pitfall for pointers!
2063
2137
///
2064
2138
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
2139
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
2065
2140
///
2066
2141
/// # Examples
2067
2142
///
@@ -2967,6 +3042,20 @@ macro_rules! atomic_int {
2967
3042
/// Err(10));
2968
3043
/// assert_eq!(some_var.load(Ordering::Relaxed), 10);
2969
3044
/// ```
3045
+ ///
3046
+ /// # Considerations
3047
+ ///
3048
+ /// `compare_exchange` is a [compare-and-swap operation] and thus exhibits the usual downsides
3049
+ /// of CAS operations. In particular, a load of the value followed by a successful
3050
+ /// `compare_exchange` with the previous load *does not ensure* that other threads have not
3051
+ /// changed the value in the interim! This is usually important when the *equality* check in
3052
+ /// the `compare_exchange` is being used to check the *identity* of a value, but equality
3053
+ /// does not necessarily imply identity. This is a particularly common case for pointers, as
3054
+ /// a pointer holding the same address does not imply that the same object exists at that
3055
+ /// address! In this case, `compare_exchange` can lead to the [ABA problem].
3056
+ ///
3057
+ /// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
3058
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
2970
3059
#[ inline]
2971
3060
#[ $stable_cxchg]
2972
3061
#[ $cfg_cas]
@@ -3016,6 +3105,20 @@ macro_rules! atomic_int {
3016
3105
/// }
3017
3106
/// }
3018
3107
/// ```
3108
+ ///
3109
+ /// # Considerations
3110
+ ///
3111
+ /// `compare_exchange` is a [compare-and-swap operation] and thus exhibits the usual downsides
3112
+ /// of CAS operations. In particular, a load of the value followed by a successful
3113
+ /// `compare_exchange` with the previous load *does not ensure* that other threads have not
3114
+ /// changed the value in the interim. This is usually important when the *equality* check in
3115
+ /// the `compare_exchange` is being used to check the *identity* of a value, but equality
3116
+ /// does not necessarily imply identity. This is a particularly common case for pointers, as
3117
+ /// a pointer holding the same address does not imply that the same object exists at that
3118
+ /// address! In this case, `compare_exchange` can lead to the [ABA problem].
3119
+ ///
3120
+ /// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
3121
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
3019
3122
#[ inline]
3020
3123
#[ $stable_cxchg]
3021
3124
#[ $cfg_cas]
@@ -3246,13 +3349,16 @@ macro_rules! atomic_int {
3246
3349
///
3247
3350
/// # Considerations
3248
3351
///
3249
- /// This method is not magic; it is not provided by the hardware.
3250
- /// It is implemented in terms of
3251
- #[ doc = concat!( "[`" , stringify!( $atomic_type) , "::compare_exchange_weak`]," ) ]
3252
- /// and suffers from the same drawbacks.
3253
- /// In particular, this method will not circumvent the [ABA Problem].
3352
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
3353
+ /// critical section or mutex.
3354
+ ///
3355
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
3356
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem]
3357
+ /// if this atomic integer is an index or more generally if knowledge of only the *bitwise value*
3358
+ /// of the atomic is not in and of itself sufficient to ensure any required preconditions.
3254
3359
///
3255
3360
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
3361
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
3256
3362
///
3257
3363
/// # Examples
3258
3364
///
@@ -3309,13 +3415,16 @@ macro_rules! atomic_int {
3309
3415
///
3310
3416
/// # Considerations
3311
3417
///
3312
- /// This method is not magic; it is not provided by the hardware.
3313
- /// It is implemented in terms of
3314
- #[ doc = concat!( "[`" , stringify!( $atomic_type) , "::compare_exchange_weak`]," ) ]
3315
- /// and suffers from the same drawbacks.
3316
- /// In particular, this method will not circumvent the [ABA Problem].
3418
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
3419
+ /// critical section or mutex.
3420
+ ///
3421
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
3422
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem]
3423
+ /// if this atomic integer is an index or more generally if knowledge of only the *bitwise value*
3424
+ /// of the atomic is not in and of itself sufficient to ensure any required preconditions.
3317
3425
///
3318
3426
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
3427
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
3319
3428
///
3320
3429
/// # Examples
3321
3430
///
@@ -3367,13 +3476,17 @@ macro_rules! atomic_int {
3367
3476
///
3368
3477
/// # Considerations
3369
3478
///
3370
- /// This method is not magic; it is not provided by the hardware.
3371
- /// It is implemented in terms of
3372
- #[ doc = concat!( "[`" , stringify!( $atomic_type) , "::compare_exchange_weak`]," ) ]
3373
- /// and suffers from the same drawbacks.
3374
- /// In particular, this method will not circumvent the [ABA Problem].
3479
+ /// [CAS operation]: https://en.wikipedia.org/wiki/Compare-and-swap
3480
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
3481
+ /// critical section or mutex.
3482
+ ///
3483
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
3484
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem]
3485
+ /// if this atomic integer is an index or more generally if knowledge of only the *bitwise value*
3486
+ /// of the atomic is not in and of itself sufficient to ensure any required preconditions.
3375
3487
///
3376
3488
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
3489
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
3377
3490
///
3378
3491
/// # Examples
3379
3492
///
0 commit comments