rust/src/libstd/sys
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
..
cloudabi Format libstd/sys with rustfmt 2019-11-29 18:37:58 -08:00
hermit Format libstd/sys with rustfmt 2019-11-29 18:37:58 -08:00
sgx Format libstd/sys with rustfmt 2019-11-29 18:37:58 -08:00
unix Auto merge of #66887 - dtolnay:rollup-uxowp8d, r=Centril 2019-11-30 12:42:44 +00:00
vxworks Format libstd/sys with rustfmt 2019-11-29 18:37:58 -08:00
wasi Format libstd/sys with rustfmt 2019-11-29 18:37:58 -08:00
wasm Rollup merge of #66705 - pitdicker:atomic_mut_ptr, r=KodrAus 2019-11-30 16:56:47 +01:00
windows Format libstd/sys with rustfmt 2019-11-29 18:37:58 -08:00
mod.rs rename cfg(rustdoc) into cfg(doc) 2019-11-06 18:32:51 +01:00