Skip to content

Commit f330560

Browse files
committed
document when atomic loads are guaranteed read-only
1 parent f222a2d commit f330560

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

library/core/src/sync/atomic.rs

+20
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@
7979
//!
8080
//! [lock-free]: https://en.wikipedia.org/wiki/Non-blocking_algorithm
8181
//!
82+
//! # Atomic accesses to read-only memory
83+
//!
84+
//! In general, atomic accesses on read-only memory are Undefined Behavior. For instance, attempting
85+
//! to do a `compare_exchange` that will definitely fail (making it conceptually a read-only
86+
//! operation) can still cause a page fault if the underlying memory page is mapped read-only. Since
87+
//! atomic `load`s might be implemented using compare-exchange operations, even a `load` can fault
88+
//! on read-only memory.
89+
//!
90+
//! However, as an exception from this general rule, Rust guarantees that "sufficiently small"
91+
//! atomic loads are implemented in a way that works on read-only memory. This threshold of
92+
//! "sufficiently small" depends on the architecture:
93+
//!
94+
//! | Target architecture | Maximal atomic `load` size that is guaranteed read-only |
95+
//! |----------|---------|
96+
//! | `x86` | 4 bytes |
97+
//! | `x86_64` | 8 bytes |
98+
//!
99+
//! Any atomic `load` on read-only memory larger than the given size are Undefined Behavior. For
100+
//! architectures not listed above, all atomic `load` on read-only memory are Undefined Behavior.
101+
//!
82102
//! # Examples
83103
//!
84104
//! A simple spinlock:

0 commit comments

Comments
 (0)