From 35275076f52d53c3dcd9dee85d92a2059a663225 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 8 Mar 2015 21:59:08 +1100 Subject: [PATCH 1/3] Remove unneeded `T: Send + Sync` bounds from `Arc`. The requirement `T: Send + Sync` only matters if the `Arc` crosses thread boundaries, and that is adequately controlled by the impls of `Send`/`Sync` for `Arc` itself. If `T` doesn't satisfy the bounds, then the `Arc` cannot cross thread boundaries and so everything is still safe (`Arc` just acts like an expensive `Rc`). --- src/liballoc/arc.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index dc1938cac1ada..ce25f907ec2cc 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -265,7 +265,7 @@ impl Deref for Arc { } } -impl Arc { +impl Arc { /// Make a mutable reference from the given `Arc`. /// /// This is also referred to as a copy-on-write operation because the inner data is cloned if @@ -300,7 +300,7 @@ impl Arc { #[unsafe_destructor] #[stable(feature = "rust1", since = "1.0.0")] -impl Drop for Arc { +impl Drop for Arc { /// Drops the `Arc`. /// /// This will decrement the strong reference count. If the strong reference count becomes zero @@ -367,7 +367,7 @@ impl Drop for Arc { #[unstable(feature = "alloc", reason = "Weak pointers may not belong in this module.")] -impl Weak { +impl Weak { /// Upgrades a weak reference to a strong reference. /// /// Upgrades the `Weak` reference to an `Arc`, if possible. @@ -406,7 +406,7 @@ impl Weak { #[unstable(feature = "alloc", reason = "Weak pointers may not belong in this module.")] -impl Clone for Weak { +impl Clone for Weak { /// Makes a clone of the `Weak`. /// /// This increases the weak reference count. @@ -430,7 +430,7 @@ impl Clone for Weak { #[unsafe_destructor] #[stable(feature = "rust1", since = "1.0.0")] -impl Drop for Weak { +impl Drop for Weak { /// Drops the `Weak`. /// /// This will decrement the weak reference count. From 25d070f228a101a806165a434b150a59a54f08ba Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 8 Mar 2015 22:01:01 +1100 Subject: [PATCH 2/3] Remove unneeded `Send`/`Sync` bounds from `Mutex`/`RwLock`. The requirements `T: Send` and `T: Send + Sync` for `Mutex` and `RwLock` respectively only matter if those types are shared/sent across thread boundaries, and that is adequately controlled by the impls of `Send`/`Sync` for them. If `T` doesn't satisfy the bounds, then the types cannot cross thread boundaries and so everything is still safe (the two types just act like an expensive `RefCell`). --- src/libstd/sync/mutex.rs | 8 +++++--- src/libstd/sync/rwlock.rs | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index 6f0febd61e803..7b5076acbca0e 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -121,6 +121,8 @@ pub struct Mutex { data: UnsafeCell, } +// these are the only places where `T: Send` matters; all other +// functionality works fine on a single thread. unsafe impl Send for Mutex { } unsafe impl Sync for Mutex { } @@ -179,7 +181,7 @@ pub const MUTEX_INIT: StaticMutex = StaticMutex { poison: poison::FLAG_INIT, }; -impl Mutex { +impl Mutex { /// Creates a new mutex in an unlocked state ready for use. #[stable(feature = "rust1", since = "1.0.0")] pub fn new(t: T) -> Mutex { @@ -242,7 +244,7 @@ impl Mutex { #[unsafe_destructor] #[stable(feature = "rust1", since = "1.0.0")] -impl Drop for Mutex { +impl Drop for Mutex { fn drop(&mut self) { // This is actually safe b/c we know that there is no further usage of // this mutex (it's up to the user to arrange for a mutex to get @@ -252,7 +254,7 @@ impl Drop for Mutex { } #[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Debug for Mutex { +impl fmt::Debug for Mutex { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.try_lock() { Ok(guard) => write!(f, "Mutex {{ data: {:?} }}", *guard), diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index e9ff6c0bf9df0..d32eae15a1bca 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -129,7 +129,7 @@ pub struct RwLockWriteGuard<'a, T: 'a> { impl<'a, T> !marker::Send for RwLockWriteGuard<'a, T> {} -impl RwLock { +impl RwLock { /// Creates a new instance of an `RwLock` which is unlocked. /// /// # Examples @@ -257,7 +257,7 @@ impl Drop for RwLock { } #[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Debug for RwLock { +impl fmt::Debug for RwLock { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.try_read() { Ok(guard) => write!(f, "RwLock {{ data: {:?} }}", *guard), From 0f6b43aa8f288d3d12adc8747fb5060956d7f0e5 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 8 Mar 2015 22:02:21 +1100 Subject: [PATCH 3/3] Remove unneeded `Send` bounds from `std::sync::mpsc`. The requirements `T: Send` only matter if the channel crosses thread boundaries i.e. the `Sender` or `Reciever` are sent across thread boundaries, and which is adequately controlled by the impls of `Send` for them. If `T` doesn't satisfy the bounds, then the types cannot cross thread boundaries and so everything is still safe (the pair of types collectively behave like a `Rc>`, or something of that nature). --- src/libstd/sync/mpsc/mod.rs | 24 ++++++++++++------------ src/libstd/sync/mpsc/mpsc_queue.rs | 4 ++-- src/libstd/sync/mpsc/oneshot.rs | 4 ++-- src/libstd/sync/mpsc/shared.rs | 4 ++-- src/libstd/sync/mpsc/spsc_queue.rs | 6 +++--- src/libstd/sync/mpsc/stream.rs | 4 ++-- src/libstd/sync/mpsc/sync.rs | 12 ++++++------ 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/libstd/sync/mpsc/mod.rs b/src/libstd/sync/mpsc/mod.rs index 1a1e9e69e7112..6ce94592c95c0 100644 --- a/src/libstd/sync/mpsc/mod.rs +++ b/src/libstd/sync/mpsc/mod.rs @@ -485,7 +485,7 @@ impl UnsafeFlavor for Receiver { /// println!("{:?}", rx.recv().unwrap()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] -pub fn channel() -> (Sender, Receiver) { +pub fn channel() -> (Sender, Receiver) { let a = Arc::new(UnsafeCell::new(oneshot::Packet::new())); (Sender::new(Flavor::Oneshot(a.clone())), Receiver::new(Flavor::Oneshot(a))) } @@ -525,7 +525,7 @@ pub fn channel() -> (Sender, Receiver) { /// assert_eq!(rx.recv().unwrap(), 2); /// ``` #[stable(feature = "rust1", since = "1.0.0")] -pub fn sync_channel(bound: usize) -> (SyncSender, Receiver) { +pub fn sync_channel(bound: usize) -> (SyncSender, Receiver) { let a = Arc::new(UnsafeCell::new(sync::Packet::new(bound))); (SyncSender::new(a.clone()), Receiver::new(Flavor::Sync(a))) } @@ -534,7 +534,7 @@ pub fn sync_channel(bound: usize) -> (SyncSender, Receiver) { // Sender //////////////////////////////////////////////////////////////////////////////// -impl Sender { +impl Sender { fn new(inner: Flavor) -> Sender { Sender { inner: UnsafeCell::new(inner), @@ -616,7 +616,7 @@ impl Sender { } #[stable(feature = "rust1", since = "1.0.0")] -impl Clone for Sender { +impl Clone for Sender { fn clone(&self) -> Sender { let (packet, sleeper, guard) = match *unsafe { self.inner() } { Flavor::Oneshot(ref p) => { @@ -662,7 +662,7 @@ impl Clone for Sender { #[unsafe_destructor] #[stable(feature = "rust1", since = "1.0.0")] -impl Drop for Sender { +impl Drop for Sender { fn drop(&mut self) { match *unsafe { self.inner_mut() } { Flavor::Oneshot(ref mut p) => unsafe { (*p.get()).drop_chan(); }, @@ -677,7 +677,7 @@ impl Drop for Sender { // SyncSender //////////////////////////////////////////////////////////////////////////////// -impl SyncSender { +impl SyncSender { fn new(inner: Arc>>) -> SyncSender { SyncSender { inner: inner } } @@ -717,7 +717,7 @@ impl SyncSender { } #[stable(feature = "rust1", since = "1.0.0")] -impl Clone for SyncSender { +impl Clone for SyncSender { fn clone(&self) -> SyncSender { unsafe { (*self.inner.get()).clone_chan(); } return SyncSender::new(self.inner.clone()); @@ -726,7 +726,7 @@ impl Clone for SyncSender { #[unsafe_destructor] #[stable(feature = "rust1", since = "1.0.0")] -impl Drop for SyncSender { +impl Drop for SyncSender { fn drop(&mut self) { unsafe { (*self.inner.get()).drop_chan(); } } @@ -736,7 +736,7 @@ impl Drop for SyncSender { // Receiver //////////////////////////////////////////////////////////////////////////////// -impl Receiver { +impl Receiver { fn new(inner: Flavor) -> Receiver { Receiver { inner: UnsafeCell::new(inner) } } @@ -855,7 +855,7 @@ impl Receiver { } } -impl select::Packet for Receiver { +impl select::Packet for Receiver { fn can_recv(&self) -> bool { loop { let new_port = match *unsafe { self.inner() } { @@ -942,7 +942,7 @@ impl select::Packet for Receiver { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T: Send> Iterator for Iter<'a, T> { +impl<'a, T> Iterator for Iter<'a, T> { type Item = T; fn next(&mut self) -> Option { self.rx.recv().ok() } @@ -950,7 +950,7 @@ impl<'a, T: Send> Iterator for Iter<'a, T> { #[unsafe_destructor] #[stable(feature = "rust1", since = "1.0.0")] -impl Drop for Receiver { +impl Drop for Receiver { fn drop(&mut self) { match *unsafe { self.inner_mut() } { Flavor::Oneshot(ref mut p) => unsafe { (*p.get()).drop_port(); }, diff --git a/src/libstd/sync/mpsc/mpsc_queue.rs b/src/libstd/sync/mpsc/mpsc_queue.rs index 14ed253d8e27e..8b6672e0c27fe 100644 --- a/src/libstd/sync/mpsc/mpsc_queue.rs +++ b/src/libstd/sync/mpsc/mpsc_queue.rs @@ -89,7 +89,7 @@ impl Node { } } -impl Queue { +impl Queue { /// Creates a new queue that is safe to share among multiple producers and /// one consumer. pub fn new() -> Queue { @@ -140,7 +140,7 @@ impl Queue { #[unsafe_destructor] #[stable(feature = "rust1", since = "1.0.0")] -impl Drop for Queue { +impl Drop for Queue { fn drop(&mut self) { unsafe { let mut cur = *self.tail.get(); diff --git a/src/libstd/sync/mpsc/oneshot.rs b/src/libstd/sync/mpsc/oneshot.rs index f287712d9d45d..c6e8d87a22e71 100644 --- a/src/libstd/sync/mpsc/oneshot.rs +++ b/src/libstd/sync/mpsc/oneshot.rs @@ -88,7 +88,7 @@ enum MyUpgrade { GoUp(Receiver), } -impl Packet { +impl Packet { pub fn new() -> Packet { Packet { data: None, @@ -368,7 +368,7 @@ impl Packet { } #[unsafe_destructor] -impl Drop for Packet { +impl Drop for Packet { fn drop(&mut self) { assert_eq!(self.state.load(Ordering::SeqCst), DISCONNECTED); } diff --git a/src/libstd/sync/mpsc/shared.rs b/src/libstd/sync/mpsc/shared.rs index 8d14824d37fee..b611ad3c35d29 100644 --- a/src/libstd/sync/mpsc/shared.rs +++ b/src/libstd/sync/mpsc/shared.rs @@ -64,7 +64,7 @@ pub enum Failure { Disconnected, } -impl Packet { +impl Packet { // Creation of a packet *must* be followed by a call to postinit_lock // and later by inherit_blocker pub fn new() -> Packet { @@ -474,7 +474,7 @@ impl Packet { } #[unsafe_destructor] -impl Drop for Packet { +impl Drop for Packet { fn drop(&mut self) { // Note that this load is not only an assert for correctness about // disconnection, but also a proper fence before the read of diff --git a/src/libstd/sync/mpsc/spsc_queue.rs b/src/libstd/sync/mpsc/spsc_queue.rs index 3fb13739aa75a..c75ac13080866 100644 --- a/src/libstd/sync/mpsc/spsc_queue.rs +++ b/src/libstd/sync/mpsc/spsc_queue.rs @@ -78,7 +78,7 @@ unsafe impl Send for Queue { } unsafe impl Sync for Queue { } -impl Node { +impl Node { fn new() -> *mut Node { unsafe { boxed::into_raw(box Node { @@ -89,7 +89,7 @@ impl Node { } } -impl Queue { +impl Queue { /// Creates a new queue. /// /// This is unsafe as the type system doesn't enforce a single @@ -227,7 +227,7 @@ impl Queue { } #[unsafe_destructor] -impl Drop for Queue { +impl Drop for Queue { fn drop(&mut self) { unsafe { let mut cur = *self.first.get(); diff --git a/src/libstd/sync/mpsc/stream.rs b/src/libstd/sync/mpsc/stream.rs index 5a1e05f9c1565..f0363fae84f73 100644 --- a/src/libstd/sync/mpsc/stream.rs +++ b/src/libstd/sync/mpsc/stream.rs @@ -74,7 +74,7 @@ enum Message { GoUp(Receiver), } -impl Packet { +impl Packet { pub fn new() -> Packet { Packet { queue: unsafe { spsc::Queue::new(128) }, @@ -472,7 +472,7 @@ impl Packet { } #[unsafe_destructor] -impl Drop for Packet { +impl Drop for Packet { fn drop(&mut self) { // Note that this load is not only an assert for correctness about // disconnection, but also a proper fence before the read of diff --git a/src/libstd/sync/mpsc/sync.rs b/src/libstd/sync/mpsc/sync.rs index 33c1614e1b297..6221ca59b54f7 100644 --- a/src/libstd/sync/mpsc/sync.rs +++ b/src/libstd/sync/mpsc/sync.rs @@ -113,10 +113,10 @@ pub enum Failure { /// Atomically blocks the current thread, placing it into `slot`, unlocking `lock` /// in the meantime. This re-locks the mutex upon returning. -fn wait<'a, 'b, T: Send>(lock: &'a Mutex>, - mut guard: MutexGuard<'b, State>, - f: fn(SignalToken) -> Blocker) - -> MutexGuard<'a, State> +fn wait<'a, 'b, T>(lock: &'a Mutex>, + mut guard: MutexGuard<'b, State>, + f: fn(SignalToken) -> Blocker) + -> MutexGuard<'a, State> { let (wait_token, signal_token) = blocking::tokens(); match mem::replace(&mut guard.blocker, f(signal_token)) { @@ -136,7 +136,7 @@ fn wakeup(token: SignalToken, guard: MutexGuard>) { token.signal(); } -impl Packet { +impl Packet { pub fn new(cap: usize) -> Packet { Packet { channels: AtomicUsize::new(1), @@ -412,7 +412,7 @@ impl Packet { } #[unsafe_destructor] -impl Drop for Packet { +impl Drop for Packet { fn drop(&mut self) { assert_eq!(self.channels.load(Ordering::SeqCst), 0); let mut guard = self.lock.lock().unwrap();