Skip to content

Commit 81fcbcd

Browse files
committedSep 28, 2024
Further clarificarion for atomic and UnsafeCell docs:
- UnsafeCell: mention the term "data race", and reference the data race definition - atomic: failing RMWs are just reads, reorder and reword docs
1 parent e7c99a7 commit 81fcbcd

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed
 

‎core/src/cell.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1895,11 +1895,17 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
18951895
/// uniqueness guarantee for mutable references is unaffected. There is *no* legal way to obtain
18961896
/// aliasing `&mut`, not even with `UnsafeCell<T>`.
18971897
///
1898+
/// `UnsafeCell` does nothing to avoid data races; they are still undefined behavior. If multiple
1899+
/// threads have access to the same `UnsafeCell`, they must follow the usual rules of the
1900+
/// [concurrent memory model]: conflicting non-synchronized accesses must be done via the APIs in
1901+
/// [`core::sync::atomic`].
1902+
///
18981903
/// The `UnsafeCell` API itself is technically very simple: [`.get()`] gives you a raw pointer
18991904
/// `*mut T` to its contents. It is up to _you_ as the abstraction designer to use that raw pointer
19001905
/// correctly.
19011906
///
19021907
/// [`.get()`]: `UnsafeCell::get`
1908+
/// [concurrent memory model]: ../sync/atomic/index.html#memory-model-for-atomic-accesses
19031909
///
19041910
/// The precise Rust aliasing rules are somewhat in flux, but the main points are not contentious:
19051911
///
@@ -1922,10 +1928,6 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
19221928
/// live memory and the compiler is allowed to insert spurious reads if it can prove that this
19231929
/// memory has not yet been deallocated.
19241930
///
1925-
/// - At all times, you must avoid data races. If multiple threads have access to
1926-
/// the same `UnsafeCell`, then any writes must have a proper happens-before relation to all other
1927-
/// accesses (or use atomics).
1928-
///
19291931
/// To assist with proper design, the following scenarios are explicitly declared legal
19301932
/// for single-threaded code:
19311933
///

‎core/src/sync/atomic.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@
3333
//! atomic load (via the operations provided in this module). A "modification of an atomic object"
3434
//! refers to an atomic store.
3535
//!
36-
//! The most important aspect of this model is that conflicting non-synchronized accesses are
37-
//! Undefined Behavior unless both accesses are atomic. Here, accesses are *conflicting* if they
38-
//! affect overlapping regions of memory and at least one of them is a write. They are
39-
//! *non-synchronized* if neither of them *happens-before* the other, according to the
40-
//! happens-before order of the memory model.
41-
//!
4236
//! The end result is *almost* equivalent to saying that creating a *shared reference* to one of the
4337
//! Rust atomic types corresponds to creating an `atomic_ref` in C++, with the `atomic_ref` being
4438
//! destroyed when the lifetime of the shared reference ends. The main difference is that Rust
@@ -47,20 +41,25 @@
4741
//! objects" and "non-atomic objects" (with `atomic_ref` temporarily converting a non-atomic object
4842
//! into an atomic object).
4943
//!
50-
//! That said, Rust *does* inherit the C++ limitation that non-synchronized conflicting atomic
51-
//! accesses may not partially overlap: they must be either disjoint or access the exact same
52-
//! memory. This in particular rules out non-synchronized differently-sized atomic accesses to the
53-
//! same data unless all accesses are reads.
44+
//! The most important aspect of this model is that *data races* are undefined behavior. A data race
45+
//! is defined as conflicting non-synchronized accesses where at least one of the accesses is
46+
//! non-atomic. Here, accesses are *conflicting* if they affect overlapping regions of memory and at
47+
//! least one of them is a write. They are *non-synchronized* if neither of them *happens-before*
48+
//! the other, according to the happens-before order of the memory model.
5449
//!
55-
//! [cpp]: https://en.cppreference.com/w/cpp/atomic
56-
//! [cpp-intro.races]: https://timsong-cpp.github.io/cppwp/n4868/intro.multithread#intro.races
50+
//! The other possible cause of undefined behavior in the memory model are mixed-size accesses: Rust
51+
//! inherits the C++ limitation that non-synchronized conflicting atomic accesses may not partially
52+
//! overlap. In other words, every pair of non-synchronized atomic accesses must be either disjoint,
53+
//! access the exact same memory (including using the same access size), or both be reads.
5754
//!
58-
//! Each method takes an [`Ordering`] which represents the strength of
59-
//! the memory barrier for that operation. These orderings behave the
60-
//! same as the corresponding [C++20 atomic orderings][1]. For more information see the [nomicon][2].
55+
//! Each atomic access takes an [`Ordering`] which defines how the operation interacts with the
56+
//! happens-before order. These orderings behave the same as the corresponding [C++20 atomic
57+
//! orderings][cpp_memory_order]. For more information, see the [nomicon].
6158
//!
62-
//! [1]: https://en.cppreference.com/w/cpp/atomic/memory_order
63-
//! [2]: ../../../nomicon/atomics.html
59+
//! [cpp]: https://en.cppreference.com/w/cpp/atomic
60+
//! [cpp-intro.races]: https://timsong-cpp.github.io/cppwp/n4868/intro.multithread#intro.races
61+
//! [cpp_memory_order]: https://en.cppreference.com/w/cpp/atomic/memory_order
62+
//! [nomicon]: ../../../nomicon/atomics.html
6463
//!
6564
//! ```rust,no_run undefined_behavior
6665
//! use std::sync::atomic::{AtomicU16, AtomicU8, Ordering};
@@ -157,7 +156,7 @@
157156
//!
158157
//! # Atomic accesses to read-only memory
159158
//!
160-
//! In general, *all* atomic accesses on read-only memory are Undefined Behavior. For instance, attempting
159+
//! In general, *all* atomic accesses on read-only memory are undefined behavior. For instance, attempting
161160
//! to do a `compare_exchange` that will definitely fail (making it conceptually a read-only
162161
//! operation) can still cause a segmentation fault if the underlying memory page is mapped read-only. Since
163162
//! atomic `load`s might be implemented using compare-exchange operations, even a `load` can fault
@@ -173,7 +172,7 @@
173172
//!
174173
//! As an exception from the general rule stated above, "sufficiently small" atomic loads with
175174
//! `Ordering::Relaxed` are implemented in a way that works on read-only memory, and are hence not
176-
//! Undefined Behavior. The exact size limit for what makes a load "sufficiently small" varies
175+
//! undefined behavior. The exact size limit for what makes a load "sufficiently small" varies
177176
//! depending on the target:
178177
//!
179178
//! | `target_arch` | Size limit |

0 commit comments

Comments
 (0)