Skip to content

Commit a2fb48e

Browse files
committed
Auto merge of #31761 - Amanieu:volatile, r=alexcrichton
Tracking issue: #31756 RFC: rust-lang/rfcs#1467 I've made these unstable for now. Should they be stabilized straight away since we've had plenty of experience with people using the unstable intrinsics?
2 parents c894ff6 + 3b67e46 commit a2fb48e

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/libcore/ptr.rs

+48
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,54 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
161161
intrinsics::move_val_init(&mut *dst, src)
162162
}
163163

164+
/// Performs a volatile read of the value from `src` without moving it. This
165+
/// leaves the memory in `src` unchanged.
166+
///
167+
/// Volatile operations are intended to act on I/O memory, and are guaranteed
168+
/// to not be elided or reordered by the compiler across other volatile
169+
/// operations. See the LLVM documentation on [[volatile]].
170+
///
171+
/// [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
172+
///
173+
/// # Safety
174+
///
175+
/// Beyond accepting a raw pointer, this is unsafe because it semantically
176+
/// moves the value out of `src` without preventing further usage of `src`.
177+
/// If `T` is not `Copy`, then care must be taken to ensure that the value at
178+
/// `src` is not used before the data is overwritten again (e.g. with `write`,
179+
/// `zero_memory`, or `copy_memory`). Note that `*src = foo` counts as a use
180+
/// because it will attempt to drop the value previously at `*src`.
181+
#[inline]
182+
#[unstable(feature = "volatile", reason = "recently added", issue = "31756")]
183+
pub unsafe fn read_volatile<T>(src: *const T) -> T {
184+
intrinsics::volatile_load(src)
185+
}
186+
187+
/// Performs a volatile write of a memory location with the given value without
188+
/// reading or dropping the old value.
189+
///
190+
/// Volatile operations are intended to act on I/O memory, and are guaranteed
191+
/// to not be elided or reordered by the compiler across other volatile
192+
/// operations. See the LLVM documentation on [[volatile]].
193+
///
194+
/// [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
195+
///
196+
/// # Safety
197+
///
198+
/// This operation is marked unsafe because it accepts a raw pointer.
199+
///
200+
/// It does not drop the contents of `dst`. This is safe, but it could leak
201+
/// allocations or resources, so care must be taken not to overwrite an object
202+
/// that should be dropped.
203+
///
204+
/// This is appropriate for initializing uninitialized memory, or overwriting
205+
/// memory that has previously been `read` from.
206+
#[inline]
207+
#[unstable(feature = "volatile", reason = "recently added", issue = "31756")]
208+
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
209+
intrinsics::volatile_store(dst, src);
210+
}
211+
164212
#[lang = "const_ptr"]
165213
impl<T: ?Sized> *const T {
166214
/// Returns true if the pointer is null.

src/test/run-make/volatile-intrinsics/main.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(core_intrinsics)]
11+
#![feature(core_intrinsics, volatile)]
1212

1313
use std::intrinsics::{volatile_load, volatile_store};
14+
use std::ptr::{read_volatile, write_volatile};
1415

1516
pub fn main() {
1617
unsafe {
1718
let mut i : isize = 1;
1819
volatile_store(&mut i, 2);
1920
assert_eq!(volatile_load(&i), 2);
2021
}
22+
unsafe {
23+
let mut i : isize = 1;
24+
write_volatile(&mut i, 2);
25+
assert_eq!(read_volatile(&i), 2);
26+
}
2127
}

0 commit comments

Comments
 (0)