rust/src/libstd
Mazdak Farrokhzad 123406cac7
Rollup merge of #66705 - pitdicker:atomic_mut_ptr, r=KodrAus
Atomic as_mut_ptr

I encountered the following pattern a few times: In Rust we use some atomic type like `AtomicI32`, and an FFI interface exposes this as `*mut i32` (or some similar `libc` type).

It was not obvious to me if a just transmuting a pointer to the atomic was acceptable, or if this should use a cast that goes through an `UnsafeCell`. See https://github.com/rust-lang/rust/issues/66136#issuecomment-557802477

Transmuting the pointer directly:
```rust
let atomic = AtomicI32::new(1);
let ptr = &atomic as *const AtomicI32 as *mut i32;
unsafe {
    ffi(ptr);
}
```

A dance with `UnsafeCell`:
```rust
let atomic = AtomicI32::new(1);
unsafe {
    let ptr = (&*(&atomic as *const AtomicI32 as *const UnsafeCell<i32>)).get();
    ffi(ptr);
}
```

Maybe in the end both ways could be valid. But why not expose a direct method to get a pointer from the standard library?

An `as_mut_ptr` method on atomics can be safe, because only the use of the resulting pointer is where things can get unsafe. I documented its use for FFI, and "Doing non-atomic reads and writes on the resulting integer can be a data race."

The standard library could make use this method in a few places in the WASM module.

cc @RalfJung as you answered my original question.
2019-11-30 16:56:47 +01:00
..
benches Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
collections Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
ffi Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
io Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
net Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
os Format libstd/os with rustfmt 2019-11-29 18:32:46 -08:00
prelude Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
sync Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
sys Rollup merge of #66705 - pitdicker:atomic_mut_ptr, r=KodrAus 2019-11-30 16:56:47 +01:00
sys_common Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
tests Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
thread Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
alloc.rs Fix intra-doc link resolution failure on re-exporting libstd 2019-05-04 23:48:57 +09:00
ascii.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
backtrace.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
build.rs redesign of the interface to the unikernel HermitCore 2019-10-06 15:26:14 +00:00
Cargo.toml Merge branch 'master' into rusty-hermit, resolve conflicts 2019-10-25 09:09:55 +02:00
env.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
error.rs Fix spelling typos 2019-11-26 22:19:54 -05:00
f32.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
f64.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
fs.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
future.rs Remove some stack frames from .async calls 2019-11-13 17:14:50 -08:00
keyword_docs.rs add missing 'static lifetime in docs 2019-11-16 08:40:35 +01:00
lib.rs Rollup merge of #66705 - pitdicker:atomic_mut_ptr, r=KodrAus 2019-11-30 16:56:47 +01:00
macros.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
memchr.rs libstd => 2018 2019-02-28 04:06:15 +09:00
num.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
panic.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
panicking.rs rename update_count_then_panic -> rust_panic_without_hook 2019-11-26 10:23:15 +01:00
path.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
primitive_docs.rs Stabilize the never_type, written !. 2019-11-21 14:55:32 +01:00
process.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
rt.rs Format libstd with rustfmt 2019-11-29 18:43:27 -08:00
time.rs Fix typo on now() comments 2019-10-05 12:23:10 +02:00