5
5
//! types.
6
6
//!
7
7
//! This module defines atomic versions of a select number of primitive
8
- //! types, including [`AtomicBool`], [`AtomicIsize`], and [`AtomicUsize`].
8
+ //! types, including [`AtomicBool`], [`AtomicIsize`], [`AtomicUsize`],
9
+ //! [`AtomicI8`], [`AtomicU16`], etc.
9
10
//! Atomic types present operations that, when used correctly, synchronize
10
11
//! updates between threads.
11
12
//!
12
13
//! [`AtomicBool`]: struct.AtomicBool.html
13
14
//! [`AtomicIsize`]: struct.AtomicIsize.html
14
15
//! [`AtomicUsize`]: struct.AtomicUsize.html
16
+ //! [`AtomicI8`]: struct.AtomicI8.html
17
+ //! [`AtomicU16`]: struct.AtomicU16.html
15
18
//!
16
19
//! Each method takes an [`Ordering`] which represents the strength of
17
20
//! the memory barrier for that operation. These orderings are the
31
34
//! [`Sync`]: ../../marker/trait.Sync.html
32
35
//! [arc]: ../../../std/sync/struct.Arc.html
33
36
//!
34
- //! Most atomic types may be stored in static variables, initialized using
35
- //! the provided static initializers like [`ATOMIC_BOOL_INIT `]. Atomic statics
37
+ //! Atomic types may be stored in static variables, initialized using
38
+ //! the constant initializers like [`AtomicBool::new `]. Atomic statics
36
39
//! are often used for lazy global initialization.
37
40
//!
38
- //! [`ATOMIC_BOOL_INIT`]: constant.ATOMIC_BOOL_INIT.html
41
+ //! [`AtomicBool::new`]: struct.AtomicBool.html#method.new
42
+ //!
43
+ //! # Portability
44
+ //!
45
+ //! All atomic types in this module are guaranteed to be [lock-free] if they're
46
+ //! available. This means they don't internally acquire a global mutex. Atomic
47
+ //! types and operations are not guaranteed to be wait-free. This means that
48
+ //! operations like `fetch_or` may be implemented with a compare-and-swap loop.
49
+ //!
50
+ //! Atomic operations may be implemented at the instruction layer with
51
+ //! larger-size atomics. For example some platforms use 4-byte atomic
52
+ //! instructions to implement `AtomicI8`. Note that this emulation should not
53
+ //! have an impact on correctness of code, it's just something to be aware of.
54
+ //!
55
+ //! The atomic types in this module may not be available on all platforms. The
56
+ //! atomic types here are all widely available, however, and can generally be
57
+ //! relied upon existing. Some notable exceptions are:
58
+ //!
59
+ //! * PowerPC and MIPS platforms with 32-bit pointers do not have `AtomicU64` or
60
+ //! `AtomicI64` types.
61
+ //! * ARM platforms like `armv5te` that aren't for Linux do not have any atomics
62
+ //! at all.
63
+ //! * ARM targets with `thumbv6m` do not have atomic operations at all.
64
+ //!
65
+ //! Note that future platforms may be added that also do not have support for
66
+ //! some atomic operations. Maximally portable code will want to be careful
67
+ //! about which atomic types are used. `AtomicUsize` and `AtomicIsize` are
68
+ //! generally the most portable, but even then they're not available everywhere.
69
+ //! For reference, the `std` library requires pointer-sized atomics, although
70
+ //! `core` does not.
71
+ //!
72
+ //! Currently you'll need to use `#[cfg(target_arch)]` primarily to
73
+ //! conditionally compile in code with atomics. There is an unstable
74
+ //! `#[cfg(target_has_atomic)]` as well which may be stabilized in the future.
75
+ //!
76
+ //! [lock-free]: https://en.wikipedia.org/wiki/Non-blocking_algorithm
39
77
//!
40
78
//! # Examples
41
79
//!
66
104
//! Keep a global count of live threads:
67
105
//!
68
106
//! ```
69
- //! use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT };
107
+ //! use std::sync::atomic::{AtomicUsize, Ordering};
70
108
//!
71
- //! static GLOBAL_THREAD_COUNT: AtomicUsize = ATOMIC_USIZE_INIT ;
109
+ //! static GLOBAL_THREAD_COUNT: AtomicUsize = AtomicUsize::new(0) ;
72
110
//!
73
111
//! let old_thread_count = GLOBAL_THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
74
112
//! println!("live threads: {}", old_thread_count + 1);
@@ -252,6 +290,7 @@ pub enum Ordering {
252
290
/// [`AtomicBool`]: struct.AtomicBool.html
253
291
#[ cfg( target_has_atomic = "8" ) ]
254
292
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
293
+ #[ rustc_deprecated( since = "1.34.0" , reason = "the `new` function is now preferred" ) ]
255
294
pub const ATOMIC_BOOL_INIT : AtomicBool = AtomicBool :: new ( false ) ;
256
295
257
296
#[ cfg( target_has_atomic = "8" ) ]
@@ -1097,7 +1136,8 @@ macro_rules! atomic_int {
1097
1136
/// `](
1098
1137
#[ doc = $int_ref]
1099
1138
/// ). For more about the differences between atomic types and
1100
- /// non-atomic types, please see the [module-level documentation].
1139
+ /// non-atomic types as well as information about the portability of
1140
+ /// this type, please see the [module-level documentation].
1101
1141
///
1102
1142
/// [module-level documentation]: index.html
1103
1143
#[ $stable]
@@ -1108,6 +1148,7 @@ macro_rules! atomic_int {
1108
1148
1109
1149
/// An atomic integer initialized to `0`.
1110
1150
#[ $stable]
1151
+ #[ rustc_deprecated( since = "1.34.0" , reason = "the `new` function is now preferred" ) ]
1111
1152
pub const $atomic_init: $atomic_type = $atomic_type:: new( 0 ) ;
1112
1153
1113
1154
#[ $stable]
@@ -1827,12 +1868,12 @@ assert_eq!(min_foo, 12);
1827
1868
1828
1869
#[ cfg( target_has_atomic = "8" ) ]
1829
1870
atomic_int ! {
1830
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1831
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1832
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1833
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1834
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1835
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1871
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1872
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1873
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1874
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1875
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1876
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1836
1877
"i8" , "../../../std/primitive.i8.html" ,
1837
1878
"#![feature(integer_atomics)]\n \n " ,
1838
1879
atomic_min, atomic_max,
@@ -1841,12 +1882,12 @@ atomic_int! {
1841
1882
}
1842
1883
#[ cfg( target_has_atomic = "8" ) ]
1843
1884
atomic_int ! {
1844
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1845
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1846
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1847
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1848
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1849
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1885
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1886
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1887
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1888
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1889
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1890
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1850
1891
"u8" , "../../../std/primitive.u8.html" ,
1851
1892
"#![feature(integer_atomics)]\n \n " ,
1852
1893
atomic_umin, atomic_umax,
@@ -1855,12 +1896,12 @@ atomic_int! {
1855
1896
}
1856
1897
#[ cfg( target_has_atomic = "16" ) ]
1857
1898
atomic_int ! {
1858
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1859
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1860
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1861
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1862
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1863
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1899
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1900
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1901
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1902
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1903
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1904
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1864
1905
"i16" , "../../../std/primitive.i16.html" ,
1865
1906
"#![feature(integer_atomics)]\n \n " ,
1866
1907
atomic_min, atomic_max,
@@ -1869,12 +1910,12 @@ atomic_int! {
1869
1910
}
1870
1911
#[ cfg( target_has_atomic = "16" ) ]
1871
1912
atomic_int ! {
1872
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1873
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1874
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1875
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1876
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1877
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1913
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1914
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1915
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1916
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1917
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1918
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1878
1919
"u16" , "../../../std/primitive.u16.html" ,
1879
1920
"#![feature(integer_atomics)]\n \n " ,
1880
1921
atomic_umin, atomic_umax,
@@ -1883,12 +1924,12 @@ atomic_int! {
1883
1924
}
1884
1925
#[ cfg( target_has_atomic = "32" ) ]
1885
1926
atomic_int ! {
1886
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1887
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1888
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1889
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1890
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1891
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1927
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1928
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1929
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1930
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1931
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1932
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1892
1933
"i32" , "../../../std/primitive.i32.html" ,
1893
1934
"#![feature(integer_atomics)]\n \n " ,
1894
1935
atomic_min, atomic_max,
@@ -1897,12 +1938,12 @@ atomic_int! {
1897
1938
}
1898
1939
#[ cfg( target_has_atomic = "32" ) ]
1899
1940
atomic_int ! {
1900
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1901
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1902
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1903
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1904
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1905
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1941
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1942
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1943
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1944
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1945
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1946
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1906
1947
"u32" , "../../../std/primitive.u32.html" ,
1907
1948
"#![feature(integer_atomics)]\n \n " ,
1908
1949
atomic_umin, atomic_umax,
@@ -1911,12 +1952,12 @@ atomic_int! {
1911
1952
}
1912
1953
#[ cfg( target_has_atomic = "64" ) ]
1913
1954
atomic_int ! {
1914
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1915
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1916
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1917
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1918
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1919
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1955
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1956
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1957
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1958
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1959
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1960
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1920
1961
"i64" , "../../../std/primitive.i64.html" ,
1921
1962
"#![feature(integer_atomics)]\n \n " ,
1922
1963
atomic_min, atomic_max,
@@ -1925,12 +1966,12 @@ atomic_int! {
1925
1966
}
1926
1967
#[ cfg( target_has_atomic = "64" ) ]
1927
1968
atomic_int ! {
1928
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1929
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1930
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1931
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1932
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1933
- unstable ( feature = "integer_atomics " , issue = "32976 " ) ,
1969
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1970
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1971
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1972
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1973
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1974
+ stable ( feature = "integer_atomics_stable " , since = "1.34.0 " ) ,
1934
1975
"u64" , "../../../std/primitive.u64.html" ,
1935
1976
"#![feature(integer_atomics)]\n \n " ,
1936
1977
atomic_umin, atomic_umax,
0 commit comments