Rollup merge of #22596 - alexcrichton:fix-some-impls, r=huonw
This commit removes many unnecessary `unsafe impl` blocks as well as pushing the needed implementations to the lowest level possible. I noticed that the bounds for `RwLock` are a little off when reviewing #22574 and wanted to ensure that we had our story straight on these implementations.
This commit is contained in:
commit
7b7cf84975
12 changed files with 68 additions and 25 deletions
|
@ -61,9 +61,6 @@ use sync::{mutex, MutexGuard, PoisonError};
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub struct Condvar { inner: Box<StaticCondvar> }
|
pub struct Condvar { inner: Box<StaticCondvar> }
|
||||||
|
|
||||||
unsafe impl Send for Condvar {}
|
|
||||||
unsafe impl Sync for Condvar {}
|
|
||||||
|
|
||||||
/// Statically allocated condition variables.
|
/// Statically allocated condition variables.
|
||||||
///
|
///
|
||||||
/// This structure is identical to `Condvar` except that it is suitable for use
|
/// This structure is identical to `Condvar` except that it is suitable for use
|
||||||
|
@ -83,9 +80,6 @@ pub struct StaticCondvar {
|
||||||
mutex: AtomicUsize,
|
mutex: AtomicUsize,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for StaticCondvar {}
|
|
||||||
unsafe impl Sync for StaticCondvar {}
|
|
||||||
|
|
||||||
/// Constant initializer for a statically allocated condition variable.
|
/// Constant initializer for a statically allocated condition variable.
|
||||||
#[unstable(feature = "std_misc",
|
#[unstable(feature = "std_misc",
|
||||||
reason = "may be merged with Condvar in the future")]
|
reason = "may be merged with Condvar in the future")]
|
||||||
|
|
|
@ -152,8 +152,6 @@ pub struct StaticMutex {
|
||||||
poison: poison::Flag,
|
poison: poison::Flag,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Sync for StaticMutex {}
|
|
||||||
|
|
||||||
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
|
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
|
||||||
/// dropped (falls out of scope), the lock will be unlocked.
|
/// dropped (falls out of scope), the lock will be unlocked.
|
||||||
///
|
///
|
||||||
|
|
|
@ -13,10 +13,9 @@
|
||||||
//! This primitive is meant to be used to run one-time initialization. An
|
//! This primitive is meant to be used to run one-time initialization. An
|
||||||
//! example use case would be for initializing an FFI library.
|
//! example use case would be for initializing an FFI library.
|
||||||
|
|
||||||
|
use prelude::v1::*;
|
||||||
|
|
||||||
use isize;
|
use isize;
|
||||||
use marker::Sync;
|
|
||||||
use mem::drop;
|
|
||||||
use ops::FnOnce;
|
|
||||||
use sync::atomic::{AtomicIsize, Ordering, ATOMIC_ISIZE_INIT};
|
use sync::atomic::{AtomicIsize, Ordering, ATOMIC_ISIZE_INIT};
|
||||||
use sync::{StaticMutex, MUTEX_INIT};
|
use sync::{StaticMutex, MUTEX_INIT};
|
||||||
|
|
||||||
|
@ -43,8 +42,6 @@ pub struct Once {
|
||||||
lock_cnt: AtomicIsize,
|
lock_cnt: AtomicIsize,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Sync for Once {}
|
|
||||||
|
|
||||||
/// Initialization value for static `Once` values.
|
/// Initialization value for static `Once` values.
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub const ONCE_INIT: Once = Once {
|
pub const ONCE_INIT: Once = Once {
|
||||||
|
|
|
@ -16,6 +16,12 @@ use fmt;
|
||||||
use thread;
|
use thread;
|
||||||
|
|
||||||
pub struct Flag { failed: UnsafeCell<bool> }
|
pub struct Flag { failed: UnsafeCell<bool> }
|
||||||
|
|
||||||
|
// 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 } };
|
pub const FLAG_INIT: Flag = Flag { failed: UnsafeCell { value: false } };
|
||||||
|
|
||||||
impl Flag {
|
impl Flag {
|
||||||
|
|
|
@ -97,9 +97,6 @@ pub struct StaticRwLock {
|
||||||
poison: poison::Flag,
|
poison: poison::Flag,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for StaticRwLock {}
|
|
||||||
unsafe impl Sync for StaticRwLock {}
|
|
||||||
|
|
||||||
/// Constant initialization for a statically-initialized rwlock.
|
/// Constant initialization for a statically-initialized rwlock.
|
||||||
#[unstable(feature = "std_misc",
|
#[unstable(feature = "std_misc",
|
||||||
reason = "may be merged with RwLock in the future")]
|
reason = "may be merged with RwLock in the future")]
|
||||||
|
|
|
@ -8,10 +8,11 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
use prelude::v1::*;
|
||||||
|
|
||||||
use cell::UnsafeCell;
|
use cell::UnsafeCell;
|
||||||
use libc;
|
use libc;
|
||||||
use ptr;
|
use ptr;
|
||||||
use std::option::Option::{Some, None};
|
|
||||||
use sys::mutex::{self, Mutex};
|
use sys::mutex::{self, Mutex};
|
||||||
use sys::time;
|
use sys::time;
|
||||||
use sys::sync as ffi;
|
use sys::sync as ffi;
|
||||||
|
@ -20,6 +21,9 @@ use num::{Int, NumCast};
|
||||||
|
|
||||||
pub struct Condvar { inner: UnsafeCell<ffi::pthread_cond_t> }
|
pub struct Condvar { inner: UnsafeCell<ffi::pthread_cond_t> }
|
||||||
|
|
||||||
|
unsafe impl Send for Condvar {}
|
||||||
|
unsafe impl Sync for Condvar {}
|
||||||
|
|
||||||
pub const CONDVAR_INIT: Condvar = Condvar {
|
pub const CONDVAR_INIT: Condvar = Condvar {
|
||||||
inner: UnsafeCell { value: ffi::PTHREAD_COND_INITIALIZER },
|
inner: UnsafeCell { value: ffi::PTHREAD_COND_INITIALIZER },
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,8 +8,9 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
use prelude::v1::*;
|
||||||
|
|
||||||
use cell::UnsafeCell;
|
use cell::UnsafeCell;
|
||||||
use marker::Sync;
|
|
||||||
use sys::sync as ffi;
|
use sys::sync as ffi;
|
||||||
use sys_common::mutex;
|
use sys_common::mutex;
|
||||||
|
|
||||||
|
@ -24,6 +25,7 @@ pub const MUTEX_INIT: Mutex = Mutex {
|
||||||
inner: UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER },
|
inner: UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
unsafe impl Send for Mutex {}
|
||||||
unsafe impl Sync for Mutex {}
|
unsafe impl Sync for Mutex {}
|
||||||
|
|
||||||
impl Mutex {
|
impl Mutex {
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
use prelude::v1::*;
|
||||||
|
|
||||||
use cell::UnsafeCell;
|
use cell::UnsafeCell;
|
||||||
use sys::sync as ffi;
|
use sys::sync as ffi;
|
||||||
|
|
||||||
|
@ -17,6 +19,9 @@ pub const RWLOCK_INIT: RWLock = RWLock {
|
||||||
inner: UnsafeCell { value: ffi::PTHREAD_RWLOCK_INITIALIZER },
|
inner: UnsafeCell { value: ffi::PTHREAD_RWLOCK_INITIALIZER },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
unsafe impl Send for RWLock {}
|
||||||
|
unsafe impl Sync for RWLock {}
|
||||||
|
|
||||||
impl RWLock {
|
impl RWLock {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn new() -> RWLock {
|
pub unsafe fn new() -> RWLock {
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
use prelude::v1::*;
|
||||||
|
|
||||||
use cell::UnsafeCell;
|
use cell::UnsafeCell;
|
||||||
use libc::{self, DWORD};
|
use libc::{self, DWORD};
|
||||||
use os;
|
use os;
|
||||||
|
@ -17,6 +19,9 @@ use time::Duration;
|
||||||
|
|
||||||
pub struct Condvar { inner: UnsafeCell<ffi::CONDITION_VARIABLE> }
|
pub struct Condvar { inner: UnsafeCell<ffi::CONDITION_VARIABLE> }
|
||||||
|
|
||||||
|
unsafe impl Send for Condvar {}
|
||||||
|
unsafe impl Sync for Condvar {}
|
||||||
|
|
||||||
pub const CONDVAR_INIT: Condvar = Condvar {
|
pub const CONDVAR_INIT: Condvar = Condvar {
|
||||||
inner: UnsafeCell { value: ffi::CONDITION_VARIABLE_INIT }
|
inner: UnsafeCell { value: ffi::CONDITION_VARIABLE_INIT }
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,7 +8,8 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use marker::Sync;
|
use prelude::v1::*;
|
||||||
|
|
||||||
use cell::UnsafeCell;
|
use cell::UnsafeCell;
|
||||||
use sys::sync as ffi;
|
use sys::sync as ffi;
|
||||||
|
|
||||||
|
@ -18,6 +19,7 @@ pub const MUTEX_INIT: Mutex = Mutex {
|
||||||
inner: UnsafeCell { value: ffi::SRWLOCK_INIT }
|
inner: UnsafeCell { value: ffi::SRWLOCK_INIT }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
unsafe impl Send for Mutex {}
|
||||||
unsafe impl Sync for Mutex {}
|
unsafe impl Sync for Mutex {}
|
||||||
|
|
||||||
#[inline]
|
#[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?
|
// 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
|
// 1. SRWLock is several times faster than CriticalSection according to
|
||||||
// Windows 8 and Windows 7.
|
// benchmarks performed on both Windows 8 and Windows 7.
|
||||||
//
|
//
|
||||||
// 2. CriticalSection allows recursive locking while SRWLock deadlocks. The Unix implementation
|
// 2. CriticalSection allows recursive locking while SRWLock deadlocks. The Unix
|
||||||
// deadlocks so consistency is preferred. See #19962 for more details.
|
// 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
|
// 3. While CriticalSection is fair and SRWLock is not, the current Rust policy
|
||||||
// no guarantees of fairness.
|
// is there there are no guarantees of fairness.
|
||||||
|
|
||||||
impl Mutex {
|
impl Mutex {
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
use prelude::v1::*;
|
||||||
|
|
||||||
use cell::UnsafeCell;
|
use cell::UnsafeCell;
|
||||||
use sys::sync as ffi;
|
use sys::sync as ffi;
|
||||||
|
|
||||||
|
@ -17,6 +19,9 @@ pub const RWLOCK_INIT: RWLock = RWLock {
|
||||||
inner: UnsafeCell { value: ffi::SRWLOCK_INIT }
|
inner: UnsafeCell { value: ffi::SRWLOCK_INIT }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
unsafe impl Send for RWLock {}
|
||||||
|
unsafe impl Sync for RWLock {}
|
||||||
|
|
||||||
impl RWLock {
|
impl RWLock {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn read(&self) {
|
pub unsafe fn read(&self) {
|
||||||
|
|
27
src/test/run-pass/std-sync-right-kind-impls.rs
Normal file
27
src/test/run-pass/std-sync-right-kind-impls.rs
Normal file
|
@ -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 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
use std::sync;
|
||||||
|
|
||||||
|
fn assert_both<T: Sync + Send>() {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert_both::<sync::StaticMutex>();
|
||||||
|
assert_both::<sync::StaticCondvar>();
|
||||||
|
assert_both::<sync::StaticRwLock>();
|
||||||
|
assert_both::<sync::Mutex<()>>();
|
||||||
|
assert_both::<sync::Condvar>();
|
||||||
|
assert_both::<sync::RwLock<()>>();
|
||||||
|
assert_both::<sync::Semaphore>();
|
||||||
|
assert_both::<sync::Barrier>();
|
||||||
|
assert_both::<sync::Arc<()>>();
|
||||||
|
assert_both::<sync::Weak<()>>();
|
||||||
|
assert_both::<sync::Once>();
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue