Skip to content

Commit 7b7cf84

Browse files
committed
Rollup merge of rust-lang#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 rust-lang#22574 and wanted to ensure that we had our story straight on these implementations.
2 parents c950ee9 + 64fe93e commit 7b7cf84

File tree

12 files changed

+68
-25
lines changed

12 files changed

+68
-25
lines changed

src/libstd/sync/condvar.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ use sync::{mutex, MutexGuard, PoisonError};
6161
#[stable(feature = "rust1", since = "1.0.0")]
6262
pub struct Condvar { inner: Box<StaticCondvar> }
6363

64-
unsafe impl Send for Condvar {}
65-
unsafe impl Sync for Condvar {}
66-
6764
/// Statically allocated condition variables.
6865
///
6966
/// This structure is identical to `Condvar` except that it is suitable for use
@@ -83,9 +80,6 @@ pub struct StaticCondvar {
8380
mutex: AtomicUsize,
8481
}
8582

86-
unsafe impl Send for StaticCondvar {}
87-
unsafe impl Sync for StaticCondvar {}
88-
8983
/// Constant initializer for a statically allocated condition variable.
9084
#[unstable(feature = "std_misc",
9185
reason = "may be merged with Condvar in the future")]

src/libstd/sync/mutex.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ pub struct StaticMutex {
152152
poison: poison::Flag,
153153
}
154154

155-
unsafe impl Sync for StaticMutex {}
156-
157155
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
158156
/// dropped (falls out of scope), the lock will be unlocked.
159157
///

src/libstd/sync/once.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
//! This primitive is meant to be used to run one-time initialization. An
1414
//! example use case would be for initializing an FFI library.
1515
16+
use prelude::v1::*;
17+
1618
use isize;
17-
use marker::Sync;
18-
use mem::drop;
19-
use ops::FnOnce;
2019
use sync::atomic::{AtomicIsize, Ordering, ATOMIC_ISIZE_INIT};
2120
use sync::{StaticMutex, MUTEX_INIT};
2221

@@ -43,8 +42,6 @@ pub struct Once {
4342
lock_cnt: AtomicIsize,
4443
}
4544

46-
unsafe impl Sync for Once {}
47-
4845
/// Initialization value for static `Once` values.
4946
#[stable(feature = "rust1", since = "1.0.0")]
5047
pub const ONCE_INIT: Once = Once {

src/libstd/sync/poison.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ use fmt;
1616
use thread;
1717

1818
pub struct Flag { failed: UnsafeCell<bool> }
19+
20+
// This flag is only ever accessed with a lock previously held. Note that this
21+
// a totally private structure.
22+
unsafe impl Send for Flag {}
23+
unsafe impl Sync for Flag {}
24+
1925
pub const FLAG_INIT: Flag = Flag { failed: UnsafeCell { value: false } };
2026

2127
impl Flag {

src/libstd/sync/rwlock.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ pub struct StaticRwLock {
9797
poison: poison::Flag,
9898
}
9999

100-
unsafe impl Send for StaticRwLock {}
101-
unsafe impl Sync for StaticRwLock {}
102-
103100
/// Constant initialization for a statically-initialized rwlock.
104101
#[unstable(feature = "std_misc",
105102
reason = "may be merged with RwLock in the future")]

src/libstd/sys/unix/condvar.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use prelude::v1::*;
12+
1113
use cell::UnsafeCell;
1214
use libc;
1315
use ptr;
14-
use std::option::Option::{Some, None};
1516
use sys::mutex::{self, Mutex};
1617
use sys::time;
1718
use sys::sync as ffi;
@@ -20,6 +21,9 @@ use num::{Int, NumCast};
2021

2122
pub struct Condvar { inner: UnsafeCell<ffi::pthread_cond_t> }
2223

24+
unsafe impl Send for Condvar {}
25+
unsafe impl Sync for Condvar {}
26+
2327
pub const CONDVAR_INIT: Condvar = Condvar {
2428
inner: UnsafeCell { value: ffi::PTHREAD_COND_INITIALIZER },
2529
};

src/libstd/sys/unix/mutex.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use prelude::v1::*;
12+
1113
use cell::UnsafeCell;
12-
use marker::Sync;
1314
use sys::sync as ffi;
1415
use sys_common::mutex;
1516

@@ -24,6 +25,7 @@ pub const MUTEX_INIT: Mutex = Mutex {
2425
inner: UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER },
2526
};
2627

