From 88186934960dae4f616df815eb25205c2713f503 Mon Sep 17 00:00:00 2001 From: Flavio Percoco Date: Mon, 22 Dec 2014 17:15:51 +0100 Subject: Relax `Arc` bounds don't require Sync+Send Besides the above making sense, it'll also allow us to make `RacyCell` private and use UnsafeCell instead. --- src/libstd/comm/mod.rs | 13 ++++--------- src/libstd/sync/mutex.rs | 9 ++++----- src/libstd/sys/unix/mutex.rs | 5 ++--- src/libstd/thread.rs | 19 +++++++++++++------ src/libstd/thread_local/scoped.rs | 4 ++-- 5 files changed, 25 insertions(+), 25 deletions(-) (limited to 'src/libstd') 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 { } -- cgit 1.4.1-3-g733a5