Skip to content

Commit ceaef25

Browse files
committed
docs: add comment on volatile and provenance
1 parent 80cb937 commit ceaef25

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

library/core/src/ptr/mod.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,20 +1763,22 @@ pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
17631763
/// - When a volatile operation is used for memory inside an [allocation], it behaves exactly like
17641764
/// [`read`], except for the additional guarantee that it won't be elided or reordered (see
17651765
/// above). This implies that the operation will actually access memory and not e.g. be lowered to
1766-
/// a register access or stack pop. Other than that, all the usual rules for memory accesses
1767-
/// apply. In particular, just like in C, whether an operation is volatile has no bearing
1768-
/// whatsoever on questions involving concurrent access from multiple threads. Volatile accesses
1769-
/// behave exactly like non-atomic accesses in that regard.
1766+
/// a register access or stack pop. Other than that, all the usual rules for memory accesses apply
1767+
/// (including provenance). In particular, just like in C, whether an operation is volatile has no
1768+
/// bearing whatsoever on questions involving concurrent access from multiple threads. Volatile
1769+
/// accesses behave exactly like non-atomic accesses in that regard.
17701770
///
17711771
/// - Volatile operations, however, may also be used to access memory that is _outside_ of any Rust
17721772
/// allocation. In this use-case, the pointer does *not* have to be [valid] for reads. This is
17731773
/// typically used for CPU and peripheral registers that must be accessed via an I/O memory
17741774
/// mapping, most commonly at fixed addresses reserved by the hardware. These often have special
17751775
/// semantics associated to their manipulation, and cannot be used as general purpose memory.
17761776
/// Here, any address value is possible, including 0 and [`usize::MAX`], so long as the semantics
1777-
/// of such a read are well-defined by the target hardware. The access must not trap. It can cause
1778-
/// side-effects, but those must not affect Rust-allocated memory in in any way. This access is
1779-
/// still not considered [atomic], and as such it cannot be used for inter-thread synchronization.
1777+
/// of such a read are well-defined by the target hardware. The provenance of the pointer is
1778+
/// irrelevant, and it can be created with [`without_provenance`]. The access must not trap. It
1779+
/// can cause side-effects, but those must not affect Rust-allocated memory in in any way. This
1780+
/// access is still not considered [atomic], and as such it cannot be used for inter-thread
1781+
/// synchronization.
17801782
///
17811783
/// Note that volatile memory operations on zero-sized types (e.g., if a zero-sized type is passed
17821784
/// to `read_volatile`) are noops and may be ignored.
@@ -1854,20 +1856,22 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
18541856
/// - When a volatile operation is used for memory inside an [allocation], it behaves exactly like
18551857
/// [`write()`], except for the additional guarantee that it won't be elided or reordered (see
18561858
/// above). This implies that the operation will actually access memory and not e.g. be lowered to
1857-
/// a register access or stack pop. Other than that, all the usual rules for memory accesses
1858-
/// apply. In particular, just like in C, whether an operation is volatile has no bearing
1859-
/// whatsoever on questions involving concurrent access from multiple threads. Volatile accesses
1860-
/// behave exactly like non-atomic accesses in that regard.
1859+
/// a register access or stack pop. Other than that, all the usual rules for memory accesses apply
1860+
/// (including provenance). In particular, just like in C, whether an operation is volatile has no
1861+
/// bearing whatsoever on questions involving concurrent access from multiple threads. Volatile
1862+
/// accesses behave exactly like non-atomic accesses in that regard.
18611863
///
18621864
/// - Volatile operations, however, may also be used to access memory that is _outside_ of any Rust
18631865
/// allocation. In this use-case, the pointer does *not* have to be [valid] for writes. This is
18641866
/// typically used for CPU and peripheral registers that must be accessed via an I/O memory
18651867
/// mapping, most commonly at fixed addresses reserved by the hardware. These often have special
18661868
/// semantics associated to their manipulation, and cannot be used as general purpose memory.
18671869
/// Here, any address value is possible, including 0 and [`usize::MAX`], so long as the semantics
1868-
/// of such a write are well-defined by the target hardware. The access must not trap. It can
1869-
/// cause side-effects, but those must not affect Rust-allocated memory in any way. This access is
1870-
/// still not considered [atomic], and as such it cannot be used for inter-thread synchronization.
1870+
/// of such a write are well-defined by the target hardware. The provenance of the pointer is
1871+
/// irrelevant, and it can be created with [`without_provenance`]. The access must not trap. It
1872+
/// can cause side-effects, but those must not affect Rust-allocated memory in any way. This
1873+
/// access is still not considered [atomic], and as such it cannot be used for inter-thread
1874+
/// synchronization.
18711875
///
18721876
/// Note that volatile memory operations on zero-sized types (e.g., if a zero-sized type is passed
18731877
/// to `write_volatile`) are noops and may be ignored.

0 commit comments

Comments
 (0)