28+
unsafe impl Send for Mutex {}
2729
unsafe impl Sync for Mutex {}
2830

2931
impl Mutex {

src/libstd/sys/unix/rwlock.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use prelude::v1::*;
12+
1113
use cell::UnsafeCell;
1214
use sys::sync as ffi;
1315

@@ -17,6 +19,9 @@ pub const RWLOCK_INIT: RWLock = RWLock {
1719
inner: UnsafeCell { value: ffi::PTHREAD_RWLOCK_INITIALIZER },
1820
};
1921

22+
unsafe impl Send for RWLock {}
23+
unsafe impl Sync for RWLock {}
24+
2025
impl RWLock {
2126
#[inline]
2227
pub unsafe fn new() -> RWLock {

src/libstd/sys/windows/condvar.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use prelude::v1::*;
12+
1113
use cell::UnsafeCell;
1214
use libc::{self, DWORD};
1315
use os;
@@ -17,6 +19,9 @@ use time::Duration;
1719

1820
pub struct Condvar { inner: UnsafeCell<ffi::CONDITION_VARIABLE> }
1921

22+
unsafe impl Send for Condvar {}
23+
unsafe impl Sync for Condvar {}
24+
2025
pub const CONDVAR_INIT: Condvar = Condvar {
2126
inner: UnsafeCell { value: ffi::CONDITION_VARIABLE_INIT }
2227
};

src/libstd/sys/windows/mutex.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use marker::Sync;
11+
use prelude::v1::*;
12+
1213
use cell::UnsafeCell;
1314
use sys::sync as ffi;
1415

@@ -18,6 +19,7 @@ pub const MUTEX_INIT: Mutex = Mutex {
1819
inner: UnsafeCell { value: ffi::SRWLOCK_INIT }
1920
};
2021

22+
unsafe impl Send for Mutex {}
2123
unsafe impl Sync for Mutex {}
2224

2325
#[inline]
@@ -27,14 +29,15 @@ pub unsafe fn raw(m: &Mutex) -> ffi::PSRWLOCK {
2729

2830
// So you might be asking why we're using SRWLock instead of CriticalSection?
2931
//
30-
// 1. SRWLock is several times faster than CriticalSection according to benchmarks performed on both
31-
// Windows 8 and Windows 7.
32+
// 1. SRWLock is several times faster than CriticalSection according to
33+
// benchmarks performed on both Windows 8 and Windows 7.
3234
//
33-
// 2. CriticalSection allows recursive locking while SRWLock deadlocks. The Unix implementation
34-
// deadlocks so consistency is preferred. See #19962 for more details.
35+
// 2. CriticalSection allows recursive locking while SRWLock deadlocks. The Unix
36+
// implementation deadlocks so consistency is preferred. See #19962 for more
37+
// details.
3538
//
36-
// 3. While CriticalSection is fair and SRWLock is not, the current Rust policy is there there are
37-
// no guarantees of fairness.
39+
// 3. While CriticalSection is fair and SRWLock is not, the current Rust policy
40+
// is there there are no guarantees of fairness.
3841

3942
impl Mutex {
4043
#[inline]

src/libstd/sys/windows/rwlock.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use prelude::v1::*;
12+
1113
use cell::UnsafeCell;
1214
use sys::sync as ffi;
1315

@@ -17,6 +19,9 @@ pub const RWLOCK_INIT: RWLock = RWLock {
1719
inner: UnsafeCell { value: ffi::SRWLOCK_INIT }
1820
};
1921

22+
unsafe impl Send for RWLock {}
23+
unsafe impl Sync for RWLock {}
24+
2025
impl RWLock {
2126
#[inline]
2227
pub unsafe fn read(&self) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::sync;
12+
13+
fn assert_both<T: Sync + Send>() {}
14+
15+
fn main() {
16+
assert_both::<sync::StaticMutex>();
17+
assert_both::<sync::StaticCondvar>();
18+
assert_both::<sync::StaticRwLock>();
19+
assert_both::<sync::Mutex<()>>();
20+
assert_both::<sync::Condvar>();
21+
assert_both::<sync::RwLock<()>>();
22+
assert_both::<sync::Semaphore>();
23+
assert_both::<sync::Barrier>();
24+
assert_both::<sync::Arc<()>>();
25+
assert_both::<sync::Weak<()>>();
26+
assert_both::<sync::Once>();
27+
}

0 commit comments

Comments
 (0)