diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index 4810e15d68b..3d10628b1cb 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -139,7 +139,7 @@ struct ArcInner { data: T, } -impl Arc { +impl Arc { /// Constructs a new `Arc`. /// /// # Examples diff --git a/src/libstd/comm/mod.rs b/src/libstd/comm/mod.rs index c317be85ebc..269d988d1ee 100644 --- a/src/libstd/comm/mod.rs +++ b/src/libstd/comm/mod.rs @@ -1027,23 +1027,18 @@ impl Drop for Receiver { /// A version of `UnsafeCell` intended for use in concurrent data /// structures (for example, you might put it in an `Arc`). -pub struct RacyCell(pub UnsafeCell); +struct RacyCell(pub UnsafeCell); impl RacyCell { - /// DOX - pub fn new(value: T) -> RacyCell { + + fn new(value: T) -> RacyCell { RacyCell(UnsafeCell { value: value }) } - /// DOX - pub unsafe fn get(&self) -> *mut T { + unsafe fn get(&self) -> *mut T { self.0.get() } - /// DOX - pub unsafe fn into_inner(self) -> T { - self.0.into_inner() - } } unsafe impl Send for RacyCell { } diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index 77c358c6259..fbbbb3e77a4 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -10,7 +10,6 @@ use prelude::*; -use comm::RacyCell; use cell::UnsafeCell; use kinds::marker; use sync::{poison, AsMutexGuard}; @@ -71,7 +70,7 @@ pub struct Mutex { // time, so to ensure that the native mutex is used correctly we box the // inner lock to give it a constant address. inner: Box, - data: RacyCell, + data: UnsafeCell, } unsafe impl Send for Mutex { } @@ -101,7 +100,7 @@ unsafe impl Sync for Mutex { } /// ``` pub struct StaticMutex { lock: sys::Mutex, - poison: RacyCell, + poison: UnsafeCell, } unsafe impl Sync for StaticMutex {} @@ -132,7 +131,7 @@ pub struct StaticMutexGuard { /// other mutex constants. pub const MUTEX_INIT: StaticMutex = StaticMutex { lock: sys::MUTEX_INIT, - poison: RacyCell(UnsafeCell { value: poison::Flag { failed: false } }), + poison: UnsafeCell { value: poison::Flag { failed: false } }, }; impl Mutex { @@ -140,7 +139,7 @@ impl Mutex { pub fn new(t: T) -> Mutex { Mutex { inner: box MUTEX_INIT, - data: RacyCell::new(t), + data: UnsafeCell::new(t), } } diff --git a/src/libstd/sys/unix/mutex.rs b/src/libstd/sys/unix/mutex.rs index 3b0114b3e90..81f8659d6ae 100644 --- a/src/libstd/sys/unix/mutex.rs +++ b/src/libstd/sys/unix/mutex.rs @@ -8,13 +8,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use comm::RacyCell; use cell::UnsafeCell; use kinds::Sync; use sys::sync as ffi; use sys_common::mutex; -pub struct Mutex { inner: RacyCell } +pub struct Mutex { inner: UnsafeCell } #[inline] pub unsafe fn raw(m: &Mutex) -> *mut ffi::pthread_mutex_t { @@ -22,7 +21,7 @@ pub unsafe fn raw(m: &Mutex) -> *mut ffi::pthread_mutex_t { } pub const MUTEX_INIT: Mutex = Mutex { - inner: RacyCell(UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER }), + inner: UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER }, }; unsafe impl Sync for Mutex {} diff --git a/src/libstd/thread.rs b/src/libstd/thread.rs index 45d5c5e0aab..56731bd7ec3 100644 --- a/src/libstd/thread.rs +++ b/src/libstd/thread.rs @@ -127,7 +127,7 @@ use any::Any; use borrow::IntoCow; use boxed::Box; -use comm::RacyCell; +use cell::UnsafeCell; use clone::Clone; use kinds::{Send, Sync}; use ops::{Drop, FnOnce}; @@ -211,8 +211,8 @@ impl Builder { } fn spawn_inner(self, f: Thunk<(), T>) -> JoinGuard { - let my_packet = Arc::new(RacyCell::new(None)); - let their_packet = my_packet.clone(); + let my_packet = Packet(Arc::new(UnsafeCell::new(None))); + let their_packet = Packet(my_packet.0.clone()); let Builder { name, stack_size, stdout, stderr } = self; @@ -266,7 +266,7 @@ impl Builder { } }; unsafe { - *their_packet.get() = Some(match (output, try_result) { + *their_packet.0.get() = Some(match (output, try_result) { (Some(data), Ok(_)) => Ok(data), (None, Err(cause)) => Err(cause), _ => unreachable!() @@ -383,6 +383,11 @@ impl thread_info::NewThread for Thread { /// A thread that completes without panicking is considered to exit successfully. pub type Result = ::result::Result>; +struct Packet(Arc>>>); + +unsafe impl Send for Packet {} +unsafe impl Sync for Packet {} + #[must_use] /// An RAII-style guard that will block until thread termination when dropped. /// @@ -391,9 +396,11 @@ pub struct JoinGuard { native: imp::rust_thread, thread: Thread, joined: bool, - packet: Arc>>>, + packet: Packet, } +unsafe impl Sync for JoinGuard {} + impl JoinGuard { /// Extract a handle to the thread this guard will join on. pub fn thread(&self) -> &Thread { @@ -410,7 +417,7 @@ impl JoinGuard { unsafe { imp::join(self.native) }; self.joined = true; unsafe { - (*self.packet.get()).take().unwrap() + (*self.packet.0.get()).take().unwrap() } } diff --git a/src/libstd/thread_local/scoped.rs b/src/libstd/thread_local/scoped.rs index 3ea051b16f2..756c86c2115 100644 --- a/src/libstd/thread_local/scoped.rs +++ b/src/libstd/thread_local/scoped.rs @@ -196,10 +196,10 @@ impl Key { #[cfg(not(any(windows, target_os = "android", target_os = "ios")))] mod imp { - use std::comm::RacyCell; + use std::cell::UnsafeCell; #[doc(hidden)] - pub struct KeyInner { pub inner: RacyCell<*mut T> } + pub struct KeyInner { pub inner: UnsafeCell<*mut T> } unsafe impl ::kinds::Sync for KeyInner { } diff --git a/src/test/run-pass/issue-17718-static-unsafe-interior.rs b/src/test/run-pass/issue-17718-static-unsafe-interior.rs index 82bfdb0612a..a25632b3376 100644 --- a/src/test/run-pass/issue-17718-static-unsafe-interior.rs +++ b/src/test/run-pass/issue-17718-static-unsafe-interior.rs @@ -9,29 +9,32 @@ // except according to those terms. use std::kinds::marker; -use std::comm::RacyCell; use std::cell::UnsafeCell; struct MyUnsafe { - value: RacyCell + value: UnsafeCell } impl MyUnsafe { fn forbidden(&self) {} } +impl Sync for MyUnsafe {} + enum UnsafeEnum { VariantSafe, - VariantUnsafe(RacyCell) + VariantUnsafe(UnsafeCell) } +impl Sync for UnsafeEnum {} + static STATIC1: UnsafeEnum = UnsafeEnum::VariantSafe; -static STATIC2: RacyCell = RacyCell(UnsafeCell { value: 1 }); -const CONST: RacyCell = RacyCell(UnsafeCell { value: 1 }); +static STATIC2: UnsafeCell = UnsafeCell { value: 1 }; +const CONST: UnsafeCell = UnsafeCell { value: 1 }; static STATIC3: MyUnsafe = MyUnsafe{value: CONST}; -static STATIC4: &'static RacyCell = &STATIC2; +static STATIC4: &'static UnsafeCell = &STATIC2; struct Wrap { value: T @@ -39,13 +42,11 @@ struct Wrap { unsafe impl Sync for Wrap {} -static UNSAFE: RacyCell = RacyCell(UnsafeCell{value: 1}); -static WRAPPED_UNSAFE: Wrap<&'static RacyCell> = Wrap { value: &UNSAFE }; +static UNSAFE: UnsafeCell = UnsafeCell{value: 1}; +static WRAPPED_UNSAFE: Wrap<&'static UnsafeCell> = Wrap { value: &UNSAFE }; fn main() { let a = &STATIC1; STATIC3.forbidden() } - -