@@ -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,15 @@ 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].
1305
+ ///
1277
1306
///
1278
1307
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1308
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1279
1309
///
1280
1310
/// # Examples
1281
1311
///
@@ -1338,11 +1368,15 @@ impl AtomicBool {
1338
1368
///
1339
1369
/// # Considerations
1340
1370
///
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].
1371
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
1372
+ /// critical section or mutex.
1373
+ ///
1374
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
1375
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem].
1376
+ ///
1344
1377
///
1345
1378
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1379
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1346
1380
///
1347
1381
/// # Examples
1348
1382
///
@@ -1393,11 +1427,15 @@ impl AtomicBool {
1393
1427
///
1394
1428
/// # Considerations
1395
1429
///
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].
1430
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
1431
+ /// critical section or mutex.
1432
+ ///
1433
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
1434
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem].
1435
+ ///
1399
1436
///
1400
1437
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1438
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1401
1439
///
1402
1440
/// # Examples
1403
1441
///
@@ -1825,6 +1863,20 @@ impl<T> AtomicPtr<T> {
1825
1863
/// let value = some_ptr.compare_exchange(ptr, other_ptr,
1826
1864
/// Ordering::SeqCst, Ordering::Relaxed);
1827
1865
/// ```
1866
+ ///
1867
+ /// # Considerations
1868
+ ///
1869
+ /// `compare_exchange` is a [compare-and-swap operation] and thus exhibits the usual downsides
1870
+ /// of CAS operations. In particular, a load of the value followed by a successful
1871
+ /// `compare_exchange` with the previous load *does not ensure* that other threads have not
1872
+ /// changed the value in the interim. This is usually important when the *equality* check in
1873
+ /// the `compare_exchange` is being used to check the *identity* of a value, but equality
1874
+ /// does not necessarily imply identity. This is a particularly common case for pointers, as
1875
+ /// a pointer holding the same address does not imply that the same object exists at that
1876
+ /// address! In this case, `compare_exchange` can lead to the [ABA problem].
1877
+ ///
1878
+ /// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1879
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1828
1880
#[ inline]
1829
1881
#[ stable( feature = "extended_compare_and_swap" , since = "1.10.0" ) ]
1830
1882
#[ cfg( target_has_atomic = "ptr" ) ]
@@ -1874,6 +1926,20 @@ impl<T> AtomicPtr<T> {
1874
1926
/// }
1875
1927
/// }
1876
1928
/// ```
1929
+ ///
1930
+ /// # Considerations
1931
+ ///
1932
+ /// `compare_exchange` is a [compare-and-swap operation] and thus exhibits the usual downsides
1933
+ /// of CAS operations. In particular, a load of the value followed by a successful
1934
+ /// `compare_exchange` with the previous load *does not ensure* that other threads have not
1935
+ /// changed the value in the interim. This is usually important when the *equality* check in
1936
+ /// the `compare_exchange` is being used to check the *identity* of a value, but equality
1937
+ /// does not necessarily imply identity. This is a particularly common case for pointers, as
1938
+ /// a pointer holding the same address does not imply that the same object exists at that
1939
+ /// address! In this case, `compare_exchange` can lead to the [ABA problem].
1940
+ ///
1941
+ /// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1942
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1877
1943
#[ inline]
1878
1944
#[ stable( feature = "extended_compare_and_swap" , since = "1.10.0" ) ]
1879
1945
#[ cfg( target_has_atomic = "ptr" ) ]
@@ -1917,11 +1983,16 @@ impl<T> AtomicPtr<T> {
1917
1983
///
1918
1984
/// # Considerations
1919
1985
///
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].
1986
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
1987
+ /// critical section or mutex.
1988
+ ///
1989
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
1990
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem],
1991
+ /// which is a particularly common pitfall for pointers!
1992
+ ///
1923
1993
///
1924
1994
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
1995
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
1925
1996
///
1926
1997
/// # Examples
1927
1998
///
@@ -1992,11 +2063,16 @@ impl<T> AtomicPtr<T> {
1992
2063
///
1993
2064
/// # Considerations
1994
2065
///
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].
2066
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
2067
+ /// critical section or mutex.
2068
+ ///
2069
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
2070
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem],
2071
+ /// which is a particularly common pitfall for pointers!
2072
+ ///
1998
2073
///
1999
2074
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
2075
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
2000
2076
///
2001
2077
/// # Examples
2002
2078
///
@@ -2057,11 +2133,16 @@ impl<T> AtomicPtr<T> {
2057
2133
///
2058
2134
/// # Considerations
2059
2135
///
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].
2136
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
2137
+ /// critical section or mutex.
2138
+ ///
2139
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
2140
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem],
2141
+ /// which is a particularly common pitfall for pointers!
2142
+ ///
2063
2143
///
2064
2144
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
2145
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
2065
2146
///
2066
2147
/// # Examples
2067
2148
///
@@ -2967,6 +3048,20 @@ macro_rules! atomic_int {
2967
3048
/// Err(10));
2968
3049
/// assert_eq!(some_var.load(Ordering::Relaxed), 10);
2969
3050
/// ```
3051
+ ///
3052
+ /// # Considerations
3053
+ ///
3054
+ /// `compare_exchange` is a [compare-and-swap operation] and thus exhibits the usual downsides
3055
+ /// of CAS operations. In particular, a load of the value followed by a successful
3056
+ /// `compare_exchange` with the previous load *does not ensure* that other threads have not
3057
+ /// changed the value in the interim! This is usually important when the *equality* check in
3058
+ /// the `compare_exchange` is being used to check the *identity* of a value, but equality
3059
+ /// does not necessarily imply identity. This is a particularly common case for pointers, as
3060
+ /// a pointer holding the same address does not imply that the same object exists at that
3061
+ /// address! In this case, `compare_exchange` can lead to the [ABA problem].
3062
+ ///
3063
+ /// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
3064
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
2970
3065
#[ inline]
2971
3066
#[ $stable_cxchg]
2972
3067
#[ $cfg_cas]
@@ -3016,6 +3111,20 @@ macro_rules! atomic_int {
3016
3111
/// }
3017
3112
/// }
3018
3113
/// ```
3114
+ ///
3115
+ /// # Considerations
3116
+ ///
3117
+ /// `compare_exchange` is a [compare-and-swap operation] and thus exhibits the usual downsides
3118
+ /// of CAS operations. In particular, a load of the value followed by a successful
3119
+ /// `compare_exchange` with the previous load *does not ensure* that other threads have not
3120
+ /// changed the value in the interim. This is usually important when the *equality* check in
3121
+ /// the `compare_exchange` is being used to check the *identity* of a value, but equality
3122
+ /// does not necessarily imply identity. This is a particularly common case for pointers, as
3123
+ /// a pointer holding the same address does not imply that the same object exists at that
3124
+ /// address! In this case, `compare_exchange` can lead to the [ABA problem].
3125
+ ///
3126
+ /// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
3127
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
3019
3128
#[ inline]
3020
3129
#[ $stable_cxchg]
3021
3130
#[ $cfg_cas]
@@ -3246,13 +3355,17 @@ macro_rules! atomic_int {
3246
3355
///
3247
3356
/// # Considerations
3248
3357
///
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].
3358
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
3359
+ /// critical section or mutex.
3360
+ ///
3361
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
3362
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem],
3363
+ /// if this atomic integer is an index or more generally if knowledge of only the *bitwise value*
3364
+ /// of the atomic is not in and of itself sufficient to ensure any required preconditions.
3365
+ ///
3254
3366
///
3255
3367
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
3368
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
3256
3369
///
3257
3370
/// # Examples
3258
3371
///
@@ -3309,13 +3422,17 @@ macro_rules! atomic_int {
3309
3422
///
3310
3423
/// # Considerations
3311
3424
///
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].
3425
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
3426
+ /// critical section or mutex.
3427
+ ///
3428
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
3429
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem],
3430
+ /// if this atomic integer is an index or more generally if knowledge of only the *bitwise value*
3431
+ /// of the atomic is not in and of itself sufficient to ensure any required preconditions.
3432
+ ///
3317
3433
///
3318
3434
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
3435
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
3319
3436
///
3320
3437
/// # Examples
3321
3438
///
@@ -3367,13 +3484,18 @@ macro_rules! atomic_int {
3367
3484
///
3368
3485
/// # Considerations
3369
3486
///
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].
3487
+ /// [CAS operation]: https://en.wikipedia.org/wiki/Compare-and-swap
3488
+ /// This method is not magic; it is not provided by the hardware, and does not act like a
3489
+ /// critical section or mutex.
3490
+ ///
3491
+ /// It is implemented on top of an atomic [compare-and-swap operation], and thus is subject to
3492
+ /// the usual drawbacks of CAS operations. In particular, be careful of the [ABA problem],
3493
+ /// if this atomic integer is an index or more generally if knowledge of only the *bitwise value*
3494
+ /// of the atomic is not in and of itself sufficient to ensure any required preconditions.
3495
+ ///
3375
3496
///
3376
3497
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem
3498
+ /// [compare-and-swap operation]: https://en.wikipedia.org/wiki/Compare-and-swap
3377
3499
///
3378
3500
/// # Examples
3379
3501
///
0 commit comments