diff options
| author | Flavio Percoco <flaper87@gmail.com> | 2014-12-22 17:15:51 +0100 |
|---|---|---|
| committer | Flavio Percoco <flaper87@gmail.com> | 2014-12-26 17:26:33 +0100 |
| commit | 88186934960dae4f616df815eb25205c2713f503 (patch) | |
| tree | 10568b070d36a957f6a6aacf726d3dae05f73354 /src/libstd | |
| parent | 7df17a2868004597bff61348060f423ad2384e04 (diff) | |
| download | rust-88186934960dae4f616df815eb25205c2713f503.tar.gz rust-88186934960dae4f616df815eb25205c2713f503.zip | |
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.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/comm/mod.rs | 13 | ||||
| -rw-r--r-- | src/libstd/sync/mutex.rs | 9 | ||||
| -rw-r--r-- | src/libstd/sys/unix/mutex.rs | 5 | ||||
| -rw-r--r-- | src/libstd/thread.rs | 19 | ||||
| -rw-r--r-- | src/libstd/thread_local/scoped.rs | 4 |
5 files changed, 25 insertions, 25 deletions
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<T: Send> Drop for Receiver<T> { /// A version of `UnsafeCell` intended for use in concurrent data /// structures (for example, you might put it in an `Arc`). -pub struct RacyCell<T>(pub UnsafeCell<T>); +struct RacyCell<T>(pub UnsafeCell<T>); impl<T> RacyCell<T> { - /// DOX - pub fn new(value: T) -> RacyCell<T> { + + fn new(value: T) -> RacyCell<T> { 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<T:Send> Send for RacyCell<T> { } 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<T> { // time, so to ensure that the native mutex is used correctly we box the // inner lock to give it a constant address. inner: Box<StaticMutex>, - data: RacyCell<T>, + data: UnsafeCell<T>, } unsafe impl<T:Send> Send for Mutex<T> { } @@ -101,7 +100,7 @@ unsafe impl<T:Send> Sync for Mutex<T> { } /// ``` pub struct StaticMutex { lock: sys::Mutex, - poison: RacyCell<poison::Flag>, + poison: UnsafeCell<poison::Flag>, } 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<T: Send> Mutex<T> { @@ -140,7 +139,7 @@ impl<T: Send> Mutex<T> { pub fn new(t: T) -> Mutex<T> { 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<ffi::pthread_mutex_t> } +pub struct Mutex { inner: UnsafeCell<ffi::pthread_mutex_t> } #[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<T: Send>(self, f: Thunk<(), T>) -> JoinGuard<T> { - 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<T> = ::result::Result<T, Box<Any + Send>>; +struct Packet<T>(Arc<UnsafeCell<Option<Result<T>>>>); + +unsafe impl<T:'static+Send> Send for Packet<T> {} +unsafe impl<T> Sync for Packet<T> {} + #[must_use] /// An RAII-style guard that will block until thread termination when dropped. /// @@ -391,9 +396,11 @@ pub struct JoinGuard<T> { native: imp::rust_thread, thread: Thread, joined: bool, - packet: Arc<RacyCell<Option<Result<T>>>>, + packet: Packet<T>, } +unsafe impl<T: Send> Sync for JoinGuard<T> {} + impl<T: Send> JoinGuard<T> { /// Extract a handle to the thread this guard will join on. pub fn thread(&self) -> &Thread { @@ -410,7 +417,7 @@ impl<T: Send> JoinGuard<T> { 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<T> Key<T> { #[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<T> { pub inner: RacyCell<*mut T> } + pub struct KeyInner<T> { pub inner: UnsafeCell<*mut T> } unsafe impl<T> ::kinds::Sync for KeyInner<T> { } |
