From e2116c8fba6e73bc2bbf7cb6bb41911d4daed043 Mon Sep 17 00:00:00 2001 From: Flavio Percoco Date: Mon, 22 Dec 2014 12:29:46 +0100 Subject: Move RacyCell to `std::comm` RacyCell is not exactly what we'd like as a final implementation for this. Therefore, we're moving it under `std::comm` and also making it private. --- src/libcore/atomic.rs | 34 ++++++++++++++-------- src/libcore/cell.rs | 27 +---------------- src/libstd/comm/mod.rs | 29 +++++++++++++++++- src/libstd/sync/mutex.rs | 5 ++-- src/libstd/sys/unix/mutex.rs | 3 +- src/libstd/thread.rs | 2 +- src/libstd/thread_local/scoped.rs | 5 ++-- .../run-pass/issue-17718-static-unsafe-interior.rs | 3 +- 8 files changed, 61 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/libcore/atomic.rs b/src/libcore/atomic.rs index a34f13d127c..9452d0a64bf 100644 --- a/src/libcore/atomic.rs +++ b/src/libcore/atomic.rs @@ -14,33 +14,43 @@ pub use self::Ordering::*; +use kinds::Sync; + use intrinsics; -use cell::{UnsafeCell, RacyCell}; +use cell::UnsafeCell; /// A boolean type which can be safely shared between threads. #[stable] pub struct AtomicBool { - v: RacyCell, + v: UnsafeCell, } +unsafe impl Sync for AtomicBool {} + /// A signed integer type which can be safely shared between threads. #[stable] pub struct AtomicInt { - v: RacyCell, + v: UnsafeCell, } +unsafe impl Sync for AtomicInt {} + /// An unsigned integer type which can be safely shared between threads. #[stable] pub struct AtomicUint { - v: RacyCell, + v: UnsafeCell, } +unsafe impl Sync for AtomicUint {} + /// A raw pointer type which can be safely shared between threads. #[stable] pub struct AtomicPtr { - p: RacyCell, + p: UnsafeCell, } +unsafe impl Sync for AtomicPtr {} + /// Atomic memory orderings /// /// Memory orderings limit the ways that both the compiler and CPU may reorder @@ -80,15 +90,15 @@ pub enum Ordering { /// An `AtomicBool` initialized to `false`. #[unstable = "may be renamed, pending conventions for static initalizers"] pub const INIT_ATOMIC_BOOL: AtomicBool = - AtomicBool { v: RacyCell(UnsafeCell { value: 0 }) }; + AtomicBool { v: UnsafeCell { value: 0 } }; /// An `AtomicInt` initialized to `0`. #[unstable = "may be renamed, pending conventions for static initalizers"] pub const INIT_ATOMIC_INT: AtomicInt = - AtomicInt { v: RacyCell(UnsafeCell { value: 0 }) }; + AtomicInt { v: UnsafeCell { value: 0 } }; /// An `AtomicUint` initialized to `0`. #[unstable = "may be renamed, pending conventions for static initalizers"] pub const INIT_ATOMIC_UINT: AtomicUint = - AtomicUint { v: RacyCell(UnsafeCell { value: 0 }) }; + AtomicUint { v: UnsafeCell { value: 0, } }; // NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly const UINT_TRUE: uint = -1; @@ -108,7 +118,7 @@ impl AtomicBool { #[stable] pub fn new(v: bool) -> AtomicBool { let val = if v { UINT_TRUE } else { 0 }; - AtomicBool { v: RacyCell::new(val) } + AtomicBool { v: UnsafeCell::new(val) } } /// Loads a value from the bool. @@ -348,7 +358,7 @@ impl AtomicInt { #[inline] #[stable] pub fn new(v: int) -> AtomicInt { - AtomicInt {v: RacyCell::new(v)} + AtomicInt {v: UnsafeCell::new(v)} } /// Loads a value from the int. @@ -534,7 +544,7 @@ impl AtomicUint { #[inline] #[stable] pub fn new(v: uint) -> AtomicUint { - AtomicUint { v: RacyCell::new(v) } + AtomicUint { v: UnsafeCell::new(v) } } /// Loads a value from the uint. @@ -721,7 +731,7 @@ impl AtomicPtr { #[inline] #[stable] pub fn new(p: *mut T) -> AtomicPtr { - AtomicPtr { p: RacyCell::new(p as uint) } + AtomicPtr { p: UnsafeCell::new(p as uint) } } /// Loads a value from the pointer. diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 6fc6c2a569d..b45424a5eed 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -158,7 +158,7 @@ use clone::Clone; use cmp::PartialEq; use default::Default; -use kinds::{marker, Copy, Send, Sync}; +use kinds::{marker, Copy}; use ops::{Deref, DerefMut, Drop}; use option::Option; use option::Option::{None, Some}; @@ -555,28 +555,3 @@ impl UnsafeCell { #[deprecated = "renamed to into_inner()"] pub unsafe fn unwrap(self) -> T { self.into_inner() } } - -/// 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); - -impl RacyCell { - /// DOX - pub fn new(value: T) -> RacyCell { - RacyCell(UnsafeCell { value: value }) - } - - /// DOX - pub 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 { } - -unsafe impl Sync for RacyCell { } // Oh dear diff --git a/src/libstd/comm/mod.rs b/src/libstd/comm/mod.rs index 618a5eebf0f..c317be85ebc 100644 --- a/src/libstd/comm/mod.rs +++ b/src/libstd/comm/mod.rs @@ -319,9 +319,10 @@ pub use self::TrySendError::*; use self::Flavor::*; use alloc::arc::Arc; +use core::kinds; use core::kinds::marker; use core::mem; -use core::cell::{UnsafeCell, RacyCell}; +use core::cell::UnsafeCell; pub use self::select::{Select, Handle}; use self::select::StartResult; @@ -1024,6 +1025,32 @@ 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); + +impl RacyCell { + /// DOX + pub fn new(value: T) -> RacyCell { + RacyCell(UnsafeCell { value: value }) + } + + /// DOX + pub 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 { } + +unsafe impl kinds::Sync for RacyCell { } // Oh dear + + #[cfg(test)] mod test { use prelude::*; diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index d2dafac281a..77c358c6259 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -10,8 +10,9 @@ use prelude::*; -use cell::{UnsafeCell, RacyCell}; -use kinds::{marker, Sync}; +use comm::RacyCell; +use cell::UnsafeCell; +use kinds::marker; use sync::{poison, AsMutexGuard}; use sys_common::mutex as sys; diff --git a/src/libstd/sys/unix/mutex.rs b/src/libstd/sys/unix/mutex.rs index 986f50bc8de..3b0114b3e90 100644 --- a/src/libstd/sys/unix/mutex.rs +++ b/src/libstd/sys/unix/mutex.rs @@ -8,8 +8,9 @@ // 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 cell::{UnsafeCell, RacyCell}; use sys::sync as ffi; use sys_common::mutex; diff --git a/src/libstd/thread.rs b/src/libstd/thread.rs index 92aa5201ec3..45d5c5e0aab 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 cell::RacyCell; +use comm::RacyCell; use clone::Clone; use kinds::{Send, Sync}; use ops::{Drop, FnOnce}; diff --git a/src/libstd/thread_local/scoped.rs b/src/libstd/thread_local/scoped.rs index d7ea163cc80..3ea051b16f2 100644 --- a/src/libstd/thread_local/scoped.rs +++ b/src/libstd/thread_local/scoped.rs @@ -196,11 +196,10 @@ impl Key { #[cfg(not(any(windows, target_os = "android", target_os = "ios")))] mod imp { - use std::cell::UnsafeCell; + use std::comm::RacyCell; - // SNAP c9f6d69 switch to `Cell` #[doc(hidden)] - pub struct KeyInner { pub inner: UnsafeCell<*mut T> } + pub struct KeyInner { pub inner: RacyCell<*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 17dd6d69fd4..82bfdb0612a 100644 --- a/src/test/run-pass/issue-17718-static-unsafe-interior.rs +++ b/src/test/run-pass/issue-17718-static-unsafe-interior.rs @@ -9,7 +9,8 @@ // except according to those terms. use std::kinds::marker; -use std::cell::{UnsafeCell, RacyCell}; +use std::comm::RacyCell; +use std::cell::UnsafeCell; struct MyUnsafe { value: RacyCell -- cgit 1.4.1-3-g733a5