diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index 52561d482c39dcc4b5acf9a05de030865870b91d..fa45f72b3a1f36ee55e53c9bb5b50dffc7b07845 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -61,9 +61,6 @@ #[stable(feature = "rust1", since = "1.0.0")] pub struct Condvar { inner: Box } -unsafe impl Send for Condvar {} -unsafe impl Sync for Condvar {} - /// Statically allocated condition variables. /// /// This structure is identical to `Condvar` except that it is suitable for use @@ -83,9 +80,6 @@ pub struct StaticCondvar { mutex: AtomicUsize, } -unsafe impl Send for StaticCondvar {} -unsafe impl Sync for StaticCondvar {} - /// Constant initializer for a statically allocated condition variable. #[unstable(feature = "std_misc", reason = "may be merged with Condvar in the future")] diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index e77c4d2e5ebd4ec7ed498335eb1a3a3a67abe284..a4129e315ffa6224abc52afaa474a9346104dbfb 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -152,8 +152,6 @@ pub struct StaticMutex { poison: poison::Flag, } -unsafe impl Sync for StaticMutex {} - /// An RAII implementation of a "scoped lock" of a mutex. When this structure is /// dropped (falls out of scope), the lock will be unlocked. /// diff --git a/src/libstd/sync/once.rs b/src/libstd/sync/once.rs index 97f985e21e8aba0bbc5068d55db1e2e6432695ea..d2054a1e819ab377d5204799f8e33e77e7bad937 100644 --- a/src/libstd/sync/once.rs +++ b/src/libstd/sync/once.rs @@ -13,10 +13,9 @@ //! This primitive is meant to be used to run one-time initialization. An //! example use case would be for initializing an FFI library. +use prelude::v1::*; + use isize; -use marker::Sync; -use mem::drop; -use ops::FnOnce; use sync::atomic::{AtomicIsize, Ordering, ATOMIC_ISIZE_INIT}; use sync::{StaticMutex, MUTEX_INIT}; @@ -43,8 +42,6 @@ pub struct Once { lock_cnt: AtomicIsize, } -unsafe impl Sync for Once {} - /// Initialization value for static `Once` values. #[stable(feature = "rust1", since = "1.0.0")] pub const ONCE_INIT: Once = Once { diff --git a/src/libstd/sync/poison.rs b/src/libstd/sync/poison.rs index 32c8150ba4070172473cfe7d7602dca56a8c09de..2587ff5238ea7b86eb29d107856be5ed31629198 100644 --- a/src/libstd/sync/poison.rs +++ b/src/libstd/sync/poison.rs @@ -16,6 +16,12 @@ use thread; pub struct Flag { failed: UnsafeCell } + +// This flag is only ever accessed with a lock previously held. Note that this +// a totally private structure. +unsafe impl Send for Flag {} +unsafe impl Sync for Flag {} + pub const FLAG_INIT: Flag = Flag { failed: UnsafeCell { value: false } }; impl Flag { diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index cd833b17867106baa763e5ab4c1defe3fddb2a01..c32fd88ab2452cddb57b23b40f515bd23e7038bb 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -97,9 +97,6 @@ pub struct StaticRwLock { poison: poison::Flag, } -unsafe impl Send for StaticRwLock {} -unsafe impl Sync for StaticRwLock {} - /// Constant initialization for a statically-initialized rwlock. #[unstable(feature = "std_misc", reason = "may be merged with RwLock in the future")] diff --git a/src/libstd/sys/unix/condvar.rs b/src/libstd/sys/unix/condvar.rs index 3bc414731526300d15d5fa0d0716b132c7d892f6..90dfebc4c454c7735808255c52a5994e08452f34 100644 --- a/src/libstd/sys/unix/condvar.rs +++ b/src/libstd/sys/unix/condvar.rs @@ -8,10 +8,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use prelude::v1::*; + use cell::UnsafeCell; use libc; use ptr; -use std::option::Option::{Some, None}; use sys::mutex::{self, Mutex}; use sys::time; use sys::sync as ffi; @@ -20,6 +21,9 @@ pub struct Condvar { inner: UnsafeCell } +unsafe impl Send for Condvar {} +unsafe impl Sync for Condvar {} + pub const CONDVAR_INIT: Condvar = Condvar { inner: UnsafeCell { value: ffi::PTHREAD_COND_INITIALIZER }, }; diff --git a/src/libstd/sys/unix/mutex.rs b/src/libstd/sys/unix/mutex.rs index 9e1527aef201abdfb0215143a18bdb46ef0bc4cd..f87c0339533dfb672273d881cbf9b7623e9b16c2 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 prelude::v1::*; + use cell::UnsafeCell; -use marker::Sync; use sys::sync as ffi; use sys_common::mutex; @@ -24,6 +25,7 @@ pub unsafe fn raw(m: &Mutex) -> *mut ffi::pthread_mutex_t { inner: UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER }, }; +unsafe impl Send for Mutex {} unsafe impl Sync for Mutex {} impl Mutex { diff --git a/src/libstd/sys/unix/rwlock.rs b/src/libstd/sys/unix/rwlock.rs index 54523e0076dc3c47ce41897cb416b4b2f795b385..b857f4ab75fed31160e45e63acdf887f545acdca 100644 --- a/src/libstd/sys/unix/rwlock.rs +++ b/src/libstd/sys/unix/rwlock.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use prelude::v1::*; + use cell::UnsafeCell; use sys::sync as ffi; @@ -17,6 +19,9 @@ pub struct RWLock { inner: UnsafeCell } inner: UnsafeCell { value: ffi::PTHREAD_RWLOCK_INITIALIZER }, }; +unsafe impl Send for RWLock {} +unsafe impl Sync for RWLock {} + impl RWLock { #[inline] pub unsafe fn new() -> RWLock { diff --git a/src/libstd/sys/windows/condvar.rs b/src/libstd/sys/windows/condvar.rs index db8038006fd6a81451a3783951609bddb7f8719f..071637e3a939f82eda497d497dabdbef9e72ea31 100644 --- a/src/libstd/sys/windows/condvar.rs +++ b/src/libstd/sys/windows/condvar.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use prelude::v1::*; + use cell::UnsafeCell; use libc::{self, DWORD}; use os; @@ -17,6 +19,9 @@ pub struct Condvar { inner: UnsafeCell } +unsafe impl Send for Condvar {} +unsafe impl Sync for Condvar {} + pub const CONDVAR_INIT: Condvar = Condvar { inner: UnsafeCell { value: ffi::CONDITION_VARIABLE_INIT } }; diff --git a/src/libstd/sys/windows/mutex.rs b/src/libstd/sys/windows/mutex.rs index 75495efc7cbb60f9d2a7f12ffa78980f2540ab39..0847f3b52bfabe171e370bed4e1b6a43cb75bfa5 100644 --- a/src/libstd/sys/windows/mutex.rs +++ b/src/libstd/sys/windows/mutex.rs @@ -8,7 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use marker::Sync; +use prelude::v1::*; + use cell::UnsafeCell; use sys::sync as ffi; @@ -18,6 +19,7 @@ pub struct Mutex { inner: UnsafeCell } inner: UnsafeCell { value: ffi::SRWLOCK_INIT } }; +unsafe impl Send for Mutex {} unsafe impl Sync for Mutex {} #[inline] @@ -27,14 +29,15 @@ pub unsafe fn raw(m: &Mutex) -> ffi::PSRWLOCK { // So you might be asking why we're using SRWLock instead of CriticalSection? // -// 1. SRWLock is several times faster than CriticalSection according to benchmarks performed on both -// Windows 8 and Windows 7. +// 1. SRWLock is several times faster than CriticalSection according to +// benchmarks performed on both Windows 8 and Windows 7. // -// 2. CriticalSection allows recursive locking while SRWLock deadlocks. The Unix implementation -// deadlocks so consistency is preferred. See #19962 for more details. +// 2. CriticalSection allows recursive locking while SRWLock deadlocks. The Unix +// implementation deadlocks so consistency is preferred. See #19962 for more +// details. // -// 3. While CriticalSection is fair and SRWLock is not, the current Rust policy is there there are -// no guarantees of fairness. +// 3. While CriticalSection is fair and SRWLock is not, the current Rust policy +// is there there are no guarantees of fairness. impl Mutex { #[inline] diff --git a/src/libstd/sys/windows/rwlock.rs b/src/libstd/sys/windows/rwlock.rs index 76fe352ed7717cf6ca623bb693a1fcf48663d5c1..009605535a0237026d18ec29e0d783feb626a362 100644 --- a/src/libstd/sys/windows/rwlock.rs +++ b/src/libstd/sys/windows/rwlock.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use prelude::v1::*; + use cell::UnsafeCell; use sys::sync as ffi; @@ -17,6 +19,9 @@ pub struct RWLock { inner: UnsafeCell } inner: UnsafeCell { value: ffi::SRWLOCK_INIT } }; +unsafe impl Send for RWLock {} +unsafe impl Sync for RWLock {} + impl RWLock { #[inline] pub unsafe fn read(&self) { diff --git a/src/test/run-pass/std-sync-right-kind-impls.rs b/src/test/run-pass/std-sync-right-kind-impls.rs new file mode 100644 index 0000000000000000000000000000000000000000..d2d72ed16618df6e74be3321eb5f9346043cc665 --- /dev/null +++ b/src/test/run-pass/std-sync-right-kind-impls.rs @@ -0,0 +1,27 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::sync; + +fn assert_both() {} + +fn main() { + assert_both::(); + assert_both::(); + assert_both::(); + assert_both::>(); + assert_both::(); + assert_both::>(); + assert_both::(); + assert_both::(); + assert_both::>(); + assert_both::>(); + assert_both::(); +}