about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2023-09-05 19:50:48 +0200
committerRalf Jung <post@ralfj.de>2023-10-15 17:41:50 +0200
commitb5e67a00d90fb7e77471f8bbc9a5469ee611929f (patch)
treefe259e44fad65128441004cfe26b1d9b7f2d964b
parent481d45abeced571b533016a994cba7337102a4a4 (diff)
downloadrust-b5e67a00d90fb7e77471f8bbc9a5469ee611929f.tar.gz
rust-b5e67a00d90fb7e77471f8bbc9a5469ee611929f.zip
document when atomic loads are guaranteed read-only
-rw-r--r--library/core/src/sync/atomic.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/library/core/src/sync/atomic.rs b/library/core/src/sync/atomic.rs
index de41bd1a116..1443b9d594c 100644
--- a/library/core/src/sync/atomic.rs
+++ b/library/core/src/sync/atomic.rs
@@ -79,6 +79,26 @@
 //!
 //! [lock-free]: https://en.wikipedia.org/wiki/Non-blocking_algorithm
 //!
+//! # Atomic accesses to read-only memory
+//!
+//! In general, atomic accesses on read-only memory are Undefined Behavior. For instance, attempting
+//! to do a `compare_exchange` that will definitely fail (making it conceptually a read-only
+//! operation) can still cause a page fault if the underlying memory page is mapped read-only. Since
+//! atomic `load`s might be implemented using compare-exchange operations, even a `load` can fault
+//! on read-only memory.
+//!
+//! However, as an exception from this general rule, Rust guarantees that "sufficiently small"
+//! atomic loads are implemented in a way that works on read-only memory. This threshold of
+//! "sufficiently small" depends on the architecture:
+//!
+//! | Target architecture | Maximal atomic `load` size that is guaranteed read-only |
+//! |----------|---------|
+//! | `x86`    | 4 bytes |
+//! | `x86_64` | 8 bytes |
+//!
+//! Any atomic `load` on read-only memory larger than the given size are Undefined Behavior. For
+//! architectures not listed above, all atomic `load` on read-only memory are Undefined Behavior.
+//!
 //! # Examples
 //!
 //! A simple spinlock: