Skip to content

Commit f95834b

Browse files
committed
Auto merge of #57425 - alexcrichton:stabilize-atomics, r=sfackler
std: Stabilize fixed-width integer atomics This commit stabilizes the `Atomic{I,U}{8,16,32,64}` APIs in the `std::sync::atomic` and `core::sync::atomic` modules. Proposed in #56753 and tracked in #32976 this feature has been unstable for quite some time and is hopefully ready to go over the finish line now! The API is being stabilized as-is. The API of `AtomicU8` and friends mirrors that of `AtomicUsize`. A list of changes made here are: * A portability documentation section has been added to describe the current state of affairs. * Emulation of smaller-size atomics with larger-size atomics has been documented. * As an added bonus, `ATOMIC_*_INIT` is now scheduled for deprecation across the board in 1.34.0 now that `const` functions can be invoked in statics. Note that the 128-bit atomic types are omitted from this stabilization explicitly. They have far less platform support than the other atomic types, and will likely require further discussion about their best location. Closes #32976 Closes #56753
2 parents 42eb5ff + 14b36fb commit f95834b

File tree

1 file changed

+96
-55
lines changed

1 file changed

+96
-55
lines changed

src/libcore/sync/atomic.rs

+96-55
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
//! types.
66
//!
77
//! 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.
910
//! Atomic types present operations that, when used correctly, synchronize
1011
//! updates between threads.
1112
//!
1213
//! [`AtomicBool`]: struct.AtomicBool.html
1314
//! [`AtomicIsize`]: struct.AtomicIsize.html
1415
//! [`AtomicUsize`]: struct.AtomicUsize.html
16+
//! [`AtomicI8`]: struct.AtomicI8.html
17+
//! [`AtomicU16`]: struct.AtomicU16.html
1518
//!
1619
//! Each method takes an [`Ordering`] which represents the strength of
1720
//! the memory barrier for that operation. These orderings are the
@@ -31,11 +34,46 @@
3134
//! [`Sync`]: ../../marker/trait.Sync.html
3235
//! [arc]: ../../../std/sync/struct.Arc.html
3336
//!
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
3639
//! are often used for lazy global initialization.
3740
//!
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
3977
//!
4078
//! # Examples
4179
//!
@@ -66,9 +104,9 @@
66104
//! Keep a global count of live threads:
67105
//!
68106
//! ```
69-
//! use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
107+
//! use std::sync::atomic::{AtomicUsize, Ordering};
70108
//!
71-
//! static GLOBAL_THREAD_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
109+
//! static GLOBAL_THREAD_COUNT: AtomicUsize = AtomicUsize::new(0);
72110
//!
73111
//! let old_thread_count = GLOBAL_THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
74112
//! println!("live threads: {}", old_thread_count + 1);
@@ -252,6 +290,7 @@ pub enum Ordering {
252290
/// [`AtomicBool`]: struct.AtomicBool.html
253291
#[cfg(target_has_atomic = "8")]
254292
#[stable(feature = "rust1", since = "1.0.0")]
293+
#[rustc_deprecated(since = "1.34.0", reason = "the `new` function is now preferred")]
255294
pub const ATOMIC_BOOL_INIT: AtomicBool = AtomicBool::new(false);
256295

257296
#[cfg(target_has_atomic = "8")]
@@ -1097,7 +1136,8 @@ macro_rules! atomic_int {
10971136
/// `](
10981137
#[doc = $int_ref]
10991138
/// ). 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].
11011141
///
11021142
/// [module-level documentation]: index.html
11031143
#[$stable]
@@ -1108,6 +1148,7 @@ macro_rules! atomic_int {
11081148

11091149
/// An atomic integer initialized to `0`.
11101150
#[$stable]
1151+
#[rustc_deprecated(since = "1.34.0", reason = "the `new` function is now preferred")]
11111152
pub const $atomic_init: $atomic_type = $atomic_type::new(0);
11121153

11131154
#[$stable]
@@ -1827,12 +1868,12 @@ assert_eq!(min_foo, 12);
18271868

18281869
#[cfg(target_has_atomic = "8")]
18291870
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"),
18361877
"i8", "../../../std/primitive.i8.html",
18371878
"#![feature(integer_atomics)]\n\n",
18381879
atomic_min, atomic_max,
@@ -1841,12 +1882,12 @@ atomic_int! {
18411882
}
18421883
#[cfg(target_has_atomic = "8")]
18431884
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"),
18501891
"u8", "../../../std/primitive.u8.html",
18511892
"#![feature(integer_atomics)]\n\n",
18521893
atomic_umin, atomic_umax,
@@ -1855,12 +1896,12 @@ atomic_int! {
18551896
}
18561897
#[cfg(target_has_atomic = "16")]
18571898
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"),
18641905
"i16", "../../../std/primitive.i16.html",
18651906
"#![feature(integer_atomics)]\n\n",
18661907
atomic_min, atomic_max,
@@ -1869,12 +1910,12 @@ atomic_int! {
18691910
}
18701911
#[cfg(target_has_atomic = "16")]
18711912
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"),
18781919
"u16", "../../../std/primitive.u16.html",
18791920
"#![feature(integer_atomics)]\n\n",
18801921
atomic_umin, atomic_umax,
@@ -1883,12 +1924,12 @@ atomic_int! {
18831924
}
18841925
#[cfg(target_has_atomic = "32")]
18851926
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"),
18921933
"i32", "../../../std/primitive.i32.html",
18931934
"#![feature(integer_atomics)]\n\n",
18941935
atomic_min, atomic_max,
@@ -1897,12 +1938,12 @@ atomic_int! {
18971938
}
18981939
#[cfg(target_has_atomic = "32")]
18991940
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"),
19061947
"u32", "../../../std/primitive.u32.html",
19071948
"#![feature(integer_atomics)]\n\n",
19081949
atomic_umin, atomic_umax,
@@ -1911,12 +1952,12 @@ atomic_int! {
19111952
}
19121953
#[cfg(target_has_atomic = "64")]
19131954
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"),
19201961
"i64", "../../../std/primitive.i64.html",
19211962
"#![feature(integer_atomics)]\n\n",
19221963
atomic_min, atomic_max,
@@ -1925,12 +1966,12 @@ atomic_int! {
19251966
}
19261967
#[cfg(target_has_atomic = "64")]
19271968
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"),
19341975
"u64", "../../../std/primitive.u64.html",
19351976
"#![feature(integer_atomics)]\n\n",
19361977
atomic_umin, atomic_umax,

0 commit comments

Comments
 (0)